1 ////////////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2001 by Andrei Alexandrescu
4 // Permission to use, copy, modify, distribute and sell this software for any
5 // purpose is hereby granted without fee, provided that the above copyright
6 // notice appear in all copies and that both that copyright notice and this
7 // permission notice appear in supporting documentation.
8 // The author makes no representations about the
9 // suitability of this software for any purpose. It is provided "as is"
10 // without express or implied warranty.
11 ////////////////////////////////////////////////////////////////////////////////
13 #ifndef VECTOR_STRING_STORAGE_INC_
14 #define VECTOR_STRING_STORAGE_INC_
16 // $Id: vectorstringstorage.h 754 2006-10-17 19:59:11Z syntheticpp $
19 /* This is the template for a storage policy
20 ////////////////////////////////////////////////////////////////////////////////
21 template <typename E, class A = @>
26 typedef @ const_iterator;
27 typedef A allocator_type;
30 StoragePolicy(const StoragePolicy& s);
31 StoragePolicy(const A&);
32 StoragePolicy(const E* s, size_type len, const A&);
33 StoragePolicy(size_type len, E c, const A&);
37 const_iterator begin() const;
39 const_iterator end() const;
41 size_type size() const;
42 size_type max_size() const;
43 size_type capacity() const;
45 void reserve(size_type res_arg);
47 void append(const E* s, size_type sz);
49 template <class InputIterator>
50 void append(InputIterator b, InputIterator e);
52 void resize(size_type newSize, E fill);
54 void swap(StoragePolicy& rhs);
56 const E* c_str() const;
57 const E* data() const;
59 A get_allocator() const;
61 ////////////////////////////////////////////////////////////////////////////////
72 ////////////////////////////////////////////////////////////////////////////////
73 // class template VectorStringStorage
75 // Takes advantage of the Empty Base Optimization if available
76 ////////////////////////////////////////////////////////////////////////////////
78 template <typename E, class A = std::allocator<E> >
79 class VectorStringStorage : protected std::vector<E, A>
81 typedef std::vector<E, A> base;
85 typedef typename base::iterator iterator;
86 typedef typename base::const_iterator const_iterator;
87 typedef A allocator_type;
88 typedef typename A::size_type size_type;
89 typedef typename A::reference reference;
91 VectorStringStorage(const VectorStringStorage &s) : base(s)
94 VectorStringStorage(const A &a) : base(1, value_type(), a)
97 VectorStringStorage(const value_type *s, size_type len, const A &a)
100 base::reserve(len + 1);
101 base::insert(base::end(), s, s + len);
103 base::push_back(value_type());
106 VectorStringStorage(size_type len, E c, const A &a)
107 : base(len + 1, c, a)
110 base::back() = value_type();
113 VectorStringStorage &operator=(const VectorStringStorage &rhs)
122 return base::begin();
125 const_iterator begin() const
127 return base::begin();
132 return base::end() - 1;
135 const_iterator end() const
137 return base::end() - 1;
140 size_type size() const
142 return base::size() - 1;
145 size_type max_size() const
147 return base::max_size() - 1;
150 size_type capacity() const
152 return base::capacity() - 1;
155 void reserve(size_type res_arg)
157 assert(res_arg < max_size());
158 base::reserve(res_arg + 1);
161 template <class ForwardIterator>
162 void append(ForwardIterator b, ForwardIterator e)
164 const typename std::iterator_traits<ForwardIterator>::difference_type
165 sz = std::distance(b, e);
168 base::reserve(base::size() + sz);
169 const value_type &v = *b;
172 VectorStringStorage *that;
175 that->base::push_back(value_type());
177 } onBlockExit = { this };
179 assert(!base::empty());
180 assert(base::back() == value_type());
182 base::insert(base::end(), ++b, e);
185 void resize(size_type n, E c)
187 base::reserve(n + 1);
189 base::resize(n + 1, c);
193 void swap(VectorStringStorage &rhs)
198 const E *c_str() const
203 const E *data() const
208 A get_allocator() const
210 return base::get_allocator();
215 #endif // VECTOR_STRING_STORAGE_INC_