libvisiontransfer  8.3.0
deviceparameters.cpp
1 /*******************************************************************************
2  * Copyright (c) 2020 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include "visiontransfer/deviceparameters.h"
16 #include "visiontransfer/parametertransfer.h"
17 #include "visiontransfer/exceptions.h"
18 #include "visiontransfer/common.h"
19 
20 using namespace visiontransfer;
21 using namespace visiontransfer::internal;
22 
23 namespace visiontransfer {
24 
25 /*************** Pimpl class containing all private members ***********/
26 
27 class DeviceParameters::Pimpl {
28 public:
29  Pimpl(const DeviceInfo& device);
30  Pimpl(const char* address, const char* service);
31 
32  int readIntParameter(int id);
33  double readDoubleParameter(int id);
34  bool readBoolParameter(int id);
35 
36  void writeIntParameter(int id, int value);
37  void writeDoubleParameter(int id, double value);
38  void writeBoolParameter(int id, bool value);
39 
40  std::map<std::string, ParameterInfo> getAllParameters();
41 
42  // this template is selected for non-floating point arguments (i.e. int and bool).
43  template<typename T>
44  void setParameter_impl(StandardParameterIDs::ParameterID id, ParameterInfo::ParameterType type, T value, ...)
45  {
46  int cid = static_cast<int>(id);
47  switch (type) {
48  case ParameterInfo::TYPE_INT: {
49  writeIntParameter(cid, static_cast<int>(value));
50  break;
51  }
52  case ParameterInfo::TYPE_BOOL: {
53  writeBoolParameter(cid, value != 0);
54  break;
55  }
56  case ParameterInfo::TYPE_DOUBLE: {
57  writeDoubleParameter(cid, static_cast<double>(value));
58  break;
59  }
60  }
61  }
62 
63  // this template is selected for floating point arguments
64  template<typename T, typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
65  void setParameter_impl(StandardParameterIDs::ParameterID id, ParameterInfo::ParameterType type, T value, double)
66  {
67  int cid = static_cast<int>(id);
68  switch (type) {
69  case ParameterInfo::TYPE_DOUBLE: {
70  writeDoubleParameter(cid, value);
71  break;
72  }
73  case ParameterInfo::TYPE_INT: {
74  writeIntParameter(cid, static_cast<int>(value));
75  break;
76  }
77  case ParameterInfo::TYPE_BOOL: {
78  writeBoolParameter(cid, value != 0);
79  break;
80  }
81  }
82  }
83 
84  template <typename T>
85  void setParameter(StandardParameterIDs::ParameterID id, ParameterInfo::ParameterType type, T t) {
86  setParameter_impl<T>(id, type, t, double{});
87  }
88 
89  ParameterInfo getParameter(const std::string& name);
90 
91  void lookupIDAndType(const std::string& name, internal::StandardParameterIDs::ParameterID& id, ParameterInfo::ParameterType& type);
92 
93 private:
94  std::map<std::string, ParameterInfo> serverSideEnumeration;
95 
96  std::map<std::string, ParameterInfo> getAllParametersInternal();
97 
98 #ifndef DOXYGEN_SHOULD_SKIP_THIS
99  template<typename T>
100  void setNamedParameterInternal(const std::string& name, T value);
101 #endif
102 
103  ParameterTransfer paramTrans;
104 };
105 
106 /******************** Stubs for all public members ********************/
107 
109  pimpl(new Pimpl(device)) {
110  // All initialization in the pimpl class
111 }
112 
113 DeviceParameters::DeviceParameters(const char* address, const char* service):
114  pimpl(new Pimpl(address, service)) {
115  // All initialization in the pimpl class
116 }
117 
118 DeviceParameters::~DeviceParameters() {
119  delete pimpl;
120 }
121 
122 int DeviceParameters::readIntParameter(int id) {
123  return pimpl->readIntParameter(id);
124 }
125 
126 double DeviceParameters::readDoubleParameter(int id) {
127  return pimpl->readDoubleParameter(id);
128 }
129 
130 bool DeviceParameters::readBoolParameter(int id) {
131  return pimpl->readBoolParameter(id);
132 }
133 
134 void DeviceParameters::writeIntParameter(int id, int value) {
135  pimpl->writeIntParameter(id, value);
136 }
137 
138 void DeviceParameters::writeDoubleParameter(int id, double value) {
139  pimpl->writeDoubleParameter(id, value);
140 }
141 
142 void DeviceParameters::writeBoolParameter(int id, bool value) {
143  pimpl->writeBoolParameter(id, value);
144 }
145 
146 void DeviceParameters::Pimpl::lookupIDAndType(const std::string& name, StandardParameterIDs::ParameterID& id, ParameterInfo::ParameterType& type) {
147  if (serverSideEnumeration.size() == 0) {
148  // get the server-side parameter list first (which reports the types as well)
149  (void) getAllParameters();
150  }
151  id = StandardParameterIDs::getParameterIDForName(name);
152  if (id == StandardParameterIDs::ParameterID::UNDEFINED) {
153  ParameterException ex("Cannot access parameter with unknown name: " + name);
154  throw ex;
155  }
156  auto it = serverSideEnumeration.find(name);
157  if (it == serverSideEnumeration.end()) {
158  ParameterException ex("Server did not report the parameter in the supported list: " + name);
159  throw ex;
160  }
161  type = it->second.getType();
162 }
163 
164 std::map<std::string, ParameterInfo> DeviceParameters::getAllParameters()
165 {
166  return pimpl->getAllParameters();
167 }
168 
169 #ifndef DOXYGEN_SHOULD_SKIP_THIS
170 template<>
171 void VT_EXPORT DeviceParameters::setNamedParameter(const std::string& name, double value) {
172  StandardParameterIDs::ParameterID id;
173  ParameterInfo::ParameterType type;
174  pimpl->lookupIDAndType(name, id, type);
175  pimpl->setParameter<double>(id, type, value);
176 }
177 template<>
178 void VT_EXPORT DeviceParameters::setNamedParameter(const std::string& name, int value) {
179  StandardParameterIDs::ParameterID id;
180  ParameterInfo::ParameterType type;
181  pimpl->lookupIDAndType(name, id, type);
182  pimpl->setParameter<int>(id, type, value);
183 }
184 template<>
185 void VT_EXPORT DeviceParameters::setNamedParameter(const std::string& name, bool value) {
186  StandardParameterIDs::ParameterID id;
187  ParameterInfo::ParameterType type;
188  pimpl->lookupIDAndType(name, id, type);
189  pimpl->setParameter<bool>(id, type, value);
190 }
191 
192 template<>
193 int VT_EXPORT DeviceParameters::getNamedParameter(const std::string& name) {
194  StandardParameterIDs::ParameterID id;
195  ParameterInfo::ParameterType type;
196  pimpl->lookupIDAndType(name, id, type);
197  return pimpl->getParameter(name).getValue<int>();
198 }
199 template<>
200 double VT_EXPORT DeviceParameters::getNamedParameter(const std::string& name) {
201  StandardParameterIDs::ParameterID id;
202  ParameterInfo::ParameterType type;
203  pimpl->lookupIDAndType(name, id, type);
204  return pimpl->getParameter(name).getValue<double>();
205 }
206 template<>
207 bool VT_EXPORT DeviceParameters::getNamedParameter(const std::string& name) {
208  StandardParameterIDs::ParameterID id;
209  ParameterInfo::ParameterType type;
210  pimpl->lookupIDAndType(name, id, type);
211  return pimpl->getParameter(name).getValue<bool>();
212 }
213 #endif
214 
215 /******************** Implementation in pimpl class *******************/
216 
217 DeviceParameters::Pimpl::Pimpl(const char* address, const char* service)
218  : paramTrans(address, service) {
219 }
220 
221 DeviceParameters::Pimpl::Pimpl(const DeviceInfo& device)
222  : paramTrans(device.getIpAddress().c_str(), "7683") {
223 }
224 
225 int DeviceParameters::Pimpl::readIntParameter(int id) {
226  return paramTrans.readIntParameter(id);
227 }
228 
229 double DeviceParameters::Pimpl::readDoubleParameter(int id) {
230  return paramTrans.readDoubleParameter(id);
231 }
232 
233 bool DeviceParameters::Pimpl::readBoolParameter(int id) {
234  return paramTrans.readBoolParameter(id);
235 }
236 
237 void DeviceParameters::Pimpl::writeIntParameter(int id, int value) {
238  paramTrans.writeIntParameter(id, value);
239 }
240 
241 void DeviceParameters::Pimpl::writeDoubleParameter(int id, double value) {
242  paramTrans.writeDoubleParameter(id, value);
243 }
244 
245 void DeviceParameters::Pimpl::writeBoolParameter(int id, bool value) {
246  paramTrans.writeBoolParameter(id, value);
247 }
248 
249 std::map<std::string, ParameterInfo> DeviceParameters::Pimpl::getAllParameters() {
250  serverSideEnumeration = paramTrans.getAllParameters();
251  return serverSideEnumeration;
252 }
253 
254 
255 ParameterInfo DeviceParameters::Pimpl::getParameter(const std::string& name)
256 {
257  return serverSideEnumeration[name];
258 }
259 
260 } // namespace
261 
std::map< std::string, ParameterInfo > getAllParameters()
Enumerates all parameters as reported by the device.
Allows a configuration of device parameters over the network.
std::string getIpAddress() const
Gets the IP address of the device.
Definition: deviceinfo.h:97
Exception class that is used for all parameter-related exceptions.
Definition: exceptions.h:41
T getNamedParameter(const std::string &name)
Get a parameter by name, specifying the return type (int, double or bool). ParameterException for inv...
Aggregates information about a discovered device.
Definition: deviceinfo.h:47
DeviceParameters(const DeviceInfo &device)
Connects to parameter server of a Nerian stereo device by using the device information from device en...
void setNamedParameter(const std::string &name, T value)
Set a parameter by name. ParameterException for invalid names.
Nerian Vision Technologies