All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CompositeProperty.cpp
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.cpp
22 
23  \brief A base class for a compound property type (non-atomic properties).
24 */
25 
26 // TerraLib
27 #include "../common/STLUtils.h"
28 #include "CompositeProperty.h"
29 #include "Enums.h"
30 
31 // STL
32 #include <cassert>
33 
34 te::dt::CompositeProperty::CompositeProperty(const std::string& cname, const std::string& name, unsigned int id, Property* parent)
35  : Property(name, COMPOSITE_TYPE, id, parent),
36  m_cname(cname)
37 {
38 }
39 
41  : Property(rhs),
42  m_cname(rhs.m_cname)
43 {
44  copy(rhs.m_properties);
45 }
46 
48 {
49  te::common::FreeContents(m_properties);
50 }
51 
53 {
54  if(this != &rhs)
55  {
57 
58  m_cname = rhs.m_cname;
59 
60  te::common::FreeContents(m_properties);
61 
62  m_properties.clear();
63 
64  copy(rhs.m_properties);
65  }
66 
67  return *this;
68 }
69 
71 {
72  assert(p && p->getParent() == 0);
73  m_properties.push_back(p);
74  p->setParent(this);
75 }
76 
77 void te::dt::CompositeProperty::add(const std::vector<Property*>& ps)
78 {
79  std::size_t size = ps.size();
80 
81  for(std::size_t i = 0; i < size; ++i)
82  {
83  assert(ps[i]->getParent() == 0);
84 
85  m_properties.push_back(ps[i]);
86 
87  ps[i]->setParent(this);
88  }
89 }
90 
91 void te::dt::CompositeProperty::add(const boost::ptr_vector<te::dt::Property>& ps)
92 {
93  std::size_t size = ps.size();
94 
95  for(std::size_t i = 0; i < size; ++i)
96  {
97  te::dt::Property* p = ps[i].clone();
98 
99  m_properties.push_back(p);
100 
101  p->setParent(this);
102  }
103 }
104 
106 {
107  std::size_t size = m_properties.size();
108 
109  for(std::size_t i = 0; i < size; ++i)
110  {
111  if(p == m_properties[i])
112  {
113  delete p;
114  m_properties.erase(m_properties.begin() + i);
115  break;
116  }
117  }
118 }
119 
121 {
122  const std::size_t size = m_properties.size();
123 
124  for(std::size_t i = 0; i < size; ++i)
125  if(m_properties[i]->getName() == name)
126  return m_properties[i];
127 
128  return 0;
129 }
130 
131 std::size_t te::dt::CompositeProperty::getPropertyPosition(const std::string& name) const
132 {
133  const std::size_t size = m_properties.size();
134 
135  for(std::size_t i = 0; i < size; ++i)
136  if(m_properties[i]->getName() == name)
137  return i;
138 
139  return std::string::npos;
140 }
141 
143 {
144  const std::size_t size = m_properties.size();
145 
146  for(std::size_t i = 0; i < size; ++i)
147  if(m_properties[i] == p)
148  return i;
149 
150  return std::string::npos;
151 }
152 
154 {
155  const std::size_t size = m_properties.size();
156 
157  for(std::size_t i = 0; i < size; ++i)
158  if(m_properties[i]->getId() == id)
159  return m_properties[i];
160 
161  return 0;
162 }
163 
164 void te::dt::CompositeProperty::copy(const std::vector<Property*>& ps)
165 {
166  std::size_t size = ps.size();
167 
168  m_properties.resize(m_properties.size() + size);
169 
170  for(std::size_t i = 0; i < size; ++i)
171  {
172  Property* p = ps[i]->clone();
173 
174  m_properties[i] = p;
175 
176  p->setParent(this);
177  }
178 }
179 
181 {
182  te::common::FreeContents(m_properties);
183  m_properties.clear();
184 }
185 
187 {
188  size_t size = m_properties.size();
189 
190  for(size_t i = 0; i < size; ++i)
191  {
192  if(m_properties[i] == p)
193  return true;
194 
195  if(m_properties[i]->has(p))
196  return true;
197  }
198 
199  return false;
200 }
201 
203 {
204  return new CompositeProperty(*this);
205 }
206 
207 te::dt::CompositeProperty::CompositeProperty(const std::string& cname, const std::string& name, int t, unsigned int id, Property* parent)
208  : Property(name, t, id, parent),
209  m_cname(cname)
210 {
211 }
212 
213 
Property * getProperty(std::size_t i) const
It returns the i-th property.
A base class for a compound property (non-atomic properties).
virtual void clear()
It clears the CompositeProperty definition.
bool has(Property *p) const
It checks if the Property "p" is associated to this property or any other parent. ...
A base class for a compound properties (non-atomic properties).
virtual Property * clone() const
It returns a clone of the object.
CompositeProperty(const std::string &cname, const std::string &name, unsigned int id=0, Property *parent=0)
It creates a new CompositeProperty.
virtual Property * clone() const =0
It returns a clone of the object.
Property & operator=(const Property &rhs)
Assignment operator.
Definition: Property.cpp:51
std::vector< Property * > m_properties
The list of property types that make the CompositeProperty.
It models a property definition.
Definition: Property.h:59
virtual ~CompositeProperty()
Virtual destructor.
CompositeProperty & operator=(const CompositeProperty &rhs)
Assignment operator.
Property * getPropertyById(unsigned int id) const
It searches for a property with the given ID.
Property * getParent() const
It returns the parent of this property, or NULL, if it doesn't have one.
Definition: Property.h:168
void copy(const std::vector< Property * > &ps)
It copies the properties from the vector.
virtual void remove(Property *p)
It removes the property from the composite.
General enumerations for the data type module.
std::size_t getPropertyPosition(const std::string &name) const
It returns the property position based on its name.
void add(Property *p)
It adds a new property to the CompositeProperty.
std::string m_cname
The composite type name.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177