37 #ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_ARRAY_POLICIES_HPP
38 #define NEKTAR_LIB_UTILITIES_BASIC_UTILS_ARRAY_POLICIES_HPP
42 #include <boost/shared_ptr.hpp>
43 #include <boost/multi_array.hpp>
44 #include <boost/bind.hpp>
48 template<
typename ObjectType,
typename enabled =
void>
49 class ArrayInitializationPolicy;
53 template<
typename ObjectType>
54 class ArrayInitializationPolicy<ObjectType,
55 typename boost::enable_if<boost::is_fundamental<ObjectType> >::type >
58 static void Initialize(ObjectType* data,
unsigned int itemsToCreate)
62 static void Initialize(ObjectType* data,
unsigned int itemsToCreate,
const ObjectType& initValue)
64 std::fill_n(data, itemsToCreate, initValue);
67 static void Initialize(ObjectType* data,
unsigned int itemsToCreate,
const ObjectType* initValue)
69 std::copy(initValue, initValue + itemsToCreate, data);
83 template<
typename ObjectType>
84 class ArrayInitializationPolicy<ObjectType,
85 typename boost::disable_if<boost::is_fundamental<ObjectType> >::type >
91 static void Initialize(ObjectType* data,
unsigned int itemsToCreate)
93 DoInitialization(data, itemsToCreate,
94 boost::bind(&ArrayInitializationPolicy<ObjectType>::DefaultConstructionWithPlacementNew, _1));
101 static void Initialize(ObjectType* data,
unsigned int itemsToCreate,
const ObjectType& initValue)
103 DoInitialization(data, itemsToCreate,
104 boost::bind(&ArrayInitializationPolicy<ObjectType>::CopyConstructionWithPlacementNew, _1, boost::ref(initValue)));
107 static void Initialize(ObjectType* data,
unsigned int itemsToCreate,
const ObjectType* initValue)
109 DoInitialization(data, itemsToCreate,
110 boost::bind(&ArrayInitializationPolicy<ObjectType>::CopyConstructionFromArray, _1, boost::ref(initValue)));
114 template<
typename CreateType>
115 static void DoInitialization(ObjectType* data,
unsigned int itemsToCreate,
const CreateType& f)
117 unsigned int nextObjectToCreate = 0;
120 for(
unsigned int i = 0; i < itemsToCreate; ++i)
122 ObjectType* memLocation = &data[i];
124 ++nextObjectToCreate;
129 for(
unsigned int i = 0; i < nextObjectToCreate; ++i)
131 ObjectType* memLocation = &data[nextObjectToCreate - i - 1];
132 memLocation->~ObjectType();
138 static void DefaultConstructionWithPlacementNew(ObjectType* element)
140 new (element) ObjectType;
143 static void CopyConstructionWithPlacementNew(ObjectType* element,
const ObjectType& initValue)
145 new (element) ObjectType(initValue);
148 static void CopyConstructionFromArray(ObjectType* element,
const ObjectType*& rhs)
150 new (element) ObjectType(*rhs);
156 template<
typename ObjectType,
typename enabled =
void>
157 class ArrayDestructionPolicy;
159 template<
typename ObjectType>
160 class ArrayDestructionPolicy<ObjectType,
161 typename boost::enable_if<boost::is_fundamental<ObjectType> >::type >
164 static void Destroy(ObjectType* data,
unsigned int itemsToDestroy)
169 template<
typename ObjectType>
170 class ArrayDestructionPolicy<ObjectType,
171 typename boost::disable_if<boost::is_fundamental<ObjectType> >::type >
174 static void Destroy(ObjectType* data,
unsigned int itemsToDestroy)
176 for(
unsigned int i = 0; i < itemsToDestroy; ++i)
178 ObjectType* memLocation = &data[itemsToDestroy - i - 1];
179 memLocation->~ObjectType();
184 template<
typename DataType>
187 ArrayDestructionPolicy<DataType>::Destroy(data, num);
191 template<
typename Dim,
typename DataType,
typename ExtentListType>
192 boost::shared_ptr<boost::multi_array_ref<DataType, Dim::Value> >
195 typedef boost::multi_array_ref<DataType, Dim::Value> ArrayType;
196 unsigned int size = std::accumulate(extent.begin(), extent.end(), 1,
197 std::multiplies<unsigned int>());
200 boost::bind(DeleteStorage<DataType>, storage, size),
204 template<
typename DataType>
205 boost::shared_ptr<boost::multi_array_ref<DataType, 1> >
208 std::vector<unsigned int> extents(1, d1);
209 return CreateStorage<OneD, DataType>(extents);
212 template<
typename DataType>
213 boost::shared_ptr<boost::multi_array_ref<DataType, 2> >
216 unsigned int vals[] = {d1, d2};
217 std::vector<unsigned int> extents(vals, vals+2);
218 return CreateStorage<TwoD, DataType>(extents);
221 template<
typename DataType>
222 boost::shared_ptr<boost::multi_array_ref<DataType, 3> >
225 unsigned int vals[] = {d1, d2, d3};
226 std::vector<unsigned int> extents(vals, vals+3);
227 return CreateStorage<ThreeD, DataType>(extents);
231 #endif //NEKTAR_LIB_UTILITIES_BASIC_UTILS_ARRAY_POLICIES_HPP