2005-08-27 10:20:46 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// flex_string
|
|
|
|
// Copyright (c) 2001 by Andrei Alexandrescu
|
|
|
|
// Permission to use, copy, modify, distribute and sell this software for any
|
|
|
|
// purpose is hereby granted without fee, provided that the above copyright
|
|
|
|
// notice appear in all copies and that both that copyright notice and this
|
|
|
|
// permission notice appear in supporting documentation.
|
|
|
|
// The author makes no representations about the
|
|
|
|
// suitability of this software for any purpose. It is provided "as is"
|
|
|
|
// without express or implied warranty.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef VECTOR_STRING_STORAGE_INC_
|
|
|
|
#define VECTOR_STRING_STORAGE_INC_
|
|
|
|
|
2006-10-17 19:59:11 +00:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
/* This is the template for a storage policy
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename E, class A = @>
|
|
|
|
class StoragePolicy
|
|
|
|
{
|
|
|
|
typedef E value_type;
|
|
|
|
typedef @ iterator;
|
|
|
|
typedef @ const_iterator;
|
|
|
|
typedef A allocator_type;
|
|
|
|
typedef @ size_type;
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
StoragePolicy(const StoragePolicy& s);
|
|
|
|
StoragePolicy(const A&);
|
|
|
|
StoragePolicy(const E* s, size_type len, const A&);
|
|
|
|
StoragePolicy(size_type len, E c, const A&);
|
|
|
|
~StoragePolicy();
|
|
|
|
|
|
|
|
iterator begin();
|
|
|
|
const_iterator begin() const;
|
|
|
|
iterator end();
|
|
|
|
const_iterator end() const;
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type size() const;
|
|
|
|
size_type max_size() const;
|
|
|
|
size_type capacity() const;
|
|
|
|
|
|
|
|
void reserve(size_type res_arg);
|
|
|
|
|
|
|
|
void append(const E* s, size_type sz);
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
template <class InputIterator>
|
|
|
|
void append(InputIterator b, InputIterator e);
|
|
|
|
|
|
|
|
void resize(size_type newSize, E fill);
|
|
|
|
|
|
|
|
void swap(StoragePolicy& rhs);
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
const E* c_str() const;
|
|
|
|
const E* data() const;
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
A get_allocator() const;
|
|
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
#include <cassert>
|
|
|
|
#include <limits>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template VectorStringStorage
|
|
|
|
// Uses std::vector
|
|
|
|
// Takes advantage of the Empty Base Optimization if available
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <typename E, class A = std::allocator<E> >
|
|
|
|
class VectorStringStorage : protected std::vector<E, A>
|
|
|
|
{
|
|
|
|
typedef std::vector<E, A> base;
|
|
|
|
|
|
|
|
public: // protected:
|
|
|
|
typedef E value_type;
|
|
|
|
typedef typename base::iterator iterator;
|
|
|
|
typedef typename base::const_iterator const_iterator;
|
|
|
|
typedef A allocator_type;
|
|
|
|
typedef typename A::size_type size_type;
|
2006-06-19 12:39:09 +00:00
|
|
|
typedef typename A::reference reference;
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
VectorStringStorage(const VectorStringStorage& s) : base(s)
|
|
|
|
{ }
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
VectorStringStorage(const A& a) : base(1, value_type(), a)
|
|
|
|
{ }
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
VectorStringStorage(const value_type* s, size_type len, const A& a)
|
|
|
|
: base(a)
|
|
|
|
{
|
|
|
|
base::reserve(len + 1);
|
|
|
|
base::insert(base::end(), s, s + len);
|
|
|
|
// Terminating zero
|
|
|
|
base::push_back(value_type());
|
|
|
|
}
|
|
|
|
|
|
|
|
VectorStringStorage(size_type len, E c, const A& a)
|
|
|
|
: base(len + 1, c, a)
|
|
|
|
{
|
|
|
|
// Terminating zero
|
|
|
|
base::back() = value_type();
|
|
|
|
}
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
VectorStringStorage& operator=(const VectorStringStorage& rhs)
|
|
|
|
{
|
|
|
|
base& v = *this;
|
|
|
|
v = rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
iterator begin()
|
|
|
|
{ return base::begin(); }
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
const_iterator begin() const
|
|
|
|
{ return base::begin(); }
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
iterator end()
|
|
|
|
{ return base::end() - 1; }
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
const_iterator end() const
|
|
|
|
{ return base::end() - 1; }
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type size() const
|
|
|
|
{ return base::size() - 1; }
|
|
|
|
|
|
|
|
size_type max_size() const
|
|
|
|
{ return base::max_size() - 1; }
|
|
|
|
|
|
|
|
size_type capacity() const
|
|
|
|
{ return base::capacity() - 1; }
|
|
|
|
|
|
|
|
void reserve(size_type res_arg)
|
2009-03-13 12:07:15 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
assert(res_arg < max_size());
|
2009-03-13 12:07:15 +00:00
|
|
|
base::reserve(res_arg + 1);
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
template <class ForwardIterator>
|
|
|
|
void append(ForwardIterator b, ForwardIterator e)
|
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
const typename std::iterator_traits<ForwardIterator>::difference_type
|
|
|
|
sz = std::distance(b, e);
|
|
|
|
assert(sz >= 0);
|
|
|
|
if (sz == 0) return;
|
|
|
|
base::reserve(base::size() + sz);
|
|
|
|
const value_type & v = *b;
|
|
|
|
struct OnBlockExit
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
VectorStringStorage * that;
|
2006-03-08 17:07:20 +00:00
|
|
|
~OnBlockExit()
|
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
that->base::push_back(value_type());
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
|
|
|
} onBlockExit = { this };
|
|
|
|
(void) onBlockExit;
|
2006-06-19 12:39:09 +00:00
|
|
|
assert(!base::empty());
|
|
|
|
assert(base::back() == value_type());
|
|
|
|
base::back() = v;
|
|
|
|
base::insert(base::end(), ++b, e);
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void resize(size_type n, E c)
|
|
|
|
{
|
|
|
|
base::reserve(n + 1);
|
|
|
|
base::back() = c;
|
|
|
|
base::resize(n + 1, c);
|
|
|
|
base::back() = E();
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(VectorStringStorage& rhs)
|
|
|
|
{ base::swap(rhs); }
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
const E* c_str() const
|
|
|
|
{ return &*begin(); }
|
|
|
|
|
|
|
|
const E* data() const
|
|
|
|
{ return &*begin(); }
|
2009-03-13 12:07:15 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
A get_allocator() const
|
|
|
|
{ return base::get_allocator(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // VECTOR_STRING_STORAGE_INC_
|