Nugget
fixed_substring.h
1 // Copyright (c) Electronic Arts Inc. All rights reserved.
4 
5 
6 #ifndef EASTL_FIXED_SUBSTRING_H
7 #define EASTL_FIXED_SUBSTRING_H
8 
9 
10 #include <EASTL/string.h>
11 
12 #if defined(EA_PRAGMA_ONCE_SUPPORTED)
13  #pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
14 #endif
15 
16 
17 
18 namespace eastl
19 {
20 
78  template <typename T>
79  class fixed_substring : public basic_string<T>
80  {
81  public:
82  typedef basic_string<T> base_type;
84  typedef typename base_type::size_type size_type;
85  typedef typename base_type::value_type value_type;
86  typedef typename base_type::iterator iterator;
87  typedef typename base_type::const_iterator const_iterator;
88 
89  using base_type::npos;
90  using base_type::mPair;
91  using base_type::AllocateSelf;
92  using base_type::internalLayout;
93  using base_type::get_allocator;
94 
95  private:
96 
97  void SetInternalHeapLayout(value_type* pBeginPtr, size_type nSize, size_type nCap)
98  {
99  internalLayout().SetHeapBeginPtr(pBeginPtr);
100  internalLayout().SetHeapSize(nSize);
101  internalLayout().SetHeapCapacity(nCap);
102  }
103 
104 
105  public:
107  : base_type()
108  {
109  }
110 
111  fixed_substring(const base_type& x)
112  : base_type()
113  {
114  #if EASTL_NAME_ENABLED
115  get_allocator().set_name(x.get_allocator().get_name());
116  #endif
117 
118  assign(x);
119  }
120 
121  // We gain no benefit from having an rvalue move constructor or assignment operator,
122  // as this class is a const class.
123 
124  fixed_substring(const base_type& x, size_type position, size_type n = base_type::npos)
125  : base_type()
126  {
127  #if EASTL_NAME_ENABLED
128  get_allocator().set_name(x.get_allocator().get_name());
129  #endif
130 
131  assign(x, position, n);
132  }
133 
134  fixed_substring(const value_type* p, size_type n)
135  : base_type()
136  {
137  assign(p, n);
138  }
139 
140  fixed_substring(const value_type* p)
141  : base_type()
142  {
143  assign(p);
144  }
145 
146  fixed_substring(const value_type* pBegin, const value_type* pEnd)
147  : base_type()
148  {
149  assign(pBegin, pEnd);
150  }
151 
152  ~fixed_substring()
153  {
154  // We need to reset, as otherwise the parent destructor will
155  // attempt to free our memory.
156  AllocateSelf();
157  }
158 
159  this_type& operator=(const base_type& x)
160  {
161  assign(x);
162  return *this;
163  }
164 
165  this_type& operator=(const value_type* p)
166  {
167  assign(p);
168  return *this;
169  }
170 
171  this_type& assign(const base_type& x)
172  {
173  // By design, we need to cast away const-ness here.
174  SetInternalHeapLayout(const_cast<value_type*>(x.data()), x.size(), x.size());
175  return *this;
176  }
177 
178  this_type& assign(const base_type& x, size_type position, size_type n)
179  {
180  // By design, we need to cast away const-ness here.
181  SetInternalHeapLayout(const_cast<value_type*>(x.data()) + position, n, n);
182  return *this;
183  }
184 
185  this_type& assign(const value_type* p, size_type n)
186  {
187  // By design, we need to cast away const-ness here.
188  SetInternalHeapLayout(const_cast<value_type*>(p), n, n);
189  return *this;
190  }
191 
192  this_type& assign(const value_type* p)
193  {
194  // By design, we need to cast away const-ness here.
195  SetInternalHeapLayout(const_cast<value_type*>(p), (size_type)CharStrlen(p), (size_type)CharStrlen(p));
196  return *this;
197  }
198 
199  this_type& assign(const value_type* pBegin, const value_type* pEnd)
200  {
201  // By design, we need to cast away const-ness here.
202  SetInternalHeapLayout(const_cast<value_type*>(pBegin), (size_type)(pEnd - pBegin), (size_type)(pEnd - pBegin));
203  return *this;
204  }
205 
206 
207  // Partially supported functionality
208  //
209  // When using fixed_substring on a character sequence that is within another
210  // string, the following functions may do one of two things:
211  // 1 Attempt to reallocate
212  // 2 Write a 0 char at the end of the fixed_substring
213  //
214  // Item #1 will result in a crash, due to the attempt by the underlying
215  // string class to free the substring memory. Item #2 will result in a 0
216  // char being written to the character array. Item #2 may or may not be
217  // a problem, depending on how you use fixed_substring. Thus the following
218  // functions cannot be used safely.
219 
220  #if 0 // !defined(EA_COMPILER_NO_DELETED_FUNCTIONS) We may want to enable these deletions after some investigation of possible user impact.
221  this_type& operator=(value_type c) = delete;
222  void resize(size_type n, value_type c) = delete;
223  void resize(size_type n) = delete;
224  void reserve(size_type = 0) = delete;
225  void set_capacity(size_type n) = delete;
226  void clear() = delete;
227  this_type& operator+=(const base_type& x) = delete;
228  this_type& operator+=(const value_type* p) = delete;
229  this_type& operator+=(value_type c) = delete;
230  this_type& append(const base_type& x) = delete;
231  this_type& append(const base_type& x, size_type position, size_type n) = delete;
232  this_type& append(const value_type* p, size_type n) = delete;
233  this_type& append(const value_type* p) = delete;
234  this_type& append(size_type n) = delete;
235  this_type& append(size_type n, value_type c) = delete;
236  this_type& append(const value_type* pBegin, const value_type* pEnd) = delete;
237  this_type& append_sprintf_va_list(const value_type* pFormat, va_list arguments) = delete;
238  this_type& append_sprintf(const value_type* pFormat, ...) = delete;
239  void push_back(value_type c) = delete;
240  void pop_back() = delete;
241  this_type& assign(size_type n, value_type c) = delete;
242  this_type& insert(size_type position, const base_type& x) = delete;
243  this_type& insert(size_type position, const base_type& x, size_type beg, size_type n) = delete;
244  this_type& insert(size_type position, const value_type* p, size_type n) = delete;
245  this_type& insert(size_type position, const value_type* p) = delete;
246  this_type& insert(size_type position, size_type n, value_type c) = delete;
247  iterator insert(const_iterator p, value_type c) = delete;
248  void insert(const_iterator p, size_type n, value_type c) = delete;
249  void insert(const_iterator p, const value_type* pBegin, const value_type* pEnd) = delete;
250  this_type& erase(size_type position = 0, size_type n = npos) = delete;
251  iterator erase(const_iterator p) = delete;
252  iterator erase(const_iterator pBegin, const_iterator pEnd) = delete;
253  void swap(base_type& x) = delete;
254  this_type& sprintf_va_list(const value_type* pFormat, va_list arguments) = delete;
255  this_type& sprintf(const value_type* pFormat, ...) = delete;
256  #endif
257 
258  }; // fixed_substring
259 
260 
261 } // namespace eastl
262 
263 
264 
265 #endif // Header include guard
Definition: string.h:280
Definition: fixed_substring.h:80
EA Standard Template Library.
Definition: algorithm.h:288