All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CompositeData.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/CompositeData.cpp
22 
23  \brief A base class for composite data values.
24 */
25 
26 // TerraLib
27 #include "../common/STLUtils.h"
28 #include "CompositeData.h"
29 
30 // STL
31 #include <cassert>
32 #include <sstream>
33 
34 te::dt::CompositeData::CompositeData(std::size_t nComponents)
35  : m_values(nComponents),
36  m_names(nComponents)
37 {
38 }
39 
40 te::dt::CompositeData::CompositeData(const std::string& name)
41  : m_name(name)
42 {
43 }
44 
46  : m_names(rhs.m_names)
47 {
49 }
50 
52 {
53  te::common::FreeContents(m_values);
54 }
55 
57 {
58  if(this != &rhs)
59  {
60  te::common::FreeContents(m_values);
61 
62  m_values.clear();
63 
64  te::common::Clone(rhs.m_values, m_values);
65 
66  m_names = rhs.m_names;
67  }
68 
69  return *this;
70 }
71 
72 void te::dt::CompositeData::setName(const std::string& name)
73 {
74  m_name = name;
75 }
76 
77 void te::dt::CompositeData::add(const std::string& name, AbstractData* value)
78 {
79  m_names.push_back(name);
80  m_values.push_back(value);
81 }
82 
84 {
85  assert(i < m_values.size());
86  return m_values[i];
87 }
88 
89 void te::dt::CompositeData::setValue(std::size_t i, AbstractData* value)
90 {
91  delete m_values[i];
92  m_values[i] = value;
93 }
94 
96 {
97  m_values.push_back(value);
98 }
99 
101 {
102  return new CompositeData(*this);
103 }
104 
106 {
107  std::stringstream ss(std::stringstream::in | std::stringstream::out);
108 
109  ss << "{\n";
110 
111  const std::size_t nComponents = m_values.size();
112 
113  for(std::size_t i = 0; i < nComponents; ++i)
114  {
115  if(i != 0)
116  ss << ",\n";
117 
118  ss << m_values[i]->toString();
119  }
120 
121  ss << "\n}";
122 
123  return ss.str();
124 }
125 
std::vector< AbstractData * > m_values
The component values.
AbstractData * clone() const
It creates a new clone of the composite.
A base class for composite data values.
~CompositeData()
Destructor.
std::vector< std::string > m_names
The component names.
void add(const std::string &name, AbstractData *value)
CompositeData()
Constructor.
Definition: CompositeData.h:54
AbstractData * getValue(std::size_t i) const
It returns the i-th component value of the composite data.
std::string toString() const
It returns the data value in a string representation.
CompositeData & operator=(const CompositeData &rhs)
Copy constructor.
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
A base class for composite data values.
Definition: CompositeData.h:49
void setName(const std::string &name)
void setValue(std::size_t i, AbstractData *value)
It sets the i-th component value of the composite data.
void Clone(const std::vector< T * > &src, std::vector< T * > &dst)
This function can be applied to a vector of pointers.
Definition: STLUtils.h:237
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