My Project
stack_containers.hpp
1 #pragma once
2 //-----------------------------------------------------------------------------
3 // Class: Stack Containers
4 // Authors: Modified by LiXizhi
5 // Emails: LiXizhi@yeah.net
6 // Company: ParaEngine corporation
7 // Date: 2008.6.20
8 //-----------------------------------------------------------------------------
9 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // * Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 // * Redistributions in binary form must reproduce the above
18 // copyright notice, this list of conditions and the following disclaimer
19 // in the documentation and/or other materials provided with the
20 // distribution.
21 // * Neither the name of Google Inc. nor the names of its
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 
25 #include <string>
26 #include <vector>
27 
28 namespace ParaEngine
29 {
30  // A macro to disallow the copy constructor and operator= functions
31  // This should be used in the private: declarations for a class
32 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
33  TypeName(const TypeName&); \
34  void operator=(const TypeName&)
35 
36  // This allocator can be used with STL containers to provide a stack buffer
37  // from which to allocate memory and overflows onto the heap. This stack buffer
38  // would be allocated on the stack and allows us to avoid heap operations in
39  // some situations.
40  //
41  // STL likes to make copies of allocators, so the allocator itself can't hold
42  // the data. Instead, we make the creator responsible for creating a
43  // StackAllocator::Source which contains the data. Copying the allocator
44  // merely copies the pointer to this shared source, so all allocators created
45  // based on our allocator will share the same stack buffer.
46  //
47  // This stack buffer implementation is very simple. The first allocation that
48  // fits in the stack buffer will use the stack buffer. Any subsequent
49  // allocations will not use the stack buffer, even if there is unused room.
50  // This makes it appropriate for array-like containers, but the caller should
51  // be sure to reserve() in the container up to the stack buffer size. Otherwise
52  // the container will allocate a small array which will "use up" the stack
53  // buffer.
54  template<typename T, size_t stack_capacity>
55  class StackAllocator : public std::allocator<T> {
56  public:
57  typedef typename std::allocator<T>::pointer pointer;
58  typedef typename std::allocator<T>::size_type size_type;
59 
60  // Backing store for the allocator. The container owner is responsible for
61  // maintaining this for as long as any containers using this allocator are
62  // live.
63  struct Source {
64  Source() : used_stack_buffer_(false) {
65  }
66 
67  // Casts the buffer in its right type.
68  T* stack_buffer() { return reinterpret_cast<T*>(stack_buffer_); }
69  const T* stack_buffer() const {
70  return reinterpret_cast<const T*>(stack_buffer_);
71  }
72 
73  //
74  // IMPORTANT: Take care to ensure that stack_buffer_ is aligned
75  // since it is used to mimic an array of T.
76  // Be careful while declaring any unaligned types (like bool)
77  // before stack_buffer_.
78  //
79 
80  // The buffer itself. It is not of type T because we don't want the
81  // constructors and destructors to be automatically called. Define a POD
82  // buffer of the right size instead.
83  char stack_buffer_[sizeof(T[stack_capacity])];
84 
85  // Set when the stack buffer is used for an allocation. We do not track
86  // how much of the buffer is used, only that somebody is using it.
87  bool used_stack_buffer_;
88  };
89 
90  // Used by containers when they want to refer to an allocator of type U.
91  template<typename U>
92  struct rebind {
94  };
95 
96  // For the straight up copy c-tor, we can share storage.
98  : source_(rhs.source_) {
99  }
100 
101  // ISO C++ requires the following constructor to be defined,
102  // and std::vector in VC++2008SP1 Release fails with an error
103  // in the class _Container_base_aux_alloc_real (from <xutility>)
104  // if the constructor does not exist.
105  // For this constructor, we cannot share storage; there's
106  // no guarantee that the Source buffer of Ts is large enough
107  // for Us.
108  // TODO: If we were fancy pants, perhaps we could share storage
109  // iff sizeof(T) == sizeof(U).
110  template<typename U, size_t other_capacity>
112  : source_(NULL) {
113  }
114 
115  explicit StackAllocator(Source* source) : source_(source) {
116  }
117 
118  // Actually do the allocation. Use the stack buffer if nobody has used it yet
119  // and the size requested fits. Otherwise, fall through to the standard
120  // allocator.
121  pointer allocate(size_type n, void* hint = 0) {
122  if (source_ != NULL && !source_->used_stack_buffer_
123  && n <= stack_capacity) {
124  source_->used_stack_buffer_ = true;
125  return source_->stack_buffer();
126  } else {
127  return std::allocator<T>::allocate(n, hint);
128  }
129  }
130 
131  // Free: when trying to free the stack buffer, just mark it as free. For
132  // non-stack-buffer pointers, just fall though to the standard allocator.
133  void deallocate(pointer p, size_type n) {
134  if (source_ != NULL && p == source_->stack_buffer())
135  source_->used_stack_buffer_ = false;
136  else
137  std::allocator<T>::deallocate(p, n);
138  }
139 
140  private:
141  Source* source_;
142  };
143 
144  // A wrapper around STL containers that maintains a stack-sized buffer that the
145  // initial capacity of the vector is based on. Growing the container beyond the
146  // stack capacity will transparently overflow onto the heap. The container must
147  // support reserve().
148  //
149  // WATCH OUT: the ContainerType MUST use the proper StackAllocator for this
150  // type. This object is really intended to be used only internally. You'll want
151  // to use the wrappers below for different types.
152  template<typename TContainerType, int stack_capacity>
154  public:
155  typedef TContainerType ContainerType;
156  typedef typename ContainerType::value_type ContainedType;
158 
159  // Allocator must be constructed before the container!
160  StackContainer() : allocator_(&stack_data_), container_(allocator_) {
161  // Make the container use the stack allocation by reserving our buffer size
162  // before doing anything else.
163  container_.reserve(stack_capacity);
164  }
165 
166  // Getters for the actual container.
167  //
168  // Danger: any copies of this made using the copy constructor must have
169  // shorter lifetimes than the source. The copy will share the same allocator
170  // and therefore the same stack buffer as the original. Use std::copy to
171  // copy into a "real" container for longer-lived objects.
172  ContainerType& container() { return container_; }
173  const ContainerType& container() const { return container_; }
174 
175  // Support operator-> to get to the container. This allows nicer syntax like:
176  // StackContainer<...> foo;
177  // std::sort(foo->begin(), foo->end());
178  ContainerType* operator->() { return &container_; }
179  const ContainerType* operator->() const { return &container_; }
180 
181 #ifdef UNIT_TEST
182  // Retrieves the stack source so that that unit tests can verify that the
183  // buffer is being used properly.
184  const typename Allocator::Source& stack_data() const {
185  return stack_data_;
186  }
187 #endif
188 
189  protected:
190  typename Allocator::Source stack_data_;
191  Allocator allocator_;
192  ContainerType container_;
193 
194  DISALLOW_COPY_AND_ASSIGN(StackContainer);
195  };
196 
197  // StackString
198  template<size_t stack_capacity>
199  class StackString : public StackContainer<
200  std::basic_string<char,
201  std::char_traits<char>,
202  StackAllocator<char, stack_capacity> >,
203  stack_capacity> {
204  public:
206  std::basic_string<char,
207  std::char_traits<char>,
209  stack_capacity>() {
210  }
211 
212  private:
213  DISALLOW_COPY_AND_ASSIGN(StackString);
214  };
215 
216  // StackVector
217  //
218  // Example:
219  // StackVector<int, 16> foo;
220  // foo->push_back(22); // we have overloaded operator->
221  // foo[0] = 10; // as well as operator[]
222  template<typename T, size_t stack_capacity>
223  class StackVector : public StackContainer<
224  std::vector<T, StackAllocator<T, stack_capacity> >,
225  stack_capacity> {
226  public:
228  std::vector<T, StackAllocator<T, stack_capacity> >,
229  stack_capacity>() {
230  }
231 
232  // We need to put this in STL containers sometimes, which requires a copy
233  // constructor. We can't call the regular copy constructor because that will
234  // take the stack buffer from the original. Here, we create an empty object
235  // and make a stack buffer of its own.
237  : StackContainer<
238  std::vector<T, StackAllocator<T, stack_capacity> >,
239  stack_capacity>() {
240  this->container().assign(other->begin(), other->end());
241  }
242 
244  const StackVector<T, stack_capacity>& other) {
245  this->container().assign(other->begin(), other->end());
246  return *this;
247  }
248 
249  // Vectors are commonly indexed, which isn't very convenient even with
250  // operator-> (using "->at()" does exception stuff we don't want).
251  T& operator[](size_t i) { return this->container().operator[](i); }
252  const T& operator[](size_t i) const {
253  return this->container().operator[](i);
254  }
255  };
256 } // ParaEngine
different physics engine has different winding order.
Definition: EventBinding.h:32
Definition: other.hpp:41
Definition: stack_containers.hpp:63
Definition: stack_containers.hpp:223
Definition: stack_containers.hpp:92
Definition: stack_containers.hpp:55
Definition: stack_containers.hpp:153
Definition: stack_containers.hpp:199