Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PropertiesT.cc
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #define PROPERTIES_C
51 
52 #include <cassert>
53 #include <utility>
54 #include <iostream>
55 
56 
57 //-----------------------------------------------------------------------------
58 
71 template<typename T>
72 bool Properties::add_property(PropertyHandleT<T> &_hProp, std::string _name)
73 {
74  // return the property if it exists
75  if(get_property(_hProp, _name))
76  return false;
77 
78  // create it if not
79  // find the first free entry
80  std::vector<BaseProperty*>::iterator it;
81  for(it = properties_.begin(); it != properties_.end(); ++it)
82  if(*it == 0)
83  break;
84 
85  if(it == properties_.end()) // found a free entry?
86  {
87  // append at the end
88  _hProp.idx_ = properties_.size();
89  property_names_.insert( std::pair<std::string, int>(_name, properties_.size()) );
90  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
91  }else{
92  // insert in the free spot
93  _hProp.idx_ = it - properties_.begin();
94  property_names_.insert( std::pair<std::string, int>(_name, _hProp.idx_) );
95  *it = new PropertyT<T>(size_);
96  }
97  return true;
98 }
99 
100 //-----------------------------------------------------------------------------
101 
112 template<typename T>
113 bool Properties::get_property(PropertyHandleT<T> &_hProp, std::string _name)
114 {
115  // find the name
116  std::map<std::string, int>::iterator f = property_names_.find(_name);
117  if(f == property_names_.end())
118  return false;
119 
120  // return the index
121  _hProp.idx_ = f->second;
122  assert(properties_[_hProp.idx_] != 0);
123  return true;
124 }
125 
126 //-----------------------------------------------------------------------------
127 
136 template<typename T>
138 {
139  if(!_hProp.isValid())
140  return false;
141  assert(_hProp.idx_ < (int)properties_.size());
142  assert(properties_[_hProp.idx_] != 0);
143 
144  // delete the property
145  delete properties_[_hProp.idx_];
146  properties_[_hProp.idx_] = 0;
147 
148  // find the names entry and delete it
149  for(std::map<std::string, int>::iterator it = property_names_.begin(); it != property_names_.end(); ++it)
150  {
151  if(it->second == _hProp.idx_)
152  {
153  property_names_.erase(it);
154  break;
155  }
156  }
157 
158  // invalidate the handle
159  _hProp.idx_ = -1;
160 
161  return true;
162 }
163 
164 //-----------------------------------------------------------------------------
165 
177 template<typename T>
179 {
180  assert(_hProp.idx_ >= 0 && _hProp.idx_ < (int)properties_.size());
181  assert(properties_[_hProp.idx_] != 0);
182  assert(reinterpret_cast< PropertyT<T>* >(properties_[_hProp.idx_]) != 0);
183 
184  return (*reinterpret_cast< PropertyT<T>* >(properties_[_hProp.idx_]))[_index];
185 }
186 
187 //-----------------------------------------------------------------------------
188 // PropertyHandleT
189 //-----------------------------------------------------------------------------
190 
199 template<typename T>
201  idx_(_idx)
202 {
203 }
204 
205 //-----------------------------------------------------------------------------
206 
210 template<typename T>
212 {
213 }
214 
215 //-----------------------------------------------------------------------------
216 
223 template<typename T>
225 {
226  return idx_ >= 0;
227 }
228 
229 //-----------------------------------------------------------------------------
230 // PropertyT
231 //-----------------------------------------------------------------------------
232 
240 template<typename T>
242 {
243  for(unsigned long i = 0; i < _size; ++i)
244  values_.push_back(T());
245 }
246 
247 //-----------------------------------------------------------------------------
248 
252 template<typename T>
254 {
255 }
256 
257 //-----------------------------------------------------------------------------
258 
264 template<typename T>
266 {
267  assert(_index >= 0 && _index < (int)values_.size());
268 
269  return values_[_index];
270 }
271 
272 //-----------------------------------------------------------------------------
273 
277 template<typename T>
279 {
280  assert(_index >= 0 && _index <= (int)values_.size());
281 
282  values_.insert(values_.begin() + _index, T());
283 }
284 
285 //-----------------------------------------------------------------------------
286 
290 template<typename T>
292 {
293  assert(_index >= 0 && _index < (int)values_.size());
294 
295  values_.erase(values_.begin() + _index);
296 }
297 
298 //-----------------------------------------------------------------------------
299 
303 template<typename T>
305 {
306  values_.clear();
307 }
308 
309 //-----------------------------------------------------------------------------
310 
311 
T & operator[](int _index)
Direct access to the value with the given index.
Definition: PropertiesT.cc:265
std::vector< BaseProperty * > properties_
A vector holding the properties.
Definition: Properties.hh:199
bool get_property(PropertyHandleT< T > &_hProp, std::string _name)
Initiates the property handle.
Definition: PropertiesT.cc:113
bool isValid()
Returns true if the handle is valid, false otherwise.
Definition: PropertiesT.cc:224
unsigned long size_
The number of fields in each property, used when new properties have to be created.
Definition: Properties.hh:200
PropertyHandleT(int _idx=-1)
Constructs a new property handle.
Definition: PropertiesT.cc:200
void insert_at(int _index)
Insert element.
Definition: PropertiesT.cc:278
bool add_property(PropertyHandleT< T > &_hProp, std::string _name)
Adds a new property.
Definition: PropertiesT.cc:72
PropertyT(unsigned long _size=0)
Creates a new property with the given size.
Definition: PropertiesT.cc:241
T & property(PropertyHandleT< T > &_hProp, int _index)
Direct access to the properties values.
Definition: PropertiesT.cc:178
void clear()
Clear the property.
Definition: PropertiesT.cc:304
A container storing a single property for all objects.
Definition: Properties.hh:135
int idx_
The properties index.
Definition: Properties.hh:89
bool remove_property(PropertyHandleT< T > &_hProp)
Deletes a property including all values.
Definition: PropertiesT.cc:137
std::map< std::string, int > property_names_
The property names, key holding the name, value the properties index in Properties::properties_.
Definition: Properties.hh:198
void remove_at(int _index)
Remove element.
Definition: PropertiesT.cc:291
virtual ~PropertyT()
Destructor.
Definition: PropertiesT.cc:253
virtual ~PropertyHandleT()
Destructor.
Definition: PropertiesT.cc:211
The property handle, use it to access the properties.
Definition: Properties.hh:75