nidas  v1.2-1520
Parameter.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2005, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 /*
27 
28  A fairly generic parameter.
29 
30 */
31 
32 #ifndef NIDAS_CORE_PARAMETER_H
33 #define NIDAS_CORE_PARAMETER_H
34 
35 #include "DOMable.h"
36 
37 #include <string>
38 #include <vector>
39 #include <sstream>
40 #include <iostream>
41 
42 namespace nidas { namespace core {
43 
44 class Dictionary;
45 
46 class Parameter
47 {
48 public:
49 
51 
52  typedef enum parType parType;
53 
54  virtual void assign(const Parameter&) = 0;
55 
56  virtual ~Parameter() {}
57 
58  virtual Parameter* clone() const = 0;
59 
60  const std::string& getName() const { return _name; }
61 
62  void setName(const std::string& val) { _name = val; }
63 
64  parType getType() const { return _type; }
65 
66  virtual int getLength() const = 0;
67 
68  virtual double getNumericValue(int i) const;
69 
70  virtual std::string getStringValue(int i) const;
71 
72  static Parameter* createParameter(const xercesc::DOMElement*, const Dictionary* d = 0)
73  throw(nidas::util::InvalidParameterException);
74 
75  virtual void fromDOMElement(const xercesc::DOMElement*, const Dictionary* dict)
76  throw(nidas::util::InvalidParameterException) = 0;
77 
78 protected:
79 
81 
82  std::string _name;
83 
85 
86 
87 };
88 
93 inline Parameter::parType getParamType(std::string)
94 {
96 }
97 
99 {
100  return Parameter::FLOAT_PARAM;
101 }
102 
104 {
105  return Parameter::INT_PARAM;
106 }
107 
109 {
110  return Parameter::BOOL_PARAM;
111 }
112 
116 template <class T>
117 class ParameterT : public Parameter {
118 public:
119 
121 
122  ParameterT* clone() const;
123 
127  void assign(const Parameter& x);
128 
129  int getLength() const { return _values.size(); }
130 
131  const std::vector<T> getValues() const { return _values; }
132 
133  void setValues(const std::vector<T>& vals) { _values = vals; }
134 
138  void setValue(unsigned int i, const T& val)
139  {
140  for (unsigned int j = _values.size(); j < i; j++) _values.push_back(T());
141  if (_values.size() > i) _values[i] = val;
142  else _values.push_back(val);
143  }
144 
148  void setValue(const T& val) {
149  _values.clear();
150  _values.push_back(val);
151  }
152 
153  T getValue(int i) const { return _values[i]; }
154 
155  void fromDOMElement(const xercesc::DOMElement*)
156  throw(nidas::util::InvalidParameterException);
157 
158  void fromDOMElement(const xercesc::DOMElement*, const Dictionary* dict)
159  throw(nidas::util::InvalidParameterException);
160 
161 protected:
162 
166  std::vector<T> _values;
167 
168 };
169 
175 public:
176  ParameterNameTypeComparator(const Parameter* param):p(param) {}
177  bool operator()(const Parameter* x) const {
178  return x->getName() == p->getName() &&
179  x->getType() == p->getType();
180  }
181 private:
182  const Parameter* p;
183 };
184 
185 }} // namespace nidas namespace core
186 
187 #endif
int getLength() const
Definition: Parameter.h:129
Interface for a Dictionary class, which can return a string value for a string token name...
Definition: Dictionary.h:38
Definition: Parameter.h:50
void fromDOMElement(const xercesc::DOMElement *)
Definition: Parameter.cc:136
virtual std::string getStringValue(int i) const
Definition: Parameter.cc:57
const std::vector< T > getValues() const
Definition: Parameter.h:131
void setValues(const std::vector< T > &vals)
Definition: Parameter.h:133
virtual int getLength() const =0
Parameter::parType getParamType(std::string)
Overloaded function to return a enumerated value corresponding to the type pointed to by the argument...
Definition: Parameter.h:93
bool operator()(const Parameter *x) const
Definition: Parameter.h:177
const Parameter * p
Definition: Parameter.h:182
parType getType() const
Definition: Parameter.h:64
virtual double getNumericValue(int i) const
Definition: Parameter.cc:39
ParameterT()
Definition: Parameter.h:120
virtual ~Parameter()
Definition: Parameter.h:56
void setValue(const T &val)
For parameters of length one, set its value.
Definition: Parameter.h:148
parType _type
Definition: Parameter.h:84
void assign(const Parameter &x)
A virtual assignment operator.
Definition: Parameter.cc:125
ParameterNameTypeComparator(const Parameter *param)
Definition: Parameter.h:176
ParameterT * clone() const
Definition: Parameter.cc:119
A typed Parameter, with data of type T.
Definition: Parameter.h:117
Definition: Parameter.h:50
Definition: Parameter.h:46
Definition: Parameter.h:50
void setName(const std::string &val)
Definition: Parameter.h:62
virtual void assign(const Parameter &)=0
std::string _name
Definition: Parameter.h:82
virtual Parameter * clone() const =0
T getValue(int i) const
Definition: Parameter.h:153
static Parameter * createParameter(const xercesc::DOMElement *, const Dictionary *d=0)
Definition: Parameter.cc:78
parType
Definition: Parameter.h:50
virtual void fromDOMElement(const xercesc::DOMElement *, const Dictionary *dict)=0
std::vector< T > _values
Vector of values.
Definition: Parameter.h:166
const std::string & getName() const
Definition: Parameter.h:60
Definition: Parameter.h:50
Functor class for Parameter, doing an equality check of parameter name and type.
Definition: Parameter.h:174
void setValue(unsigned int i, const T &val)
Set ith value.
Definition: Parameter.h:138