AlgorithmParametersSerializer.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 #ifndef __TERRALIB_RP_INTERNAL_ALGORITHM_PARAMETERS_SERIALIZER_H
21 #define __TERRALIB_RP_INTERNAL_ALGORITHM_PARAMETERS_SERIALIZER_H
22 
23 #include "Config.h"
24 
25 #include<string>
26 #include<map>
27 #include<vector>
28 #include<complex>
29 
30 namespace te
31 {
32  namespace rp
33  {
34  /*!
35  \class AlgorithmParametersSerializer
36  \brief A class to standardize algorithm parameters serialization.
37  */
39  {
40  public:
41 
43 
45 
47 
49  const AlgorithmParametersSerializer& other );
50 
51  /*!
52  \brief Set a non-empty algorithm name.
53  \param The new non-empty algorithm name.
54  */
55  void setAlgorithmName( const std::string& name );
56 
57  /*!
58  \brief Add a new parameter.
59  \param name A new non-empty parameter name.
60  \param value A new parameter value.
61  \return true if ok, false on errors.
62  */
63  bool addParameter( const std::string& name, const std::string& value );
64 
65  /*!
66  \brief Add a new parameter.
67  \param name A new non-empty parameter name.
68  \param value A new parameter value.
69  \return true if ok, false on errors.
70  */
71  template< typename ValueType >
72  bool addParameter( const std::string& name, const ValueType& value )
73  {
74  try
75  {
76  return addParameter( name, toString( value ) );
77  }
78  catch(...)
79  {
80  return false;
81  }
82  }
83 
84  /*!
85  \brief Add a new multivalued parameter.
86  \param name A new non-empty parameter name.
87  \param value A new parameter values.
88  \return true if ok, false on errors.
89  */
90  bool addMultivaluedParameter( const std::string& name,
91  const std::vector< std::string >& values );
92 
93  /*!
94  \brief Add a new multivalued parameter.
95  \param name A new non-empty parameter name.
96  \param value A new parameter values.
97  \return true if ok, false on errors.
98  */
99  template< typename ValueType >
100  bool addMultivaluedParameter( const std::string& name,
101  const std::vector< ValueType >& values )
102  {
103  const std::size_t valuesSize = values.size();
104  std::vector< std::string > stringValues;
105 
106  try
107  {
108  for( std::size_t valuesIdx = 0 ; valuesIdx < valuesSize ; ++valuesIdx )
109  {
110  stringValues.push_back( toString( values[ valuesIdx ] ) );
111  }
112  }
113  catch(...)
114  {
115  return false;
116  }
117 
118  return addMultivaluedParameter( name, stringValues );
119  }
120 
121  /*!
122  \brief Add sets of new multivalued parameters.
123  \param name A new non-empty parameter name.
124  \param values Parameter values.
125  \return true if ok, false on errors.
126  */
127  template< typename ValueType >
128  bool addMultivaluedParameters( const std::string& name,
129  const std::vector< std::vector< ValueType > >& values )
130  {
131  const std::size_t valuesSize = values.size();
132 
133  for( std::size_t valuesIdx = 0 ; valuesIdx < valuesSize ; ++valuesIdx )
134  {
135  if( ! addMultivaluedParameter( name, values[ valuesIdx ] ) )
136  {
137  return false;
138  }
139  }
140 
141  return true;
142  }
143 
144  /*!
145  \brief Returns all serialized parameters formatted for raster metadata.
146  \param metadata The output metadata.
147  */
148  void getMetaData( std::map<std::string, std::string>& metadata ) const;
149 
150  /*!
151  \brief Reset to the initial empty state.
152  */
153  void reset();
154 
155  protected :
156 
157  typedef std::pair< std::string, std::vector< std::string > > ParsContPairT;
158 
159  typedef std::multimap<std::string, std::vector< std::string > > ParsContT;
160 
161  template< typename ValueType >
162  std::string toString( const std::complex< ValueType >& value ) const
163  {
164  return std::to_string( value.real() ) + "+" +
165  std::to_string( value.imag() ) + "i";
166  }
167 
168  template< typename ValueType >
169  std::string toString( const ValueType& value ) const
170  {
171  return std::to_string( value );
172  }
173 
174  private :
175 
176  std::string m_algoName;
177 
179 
180  };
181  } // end namespace rp
182 } // end namespace te
183 
184 #endif // __TERRALIB_RP_INTERNAL_ALGORITHM_PARAMETERS_SERIALIZER_H
185 
A class to standardize algorithm parameters serialization.
std::pair< std::string, std::vector< std::string > > ParsContPairT
bool addMultivaluedParameter(const std::string &name, const std::vector< std::string > &values)
Add a new multivalued parameter.
AlgorithmParametersSerializer(const AlgorithmParametersSerializer &other)
AlgorithmParametersSerializer & operator=(const AlgorithmParametersSerializer &other)
std::string toString(const ValueType &value) const
void getMetaData(std::map< std::string, std::string > &metadata) const
Returns all serialized parameters formatted for raster metadata.
bool addParameter(const std::string &name, const ValueType &value)
Add a new parameter.
std::string toString(const std::complex< ValueType > &value) const
std::multimap< std::string, std::vector< std::string > > ParsContT
void reset()
Reset to the initial empty state.
bool addParameter(const std::string &name, const std::string &value)
Add a new parameter.
bool addMultivaluedParameters(const std::string &name, const std::vector< std::vector< ValueType > > &values)
Add sets of new multivalued parameters.
bool addMultivaluedParameter(const std::string &name, const std::vector< ValueType > &values)
Add a new multivalued parameter.
void setAlgorithmName(const std::string &name)
Set a non-empty algorithm name.
TerraLib.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
Proxy configuration file for TerraView (see terraview_config.h).