CompositeProperty.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/datatype/CompositeProperty.h
22 
23  \brief A base class for a compound properties (non-atomic properties).
24 */
25 
26 #ifndef __TERRALIB_DATATYPE_INTERNAL_COMPOSITEPROPERTY_H
27 #define __TERRALIB_DATATYPE_INTERNAL_COMPOSITEPROPERTY_H
28 
29 // TerraLib
30 #include "Property.h"
31 
32 // STL
33 #include <vector>
34 
35 // Boost
36 #include <boost/ptr_container/ptr_vector.hpp>
37 
38 namespace te
39 {
40  namespace dt
41  {
42  /*!
43  \class CompositeProperty
44 
45  \brief A base class for a compound property (non-atomic properties).
46 
47  \ingroup datatype
48 
49  \sa Property, SimpleProperty, NumericProperty, StringProperty, ArrayProperty, DateTimeProperty
50  */
52  {
53  public:
54 
55  /*!
56  \brief It creates a new CompositeProperty.
57 
58  \param cname The composite type name.
59  \param name The property name.
60  \param id The property identifier.
61  \param parent A reference to the parent Property of the new object if it has one.
62 
63  \warning The identifier value (id) may be used by data source implementations. So, don't rely on its value!
64  */
65  CompositeProperty(const std::string& cname,
66  const std::string& name,
67  unsigned int id = 0,
68  Property* parent = 0);
69 
70  /*!
71  \brief Copy constructor.
72 
73  \param rhs The right-hand-side copy used to copy from.
74  */
76 
77  /*! \brief Virtual destructor. */
78  virtual ~CompositeProperty();
79 
80  /*!
81  \brief Assignment operator.
82 
83  \param rhs The right-hand-side copy used to copy from.
84 
85  \return A reference to this object.
86  */
87  CompositeProperty& operator=(const CompositeProperty& rhs);
88 
89  /*!
90  \brief It returns the name of the composite type.
91 
92  \return The name of the composite type.
93 
94  \note This name may be different from the property name (returned by getName method)!
95  */
96  const std::string& getCompositeName() const { return m_cname; }
97 
98  /*!
99  \brief It sets the composite type name.
100 
101  \param name The composite type name.
102  */
103  void setCompositeName(const std::string& cname) { m_cname = cname; }
104 
105  /*!
106  \brief It adds a new property to the CompositeProperty.
107 
108  \param p The property to be inserted.
109 
110  \pre The Property p must be valid.
111  \pre The Property p must not be associated to another property.
112 
113  \post The property p will be associated to CompositeProperty.
114  \post The CompositeProperty takes the ownership of the property.
115  */
116  void add(Property* p);
117 
118  /*!
119  \brief It adds a list of property types to the CompositeProperty.
120 
121  \param ps The list of properties to be added.
122 
123  \pre The properties must be valid.
124  \pre The properties must not be associated to another property.
125 
126  \note The CompositeProperty takes the ownership of the properties.
127  */
128  void add(const std::vector<Property*>& ps);
129 
130  void add(const boost::ptr_vector<te::dt::Property>& ps);
131 
132  /*!
133  \brief It removes the property from the composite.
134 
135  \param p The property to be removed from the composite.
136 
137  \post The property pointer will be invalidated.
138  */
139  virtual void remove(Property* p);
140 
141  /*!
142  \brief It returns the number of properties of the CompositeProperty.
143 
144  \return The number of properties of the CompositeProperty.
145  */
146  std::size_t size() const { return m_properties.size(); }
147 
148  /*!
149  \brief It returns the list of properties describing the CompositeProperty.
150 
151  \return The list of Propertys describing the CompositeProperty.
152  */
153  const std::vector<Property*>& getProperties() const { return m_properties; }
154 
155  /*!
156  \brief It returns the list of properties describing the CompositeProperty.
157 
158  \return The list of Propertys describing the CompositeProperty.
159  */
160  std::vector<Property*>& getProperties() { return m_properties; }
161 
162  /*!
163  \brief It returns the i-th property.
164 
165  \param i The property position in the property array.
166 
167  \return A pointer to the i-th property. Don't delete it, as it belongs to the CompositeProperty.
168 
169  \pre i must be in the valid range [0, size()).
170  */
171  Property* getProperty(std::size_t i) const { return m_properties[i]; }
172 
173  /*!
174  \brief It returns the property with the given name or NULL if none is found.
175 
176  \param name The name of the property we are looking for.
177 
178  \return A pointer to a property or NULL if none is found. Don't delete it, as it belongs to the CompositeProperty.
179  */
180  Property* getProperty(const std::string& name) const;
181 
182  /*!
183  \brief It returns the property position based on its name.
184 
185  \param name The property name.
186 
187  \return A property position in the property array.
188  */
189  std::size_t getPropertyPosition(const std::string& name) const;
190 
191  /*!
192  \brief It returns the property position .
193 
194  \param p The property.
195 
196  \return A property position in the property array.
197  */
198  std::size_t getPropertyPosition(const Property* p) const;
199 
200  /*!
201  \brief It searches for a property with the given ID.
202 
203  \param id The property ID.
204 
205  \return A pointer to the first property with the given ID if found or NULL otherwise. Don't delete it, as it belongs to the CompositeProperty.
206  */
207  Property* getPropertyById(unsigned int id) const;
208 
209  /*!
210  \brief Tells if there is a property of the given data type.
211  */
212  bool hasPropertyOfType(const int t) const;
213 
214  /*!
215  \brief returns the first property of the given data type. Caller doesn't take ownership of the returned pointer.
216  */
217  Property* findFirstPropertyOfType(const int t) const;
218 
219  /*!
220  \brief It copies the properties from the vector.
221 
222  \param ps The list of properties to be copied.
223 
224  \note The CompositeProperty will not take the ownership of the properties, it will clone each one.
225  So, it is the caller responsability to release them.
226  */
227  void copy(const std::vector<Property*>& ps);
228 
229  /*! \brief It clears the CompositeProperty definition. */
230  virtual void clear();
231 
232  /*!
233  \brief It checks if the Property "p" is associated to this property or any other parent.
234 
235  \param p The Property we are checking.
236 
237  \return True if the given Property "p" is associated to to this Property, otherwise, returns false.
238  */
239  bool has(Property* p) const;
240 
241  /*!
242  \brief It returns a clone of the object.
243 
244  The new property will NOT have associations to other elements.
245 
246  \return A clone of the object.
247  */
248  virtual Property* clone() const;
249 
250  protected:
251 
252  /*!
253  \brief It creates a new Property.
254 
255  \param cname The composite type name.
256  \param name The Property name.
257  \param t The data type id.
258  \param id The property identifier.
259  \param parent A reference to the parent Property of the new object if it has one.
260  */
261  CompositeProperty(const std::string& cname,
262  const std::string& name,
263  int t,
264  unsigned int id,
265  Property* parent);
266 
267  protected:
268 
269  std::string m_cname; //!< The composite type name.
270  std::vector<Property*> m_properties; //!< The list of property types that make the CompositeProperty.
271  };
272 
273  inline bool CompositeProperty::hasPropertyOfType(const int t) const
274  {
275  return findFirstPropertyOfType(t) != 0;
276  }
277 
279  {
280  const std::size_t size = m_properties.size();
281 
282  for(std::size_t i = 0; i != size; ++i)
283  if(m_properties[i]->getType() == t)
284  return m_properties[i];
285 
286  return 0;
287  }
288 
289  } // end namespace dt
290 } // end namespace te
291 
292 
293 #endif // __TERRALIB_DATATYPE_INTERNAL_COMPOSITEPROPERTY_H
294 
295 
Property * getProperty(std::size_t i) const
It returns the i-th property.
A base class for a compound property (non-atomic properties).
It models a property definition.
const std::string & getCompositeName() const
It returns the name of the composite type.
std::vector< Property * > m_properties
The list of property types that make the CompositeProperty.
It models a property definition.
Definition: Property.h:59
#define TEDATATYPEEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:61
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
URI C++ Library.
std::size_t size() const
It returns the number of properties of the CompositeProperty.
std::vector< Property * > & getProperties()
It returns the list of properties describing the CompositeProperty.
int getType() const
It returns the property data type.
Definition: Property.h:161
Property * findFirstPropertyOfType(const int t) const
returns the first property of the given data type. Caller doesn't take ownership of the returned poin...
bool hasPropertyOfType(const int t) const
Tells if there is a property of the given data type.
void setCompositeName(const std::string &cname)
It sets the composite type name.
std::string m_cname
The composite type name.