21 class Base_property_array
26 Base_property_array(
const std::string& name) : name_(name) {}
29 virtual ~Base_property_array() {}
32 virtual void reserve(
size_t n) = 0;
35 virtual void resize(
size_t n) = 0;
38 virtual void free_memory() = 0;
41 virtual void push_back() = 0;
44 virtual void swap(
size_t i0,
size_t i1) = 0;
47 virtual Base_property_array* clone ()
const = 0;
50 virtual const std::type_info& type() = 0;
53 const std::string& name()
const {
return name_; }
56 void rename(std::string newname)
57 { name_ =
give(newname) ; }
70 class gsProperty_array :
public Base_property_array
75 typedef std::vector<value_type> vector_type;
76 typedef typename vector_type::reference reference;
77 typedef typename vector_type::const_reference const_reference;
79 gsProperty_array(
const std::string& name, T t=T()) :
80 Base_property_array(name), value_(
give(t)) {}
84 virtual void reserve(
size_t n)
89 virtual void resize(
size_t n)
91 data_.resize(n, value_);
94 virtual void push_back()
96 data_.push_back(value_);
99 virtual void free_memory()
101 vector_type(data_).swap(data_);
104 virtual void swap(
size_t i0,
size_t i1)
111 virtual Base_property_array* clone()
const
113 gsProperty_array<T>* p =
new gsProperty_array<T>(name_, value_);
118 virtual const std::type_info& type() {
return typeid(T); }
124 const T* data()
const
131 std::vector<T>& vector()
138 reference operator[](
int _idx)
140 assert(
size_t(_idx) < data_.size() );
145 const_reference operator[](
int _idx)
const
147 assert(
size_t(_idx) < data_.size());
162 gsProperty_array<bool>::data()
const
178 typedef typename gsProperty_array<T>::reference reference;
179 typedef typename gsProperty_array<T>::const_reference const_reference;
181 friend class gsProperty_container;
182 friend class gsSurfMesh;
187 gsProperty(gsProperty_array<T>* p=NULL) : parray_(p) {}
194 operator bool()
const
196 return parray_ != NULL;
199 reference operator[](
int i)
201 assert(parray_ != NULL);
202 return (*parray_)[i];
205 const_reference operator[](
int i)
const
207 assert(parray_ != NULL);
208 return (*parray_)[i];
211 const T* data()
const
213 assert(parray_ != NULL);
214 return parray_->data();
218 std::vector<T>& vector()
220 assert(parray_ != NULL);
221 return parray_->vector();
227 gsProperty_array<T>& array()
229 assert(parray_ != NULL);
233 const gsProperty_array<T>& array()
const
235 assert(parray_ != NULL);
241 gsProperty_array<T>* parray_;
249 class gsProperty_container
254 gsProperty_container() : size_(0) {}
257 virtual ~gsProperty_container() { clear(); }
260 gsProperty_container(
const gsProperty_container& _rhs) { operator=(_rhs); }
263 gsProperty_container& operator=(
const gsProperty_container& _rhs)
268 parrays_.resize(_rhs.n_properties());
270 for (
unsigned int i=0; i<parrays_.size(); ++i)
271 parrays_[i] = _rhs.parrays_[i]->clone();
277 size_t size()
const {
return size_; }
280 size_t n_properties()
const {
return parrays_.size(); }
283 std::vector<std::string> properties()
const
285 std::vector<std::string> names;
286 for (
unsigned int i=0; i<parrays_.size(); ++i)
287 names.push_back(parrays_[i]->name());
293 template <
class T> gsProperty<T> add(
const std::string& name, T t=T())
296 for (
unsigned int i=0; i<parrays_.size(); ++i)
298 if (parrays_[i]->name() == name)
300 std::cerr <<
"[gsProperty_container] A property with name \""
301 << name <<
"\" already exists. Returning invalid property.\n";
302 return gsProperty<T>();
309 gsProperty_array<T>* p =
new gsProperty_array<T>(name,
give(t));
313 parrays_.push_back(p);
314 return gsProperty<T>(p);
319 template <
class T> gsProperty<T>
get(
const std::string& name)
const
321 for (
unsigned int i=0; i<parrays_.size(); ++i)
322 if (parrays_[i]->name() == name)
323 return gsProperty<T>(
dynamic_cast<gsProperty_array<T>*
>(parrays_[i]));
324 return gsProperty<T>();
329 template <
class T> gsProperty<T> get_or_add(
const std::string& name,
const T t=T())
331 gsProperty<T> p = get<T>(name);
332 if (!p) p = add<T>(name,
give(t));
337 bool has(
const std::string& name)
const
339 for (
unsigned int i=0; i<parrays_.size(); ++i)
340 if (parrays_[i]->name() == name)
347 const std::type_info& get_type(
const std::string& name)
349 for (
unsigned int i=0; i<parrays_.size(); ++i)
350 if (parrays_[i]->name() == name)
351 return parrays_[i]->type();
356 void swap(
const std::string& name1,
const std::string& name2)
const
358 std::pair<int,int> spair(-1,-1);
359 for (
unsigned int i=0; i<parrays_.size(); ++i)
361 if (parrays_[i]->name() == name1)
363 if (parrays_[i]->name() == name2)
366 GISMO_ASSERT(spair.first!=-1 && spair.second!=-1,
"error");
367 parrays_[spair.first ]->rename(name2);
368 parrays_[spair.second]->rename(name1);
372 template <
class T>
void rename(gsProperty<T>& h, std::string newname)
374 GISMO_ASSERT( !has(newname),
"There is already a property with this name.");
375 h.parray_->rename(
give(newname));
379 template <
class T>
void remove(gsProperty<T>& h)
381 std::vector<Base_property_array*>::iterator
384 for (; it!=end; ++it)
386 if (*it == h.parray_)
399 for (
unsigned int i=0; i<parrays_.size(); ++i)
407 void reserve(
size_t n)
const
409 for (
unsigned int i=0; i<parrays_.size(); ++i)
410 parrays_[i]->reserve(n);
414 void resize(
size_t n)
416 for (
unsigned int i=0; i<parrays_.size(); ++i)
417 parrays_[i]->resize(n);
422 void free_memory()
const
424 for (
unsigned int i=0; i<parrays_.size(); ++i)
425 parrays_[i]->free_memory();
431 for (
unsigned int i=0; i<parrays_.size(); ++i)
432 parrays_[i]->push_back();
437 void swap(
size_t i0,
size_t i1)
const
439 for (
unsigned int i=0; i<parrays_.size(); ++i)
440 parrays_[i]->swap(i0, i1);
445 std::vector<Base_property_array*> parrays_;
S give(S &x)
Definition: gsMemory.h:266
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89