libvisiontransfer  10.0.0
deviceparameters.cpp
1 /*******************************************************************************
2  * Copyright (c) 2022 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 using namespace visiontransfer::param;
23 
24 namespace visiontransfer {
25 
26 /*************** Pimpl class containing all private members ***********/
27 
28 class DeviceParameters::Pimpl {
29 public:
30  Pimpl(const DeviceInfo& device);
31  Pimpl(const char* address, const char* service);
32 
33  template<typename T>
34  void writeParameter(const char* id, const T& value) {
35  paramTrans.writeParameter(id, value);
36  }
37 
38  int readIntParameter(const char* id);
39  double readDoubleParameter(const char* id);
40  bool readBoolParameter(const char* id);
41 
42  void writeIntParameter(const char* id, int value);
43  void writeDoubleParameter(const char* id, double value);
44  void writeBoolParameter(const char* id, bool value);
45 
46  std::map<std::string, ParameterInfo> getAllParameters();
47 
48  Parameter& getParameter(const std::string& name);
49 
50  ParameterSet& getParameterSet();
51 
52 private:
53  std::map<std::string, ParameterInfo> serverSideEnumeration;
54 
55 #ifndef DOXYGEN_SHOULD_SKIP_THIS
56  template<typename T>
57  void setNamedParameterInternal(const std::string& name, T value);
58 #endif
59 
60  ParameterTransfer paramTrans;
61 };
62 
63 /******************** Stubs for all public members ********************/
64 
66  pimpl(new Pimpl(device)) {
67  // All initialization in the pimpl class
68 }
69 
70 DeviceParameters::DeviceParameters(const char* address, const char* service):
71  pimpl(new Pimpl(address, service)) {
72  // All initialization in the pimpl class
73 }
74 
75 DeviceParameters::~DeviceParameters() {
76  delete pimpl;
77 }
78 
79 int DeviceParameters::readIntParameter(const char* id) {
80  return pimpl->readIntParameter(id);
81 }
82 
83 double DeviceParameters::readDoubleParameter(const char* id) {
84  return pimpl->readDoubleParameter(id);
85 }
86 
87 bool DeviceParameters::readBoolParameter(const char* id) {
88  return pimpl->readBoolParameter(id);
89 }
90 
91 void DeviceParameters::writeIntParameter(const char* id, int value) {
92  pimpl->writeIntParameter(id, value);
93 }
94 
95 void DeviceParameters::writeDoubleParameter(const char* id, double value) {
96  pimpl->writeDoubleParameter(id, value);
97 }
98 
99 void DeviceParameters::writeBoolParameter(const char* id, bool value) {
100  pimpl->writeBoolParameter(id, value);
101 }
102 
103 std::map<std::string, ParameterInfo> DeviceParameters::getAllParameters()
104 {
105  return pimpl->getAllParameters();
106 }
107 
108 #ifndef DOXYGEN_SHOULD_SKIP_THIS
109 // Deprecated versions
110 template<>
111 void VT_EXPORT DeviceParameters::setNamedParameter(const std::string& name, double value) {
112  pimpl->writeParameter(name.c_str(), value);
113 }
114 template<>
115 void VT_EXPORT DeviceParameters::setNamedParameter(const std::string& name, int value) {
116  pimpl->writeParameter(name.c_str(), value);
117 }
118 template<>
119 void VT_EXPORT DeviceParameters::setNamedParameter(const std::string& name, bool value) {
120  pimpl->writeParameter(name.c_str(), value);
121 }
122 // New versions (extensible for access for other types)
123 template<>
124 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, double value) {
125  pimpl->writeParameter(name.c_str(), value);
126 }
127 template<>
128 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, int value) {
129  pimpl->writeParameter(name.c_str(), value);
130 }
131 template<>
132 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, bool value) {
133  pimpl->writeParameter(name.c_str(), value);
134 }
135 template<>
136 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, const std::string& value) {
137  pimpl->writeParameter(name.c_str(), value);
138 }
139 template<>
140 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, std::string value) {
141  pimpl->writeParameter(name.c_str(), value);
142 }
143 
144 template<>
145 int VT_EXPORT DeviceParameters::getNamedParameter(const std::string& name) {
146  return pimpl->readIntParameter(name.c_str());
147 }
148 template<>
149 double VT_EXPORT DeviceParameters::getNamedParameter(const std::string& name) {
150  return pimpl->readDoubleParameter(name.c_str());
151 }
152 template<>
153 bool VT_EXPORT DeviceParameters::getNamedParameter(const std::string& name) {
154  return pimpl->readBoolParameter(name.c_str());
155 }
156 #endif
157 
158 #if __cplusplus >= 201103L
159 Parameter DeviceParameters::getParameter(const std::string& name) {
160  return pimpl->getParameter(name); // copied here
161 }
162 
163 ParameterSet DeviceParameters::getParameterSet() {
164  return pimpl->getParameterSet(); // copied here
165 }
166 #endif
167 
168 /******************** Implementation in pimpl class *******************/
169 
170 DeviceParameters::Pimpl::Pimpl(const char* address, const char* service)
171  : paramTrans(address, service) {
172 }
173 
174 DeviceParameters::Pimpl::Pimpl(const DeviceInfo& device)
175  : paramTrans(device.getIpAddress().c_str(), "7683") {
176 }
177 
178 int DeviceParameters::Pimpl::readIntParameter(const char* id) {
179  return paramTrans.readIntParameter(id);
180 }
181 
182 double DeviceParameters::Pimpl::readDoubleParameter(const char* id) {
183  return paramTrans.readDoubleParameter(id);
184 }
185 
186 bool DeviceParameters::Pimpl::readBoolParameter(const char* id) {
187  return paramTrans.readBoolParameter(id);
188 }
189 
190 void DeviceParameters::Pimpl::writeIntParameter(const char* id, int value) {
191  paramTrans.writeParameter(id, value);
192 }
193 
194 void DeviceParameters::Pimpl::writeDoubleParameter(const char* id, double value) {
195  paramTrans.writeParameter(id, value);
196 }
197 
198 void DeviceParameters::Pimpl::writeBoolParameter(const char* id, bool value) {
199  paramTrans.writeParameter(id, value);
200 }
201 
202 std::map<std::string, ParameterInfo> DeviceParameters::Pimpl::getAllParameters() {
203  serverSideEnumeration = paramTrans.getAllParameters();
204  return serverSideEnumeration;
205 }
206 
207 Parameter& DeviceParameters::Pimpl::getParameter(const std::string& name) {
208  auto& paramSet = paramTrans.getParameterSet();
209  if (paramSet.count(name)) {
210  return paramSet[name];
211  } else {
212  throw ParameterException("Invalid or inaccessible parameter name");
213  }
214 }
215 
216 ParameterSet& DeviceParameters::Pimpl::getParameterSet() {
217  return paramTrans.getParameterSet();
218 }
219 
220 } // namespace
221 
std::map< std::string, ParameterInfo > getAllParameters()
Enumerates all simple parameters as reported by the device [DEPRECATED].
Allows a configuration of device parameters over the network.
std::string getIpAddress() const
Gets the IP address of the device.
Definition: deviceinfo.h:97
void setParameter(const std::string &name, T value)
Set a parameter by name. ParameterException for invalid names or values.
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