OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Public Types | Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
ossim::JsonParam Class Reference

Represents a single configuration parameter. More...

#include <JsonConfig.h>

Public Types

enum  ParamType {
  UNASSIGNED =0, BOOL =1, INT =2, UINT =3,
  FLOAT =4, STRING =5, VECTOR =6
}
 

Public Member Functions

 JsonParam ()
 
 JsonParam (const ossimString &argname, const ossimString &arglabel, const ossimString &argdescr, ParamType argparamType, void *value)
 
 JsonParam (const JsonParam &copy)
 
 ~JsonParam ()
 
bool loadJSON (const Json::Value &json_node)
 Initializes from a JSON node. More...
 
void saveJSON (Json::Value &json_node) const
 
const ossimStringname () const
 
const ossimStringlabel () const
 
const ossimStringdescr () const
 
ParamType type () const
 
bool isBool () const
 
bool asBool () const
 
bool isUint () const
 
unsigned int asUint () const
 
bool isInt () const
 
int asInt () const
 
bool isFloat () const
 
double asFloat () const
 
bool isString () const
 
std::string asString () const
 
bool isVector () const
 
void asVector (std::vector< double > &v) const
 
bool operator== (const JsonParam &p) const
 

Private Member Functions

void setValue (void *value)
 
void resetValue ()
 

Private Attributes

ossimString _name
 
ossimString _label
 
ossimString _descr
 
ParamType _type
 
void * _value
 
std::vector< ossimString_allowedValues
 

Friends

std::ostream & operator<< (std::ostream &out, const JsonParam &obj)
 Outputs JSON to output stream provided. More...
 

Detailed Description

Represents a single configuration parameter.

This class provides for packing and unpacking the parameter from JSON payload, and provides for handling all datatypes of parameters.

Definition at line 112 of file JsonConfig.h.

Member Enumeration Documentation

◆ ParamType

Enumerator
UNASSIGNED 
BOOL 
INT 
UINT 
FLOAT 
STRING 
VECTOR 

Definition at line 115 of file JsonConfig.h.

Constructor & Destructor Documentation

◆ JsonParam() [1/3]

ossim::JsonParam::JsonParam ( )
inline

Definition at line 125 of file JsonConfig.h.

◆ JsonParam() [2/3]

ossim::JsonParam::JsonParam ( const ossimString argname,
const ossimString arglabel,
const ossimString argdescr,
ParamType  argparamType,
void *  value 
)

Definition at line 22 of file JsonConfig.cpp.

References setValue().

27 : _name (argname),
28  _label (arglabel),
29  _descr (argdescr),
30  _type (argparamType),
31  _value(0)
32 {
33  setValue(value);
34 }
void setValue(void *value)
Definition: JsonConfig.cpp:47
ossimString _name
Definition: JsonConfig.h:174
ParamType _type
Definition: JsonConfig.h:177
ossimString _label
Definition: JsonConfig.h:175
ossimString _descr
Definition: JsonConfig.h:176

◆ JsonParam() [3/3]

ossim::JsonParam::JsonParam ( const JsonParam copy)

Definition at line 36 of file JsonConfig.cpp.

References _value, and setValue().

37 : _label (copy._label),
38  _name (copy._name),
39  _descr (copy._descr),
40  _type (copy._type),
41  _value (0),
42  _allowedValues (copy._allowedValues)
43 {
44  setValue(copy._value);
45 }
void setValue(void *value)
Definition: JsonConfig.cpp:47
std::vector< ossimString > _allowedValues
Definition: JsonConfig.h:179
ossimString _name
Definition: JsonConfig.h:174
ParamType _type
Definition: JsonConfig.h:177
ossimString _label
Definition: JsonConfig.h:175
ossimString _descr
Definition: JsonConfig.h:176

◆ ~JsonParam()

ossim::JsonParam::~JsonParam ( )
inline

Definition at line 135 of file JsonConfig.h.

References resetValue().

135 { resetValue(); }

Member Function Documentation

◆ asBool()

bool ossim::JsonParam::asBool ( ) const

◆ asFloat()

double ossim::JsonParam::asFloat ( ) const

◆ asInt()

int ossim::JsonParam::asInt ( ) const

Definition at line 271 of file JsonConfig.cpp.

References _type, _value, INT, and UINT.

Referenced by ATP::AtpAnnotatedImage::AtpAnnotatedImage().

272 {
273  if ((_type == INT) || (_type == UINT))
274  return *(int*)_value;
275  return 0;
276 }
ParamType _type
Definition: JsonConfig.h:177

◆ asString()

std::string ossim::JsonParam::asString ( ) const

Definition at line 285 of file JsonConfig.cpp.

References _type, _value, and STRING.

Referenced by ATP::AtpGenerator::constructChain(), ATP::ossimDescriptorSource::getTile(), ATP::AtpGenerator::initialize(), and ATP::ossimAtpTool::loadJSON().

286 {
287  if (_type == STRING)
288  return *(string*)_value;
289  return "";
290 }
ParamType _type
Definition: JsonConfig.h:177

◆ asUint()

unsigned int ossim::JsonParam::asUint ( ) const

◆ asVector()

void ossim::JsonParam::asVector ( std::vector< double > &  v) const

Definition at line 292 of file JsonConfig.cpp.

References _type, _value, and VECTOR.

293 {
294  v.clear();
295  if (_type == VECTOR)
296  v = *(vector<double>*)_value;
297 }
ParamType _type
Definition: JsonConfig.h:177

◆ descr()

const ossimString& ossim::JsonParam::descr ( ) const
inline

Definition at line 144 of file JsonConfig.h.

References _descr.

Referenced by ossim::JsonConfig::loadJSON().

144 { return _descr; }
ossimString _descr
Definition: JsonConfig.h:176

◆ isBool()

bool ossim::JsonParam::isBool ( ) const
inline

Definition at line 147 of file JsonConfig.h.

References _type, and BOOL.

147 {return (_type == BOOL); }
ParamType _type
Definition: JsonConfig.h:177

◆ isFloat()

bool ossim::JsonParam::isFloat ( ) const
inline

Definition at line 156 of file JsonConfig.h.

References _type, and FLOAT.

156 {return (_type == FLOAT);}
ParamType _type
Definition: JsonConfig.h:177

◆ isInt()

bool ossim::JsonParam::isInt ( ) const
inline

Definition at line 153 of file JsonConfig.h.

References _type, and INT.

153 {return (_type == INT);}
ParamType _type
Definition: JsonConfig.h:177

◆ isString()

bool ossim::JsonParam::isString ( ) const
inline

Definition at line 159 of file JsonConfig.h.

References _type, and STRING.

159 {return (_type == STRING);}
ParamType _type
Definition: JsonConfig.h:177

◆ isUint()

bool ossim::JsonParam::isUint ( ) const
inline

Definition at line 150 of file JsonConfig.h.

References _type, and UINT.

150 {return (_type == UINT); }
ParamType _type
Definition: JsonConfig.h:177

◆ isVector()

bool ossim::JsonParam::isVector ( ) const
inline

Definition at line 162 of file JsonConfig.h.

References _type, and VECTOR.

162 {return (_type == VECTOR);}
ParamType _type
Definition: JsonConfig.h:177

◆ label()

const ossimString& ossim::JsonParam::label ( ) const
inline

Definition at line 143 of file JsonConfig.h.

References _label.

143 { return _label; }
ossimString _label
Definition: JsonConfig.h:175

◆ loadJSON()

bool ossim::JsonParam::loadJSON ( const Json::Value &  json_node)

Initializes from a JSON node.

Return true if successful

Definition at line 111 of file JsonConfig.cpp.

References _allowedValues, _descr, _label, _name, _type, BOOL, ossimString::empty(), FLOAT, INT, n, ossimNotify(), ossimNotifyLevel_WARN, setValue(), STRING, UINT, ossimString::upcase(), and VECTOR.

Referenced by ossim::JsonConfig::loadJSON().

112 {
113  try
114  {
115  _name = paramNode["name"].asString();
116  _label = paramNode["label"].asString();
117  _descr = paramNode["descr"].asString();
118  Json::Value value = paramNode["value"];
119  Json::Value allowedValues = paramNode["allowedValues"];
120 
121  ossimString ptype = paramNode["type"].asString();
122  if (ptype.empty() || _name.empty())
123  return false;
124 
125  ptype.upcase();
126 
127  if (ptype == "VECTOR")
128  {
130  vector<double> v;
131  if (value.isArray())
132  {
133  int n = value.size();
134  for (unsigned int j=0; j<n; ++j)
135  v.push_back(value[j].asDouble());
136  }
137  setValue(&v);
138  }
139  else
140  {
141  // Screen for param value list as found in the default config JSONs. Pick the first element
142  // as the default:
143  if (value.isArray())
144  value = value[0];
145 
146  if (ptype == "BOOL")
147  {
149  bool v = value.asBool();
150  setValue(&v);
151  }
152  else if (ptype == "UINT")
153  {
155  unsigned int v = value.asUInt();
156  setValue(&v);
157  }
158  else if (ptype == "INT")
159  {
161  int v = value.asInt();
162  setValue(&v);
163  }
164  else if (ptype == "FLOAT")
165  {
167  double v = value.asDouble();
168  setValue(&v);
169  }
170  else if (ptype == "STRING")
171  {
173  string v = value.asString();
174  setValue(&v);
175  if (!allowedValues.empty() && allowedValues.isArray())
176  {
177  for (const auto &allowedValue : allowedValues)
178  _allowedValues.emplace_back(allowedValue.asString());
179  }
180  }
181  }
182  }
183  catch (exception& e)
184  {
185  ossimNotify(ossimNotifyLevel_WARN)<<"JsonParam::loadJSON() parse error encountered. Ignoring "
186  "but should check the JsonConfig JSON for parameter <"<<_name<<">."<<endl;
187  return false;
188  }
189  return true;
190 }
static ossimString upcase(const ossimString &aString)
Definition: ossimString.cpp:34
void setValue(void *value)
Definition: JsonConfig.cpp:47
std::vector< ossimString > _allowedValues
Definition: JsonConfig.h:179
ossimString _name
Definition: JsonConfig.h:174
os2<< "> n<< " > nendobj n
ParamType _type
Definition: JsonConfig.h:177
ossimString _label
Definition: JsonConfig.h:175
bool empty() const
Definition: ossimString.h:411
ossimString _descr
Definition: JsonConfig.h:176
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)

◆ name()

const ossimString& ossim::JsonParam::name ( ) const
inline

Definition at line 142 of file JsonConfig.h.

References _name.

Referenced by ossim::JsonConfig::loadJSON(), and ossim::JsonConfig::setParameter().

142 { return _name; }
ossimString _name
Definition: JsonConfig.h:174

◆ operator==()

bool ossim::JsonParam::operator== ( const JsonParam p) const
inline

Definition at line 165 of file JsonConfig.h.

References _name.

165 { return (p._name == _name); }
ossimString _name
Definition: JsonConfig.h:174

◆ resetValue()

void ossim::JsonParam::resetValue ( )
private

Definition at line 81 of file JsonConfig.cpp.

References _type, _value, BOOL, FLOAT, INT, STRING, UINT, and VECTOR.

Referenced by ~JsonParam().

82 {
83  if (!_value)
84  return;
85 
86  switch (_type)
87  {
88  case JsonParam::BOOL:
89  delete (bool*)_value;
90  break;
91  case JsonParam::INT:
92  case JsonParam::UINT:
93  delete (int*)_value;
94  break;
95  case JsonParam::FLOAT:
96  delete (double*)_value;
97  break;
98  case JsonParam::STRING:
99  delete (string*)_value;
100  break;
101  case JsonParam::VECTOR:
102  ((vector<double>*)_value)->clear();
103  delete (vector<double>*)_value;
104  break;
105  default:
106  break;
107  }
108  _value = 0;
109 }
ParamType _type
Definition: JsonConfig.h:177

◆ saveJSON()

void ossim::JsonParam::saveJSON ( Json::Value &  json_node) const

Definition at line 192 of file JsonConfig.cpp.

References _allowedValues, _descr, _label, _name, _type, _value, BOOL, FLOAT, INT, n, STRING, ossimString::string(), UINT, UNASSIGNED, and VECTOR.

Referenced by ossim::JsonConfig::loadJSON(), and ossim::operator<<().

193 {
194  vector<double>& v = *(vector<double>*)_value; // maybe not used since maybe not valid cast
195  unsigned int n = 0;
196  int i;
197  double f;
198  string s;
199  bool b;
200 
201  paramNode["name"] = _name.string();
202  paramNode["label"] = _label.string();
203  paramNode["descr"] = _descr.string();
204 
205  switch (_type)
206  {
207  case JsonParam::BOOL:
208  paramNode["type"] = "bool";
209  b = *(bool*)_value;
210  paramNode["value"] = b;
211  break;
212 
213  case JsonParam::INT:
214  paramNode["type"] = "int";
215  i = *(int*)_value;
216  paramNode["value"] = i;
217  break;
218 
219  case JsonParam::UINT:
220  paramNode["type"] = "uint";
221  n = *(unsigned int*)_value;
222  paramNode["value"] = n;
223  break;
224 
225  case JsonParam::FLOAT:
226  paramNode["type"] = "float";
227  f = *(double*)_value;
228  paramNode["value"] = f;
229  break;
230 
231  case JsonParam::STRING:
232  paramNode["type"] = "string";
233  s = *(string*)_value;
234  paramNode["value"] = s;
235  if (!_allowedValues.empty())
236  {
237  Json::Value allowedValues(Json::arrayValue);
238  for (const auto &allowedValue : _allowedValues)
239  allowedValues.append(allowedValue.c_str());
240  paramNode["allowedValues"] = allowedValues;
241  }
242  break;
243 
244  case JsonParam::VECTOR:
245  paramNode["type"] = "vector";
246  n = v.size();
247  for (unsigned int j=0; j<n; ++j)
248  paramNode["value"][j] = v[j];
249  break;
250 
251  case UNASSIGNED:
252  default:
253  break;
254  }
255 }
std::vector< ossimString > _allowedValues
Definition: JsonConfig.h:179
ossimString _name
Definition: JsonConfig.h:174
os2<< "> n<< " > nendobj n
ParamType _type
Definition: JsonConfig.h:177
ossimString _label
Definition: JsonConfig.h:175
ossimString _descr
Definition: JsonConfig.h:176
const std::string & string() const
Definition: ossimString.h:414

◆ setValue()

void ossim::JsonParam::setValue ( void *  value)
private

Definition at line 47 of file JsonConfig.cpp.

References _type, _value, BOOL, FLOAT, INT, STRING, UINT, and VECTOR.

Referenced by JsonParam(), and loadJSON().

48 {
49  if (!value)
50  return;
51 
52  switch (_type)
53  {
54  case JsonParam::BOOL:
55  _value = new bool;
56  memcpy(_value, value, sizeof(bool));
57  break;
58  case JsonParam::INT:
59  _value = new int;
60  memcpy(_value, value, sizeof(int));
61  break;
62  case JsonParam::UINT:
63  _value = new unsigned int;
64  memcpy(_value, value, sizeof(unsigned int));
65  break;
66  case JsonParam::FLOAT:
67  _value = new double;
68  memcpy(_value, value, sizeof(double));
69  break;
70  case JsonParam::STRING:
71  _value = new string(*(string*)(value));
72  break;
73  case JsonParam::VECTOR:
74  _value = new vector<double>(*(vector<double>*)(value));
75  break;
76  default:
77  _value = 0;
78  }
79 }
ParamType _type
Definition: JsonConfig.h:177

◆ type()

ParamType ossim::JsonParam::type ( ) const
inline

Definition at line 145 of file JsonConfig.h.

References _type.

145 { return _type; }
ParamType _type
Definition: JsonConfig.h:177

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const JsonParam obj 
)
friend

Outputs JSON to output stream provided.

Definition at line 299 of file JsonConfig.cpp.

300 {
301  Json::Value jsonNode;
302  obj.saveJSON(jsonNode);
303  out << jsonNode << endl;
304  return out;
305 }

Member Data Documentation

◆ _allowedValues

std::vector<ossimString> ossim::JsonParam::_allowedValues
private

Definition at line 179 of file JsonConfig.h.

Referenced by loadJSON(), and saveJSON().

◆ _descr

ossimString ossim::JsonParam::_descr
private

Definition at line 176 of file JsonConfig.h.

Referenced by descr(), loadJSON(), and saveJSON().

◆ _label

ossimString ossim::JsonParam::_label
private

Definition at line 175 of file JsonConfig.h.

Referenced by label(), loadJSON(), and saveJSON().

◆ _name

ossimString ossim::JsonParam::_name
private

Definition at line 174 of file JsonConfig.h.

Referenced by loadJSON(), name(), operator==(), and saveJSON().

◆ _type

ParamType ossim::JsonParam::_type
private

◆ _value

void* ossim::JsonParam::_value
private

The documentation for this class was generated from the following files: