Developer Documentation
Properties.hh
Go to the documentation of this file.
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 #ifndef PROPERTIES_HH
43 #define PROPERTIES_HH
44 
54 #include <vector>
55 #include <map>
56 #include <string>
57 
59 
60 
66 template<typename T>
68 {
69  friend class Properties;
70 
71 protected:
72  typedef T value_type;
73 
74 public:
75  explicit PropertyHandleT(int _idx = -1);
76  virtual ~PropertyHandleT();
77 
78  inline bool isValid();
79 
80 protected:
81  int idx_;
82 };
83 
92 class OBJECTTYPEDLLEXPORT Properties
93 {
94 protected:
101  class OBJECTTYPEDLLEXPORT BaseProperty
102  {
103  friend class Properties;
104  public:
105  BaseProperty();
106  virtual ~BaseProperty();
107 
108  protected:
114  virtual void insert_at(int _index) = 0;
115  virtual void remove_at(int _index) = 0;
116  virtual void clear() = 0;
117 
118  };
119 
126  template<typename T>
127  class PropertyT : public BaseProperty
128  {
129  friend class Properties;
130 
131  public:
132  explicit PropertyT(unsigned long _size = 0);
133  virtual ~PropertyT();
134 
135  public:
136  inline T &operator[](int _index);
137 
138  protected:
144  void insert_at(int _index) override;
145  void remove_at(int _index) override;
146  void clear() override;
148 
149  protected:
150  std::vector<T> values_;
151  };
152 
153 public:
154  Properties();
155  virtual ~Properties();
156 
157 public:
163  template<typename T> bool add_property(PropertyHandleT<T> &_hProp, std::string _name);
164  template<typename T> bool get_property(PropertyHandleT<T> &_hProp, std::string _name);
165  bool has_property(std::string _name);
166  template<typename T> bool remove_property(PropertyHandleT<T> &_hProp);
167  void clear_properties();
169 
175  template<typename T> T &property(PropertyHandleT<T> &_hProp, int _index);
177 
178 protected:
184  void insert_property_at(int _index);
185  void remove_property_at(int _index);
186  void clean_properties();
188 
189 protected:
190  std::map<std::string, int> property_names_;
191  std::vector<BaseProperty*> properties_;
192  unsigned long size_;
193 };
194 
205 //=============================================================================
206 //=============================================================================
207 #if defined(INCLUDE_TEMPLATES) && !defined(PROPERTIES_C)
208 #define PROPERTIES_TEMPLATES
209 #include "PropertiesT_impl.hh"
210 #endif
211 //=============================================================================
212 #endif // PROPERTIES_HH defined
213 //=============================================================================
214 
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
std::vector< T > values_
Stores a property for every object in the class derived from the Properties class. Access by index.
Definition: Properties.hh:150
bool add_property(PropertyHandleT< T > &_hProp, std::string _name)
Adds a new property.
int idx_
The properties index.
Definition: Properties.hh:81
The properties storage class.
Definition: Properties.hh:92
bool isValid()
Returns true if the handle is valid, false otherwise.
The base class for all property template instances.
Definition: Properties.hh:101
void remove_property_at(int _index)
Removes a property for an object that is being deleted from the derived class.
Definition: Properties.cc:122
A container storing a single property for all objects.
Definition: Properties.hh:127
void clear_properties()
Deletes all properties, including their values.
Definition: Properties.cc:85
void insert_property_at(int _index)
Inserts a property for a new object at the given index.
Definition: Properties.cc:105
bool get_property(PropertyHandleT< T > &_hProp, std::string _name)
Initiates the 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
PropertyHandleT(int _idx=-1)
Constructs a new property handle.
bool has_property(std::string _name)
Returns true if a property with the given name exists.
Definition: Properties.cc:137
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
void clean_properties()
While preserving the properties themself, all values in the property are deleted. ...
Definition: Properties.cc:71
virtual ~PropertyHandleT()
Destructor.