Developer Documentation
PropertiesT_impl.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 #define PROPERTIES_C
45 
46 #include <cassert>
47 #include <utility>
48 #include <iostream>
49 
50 
51 //-----------------------------------------------------------------------------
52 
65 template<typename T>
66 bool Properties::add_property(PropertyHandleT<T> &_hProp, std::string _name)
67 {
68  // return the property if it exists
69  if(get_property(_hProp, _name))
70  return false;
71 
72  // create it if not
73  // find the first free entry
74  std::vector<BaseProperty*>::iterator it;
75  for(it = properties_.begin(); it != properties_.end(); ++it)
76  if(*it == 0)
77  break;
78 
79  if(it == properties_.end()) // found a free entry?
80  {
81  // append at the end
82  _hProp.idx_ = properties_.size();
83  property_names_.insert( std::pair<std::string, int>(_name, properties_.size()) );
84  properties_.push_back(dynamic_cast<BaseProperty*>(new PropertyT<T>(size_))); // the new one needs to have the same number of entries, to keep the correspondence
85  }else{
86  // insert in the free spot
87  _hProp.idx_ = it - properties_.begin();
88  property_names_.insert( std::pair<std::string, int>(_name, _hProp.idx_) );
89  *it = new PropertyT<T>(size_);
90  }
91  return true;
92 }
93 
94 //-----------------------------------------------------------------------------
95 
106 template<typename T>
107 bool Properties::get_property(PropertyHandleT<T> &_hProp, std::string _name)
108 {
109  // find the name
110  std::map<std::string, int>::iterator f = property_names_.find(_name);
111  if(f == property_names_.end())
112  return false;
113 
114  // return the index
115  _hProp.idx_ = f->second;
116  assert(properties_[_hProp.idx_] != 0);
117  return true;
118 }
119 
120 //-----------------------------------------------------------------------------
121 
130 template<typename T>
132 {
133  if(!_hProp.isValid())
134  return false;
135  assert(_hProp.idx_ < (int)properties_.size());
136  assert(properties_[_hProp.idx_] != 0);
137 
138  // delete the property
139  delete properties_[_hProp.idx_];
140  properties_[_hProp.idx_] = 0;
141 
142  // find the names entry and delete it
143  for(std::map<std::string, int>::iterator it = property_names_.begin(); it != property_names_.end(); ++it)
144  {
145  if(it->second == _hProp.idx_)
146  {
147  property_names_.erase(it);
148  break;
149  }
150  }
151 
152  // invalidate the handle
153  _hProp.idx_ = -1;
154 
155  return true;
156 }
157 
158 //-----------------------------------------------------------------------------
159 
171 template<typename T>
173 {
174  assert(_hProp.idx_ >= 0 && _hProp.idx_ < (int)properties_.size());
175  assert(properties_[_hProp.idx_] != 0);
176  assert(reinterpret_cast< PropertyT<T>* >(properties_[_hProp.idx_]) != 0);
177 
178  return (*reinterpret_cast< PropertyT<T>* >(properties_[_hProp.idx_]))[_index];
179 }
180 
181 //-----------------------------------------------------------------------------
182 // PropertyHandleT
183 //-----------------------------------------------------------------------------
184 
193 template<typename T>
195  idx_(_idx)
196 {
197 }
198 
199 //-----------------------------------------------------------------------------
200 
204 template<typename T>
206 {
207 }
208 
209 //-----------------------------------------------------------------------------
210 
217 template<typename T>
219 {
220  return idx_ >= 0;
221 }
222 
223 //-----------------------------------------------------------------------------
224 // PropertyT
225 //-----------------------------------------------------------------------------
226 
234 template<typename T>
236 {
237  for(unsigned long i = 0; i < _size; ++i)
238  values_.push_back(T());
239 }
240 
241 //-----------------------------------------------------------------------------
242 
246 template<typename T>
248 {
249 }
250 
251 //-----------------------------------------------------------------------------
252 
258 template<typename T>
260 {
261  assert(_index >= 0 && _index < (int)values_.size());
262 
263  return values_[_index];
264 }
265 
266 //-----------------------------------------------------------------------------
267 
271 template<typename T>
273 {
274  assert(_index >= 0 && _index <= (int)values_.size());
275 
276  values_.insert(values_.begin() + _index, T());
277 }
278 
279 //-----------------------------------------------------------------------------
280 
284 template<typename T>
286 {
287  assert(_index >= 0 && _index < (int)values_.size());
288 
289  values_.erase(values_.begin() + _index);
290 }
291 
292 //-----------------------------------------------------------------------------
293 
297 template<typename T>
299 {
300  values_.clear();
301 }
302 
303 //-----------------------------------------------------------------------------
304 
305 
void clear() override
Clear the property.
The property handle, use it to access the properties.
Definition: Properties.hh:67
std::vector< BaseProperty * > properties_
A vector holding the properties.
Definition: Properties.hh:191
T & operator[](int _index)
Direct access to the value with the given index.
bool add_property(PropertyHandleT< T > &_hProp, std::string _name)
Adds a new property.
int idx_
The properties index.
Definition: Properties.hh:81
bool isValid()
Returns true if the handle is valid, false otherwise.
A container storing a single property for all objects.
Definition: Properties.hh:127
PropertyT(unsigned long _size=0)
Creates a new property with the given size.
void insert_at(int _index) override
Insert element.
bool get_property(PropertyHandleT< T > &_hProp, std::string _name)
Initiates the property handle.
PropertyHandleT(int _idx=-1)
Constructs a new property handle.
std::map< std::string, int > property_names_
The property names, key holding the name, value the properties index in Properties::properties_.
Definition: Properties.hh:190
virtual ~PropertyT()
Destructor.
void remove_at(int _index) override
Remove element.
bool remove_property(PropertyHandleT< T > &_hProp)
Deletes a property including all values.
T & property(PropertyHandleT< T > &_hProp, int _index)
Direct access to the properties values.
unsigned long size_
The number of fields in each property, used when new properties have to be created.
Definition: Properties.hh:192
virtual ~PropertyHandleT()
Destructor.