libvisiontransfer  10.6.0
deviceparameters.cpp
1 /*******************************************************************************
2  * Copyright (c) 2023 Allied Vision Technologies 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.writeParameterTransactionGuarded(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  void writeBoolParameterUnguarded(const char* id, bool value);
46 
47  std::map<std::string, ParameterInfo> getAllParameters();
48 
49  bool hasParameter(const std::string& name) const;
50 
51  Parameter const& getParameter(const std::string& name) const;
52 
53  ParameterSet const& getParameterSet() const;
54 
55  void setParameterUpdateCallback(std::function<void(const std::string& uid)> callback);
56 
57  void transactionStartQueue();
58  void transactionCommitQueue();
59 
60 private:
61  std::map<std::string, ParameterInfo> serverSideEnumeration;
62 
63 #ifndef DOXYGEN_SHOULD_SKIP_THIS
64  template<typename T>
65  void setNamedParameterInternal(const std::string& name, T value);
66 #endif
67 
68  ParameterTransfer paramTrans;
69 };
70 
71 /******************** Stubs for all public members ********************/
72 
74  pimpl(new Pimpl(device)) {
75  // All initialization in the pimpl class
76 }
77 
78 DeviceParameters::DeviceParameters(const char* address, const char* service):
79  pimpl(new Pimpl(address, service)) {
80  // All initialization in the pimpl class
81 }
82 
83 DeviceParameters::~DeviceParameters() {
84  delete pimpl;
85 }
86 
87 int DeviceParameters::readIntParameter(const char* id) {
88  return pimpl->readIntParameter(id);
89 }
90 
91 double DeviceParameters::readDoubleParameter(const char* id) {
92  return pimpl->readDoubleParameter(id);
93 }
94 
95 bool DeviceParameters::readBoolParameter(const char* id) {
96  return pimpl->readBoolParameter(id);
97 }
98 
99 void DeviceParameters::writeIntParameter(const char* id, int value) {
100  pimpl->writeIntParameter(id, value);
101 }
102 
103 void DeviceParameters::writeDoubleParameter(const char* id, double value) {
104  pimpl->writeDoubleParameter(id, value);
105 }
106 
107 void DeviceParameters::writeBoolParameter(const char* id, bool value) {
108  pimpl->writeBoolParameter(id, value);
109 }
110 
111 void DeviceParameters::writeBoolParameterUnguarded(const char* id, bool value) {
112  pimpl->writeBoolParameterUnguarded(id, value);
113 }
114 
115 std::map<std::string, ParameterInfo> DeviceParameters::getAllParameters()
116 {
117  return pimpl->getAllParameters();
118 }
119 
120 #ifndef DOXYGEN_SHOULD_SKIP_THIS
121 // Deprecated versions
122 template<>
123 void VT_EXPORT DeviceParameters::setNamedParameter(const std::string& name, double value) {
124  pimpl->writeParameter(name.c_str(), value);
125 }
126 template<>
127 void VT_EXPORT DeviceParameters::setNamedParameter(const std::string& name, int value) {
128  pimpl->writeParameter(name.c_str(), value);
129 }
130 template<>
131 void VT_EXPORT DeviceParameters::setNamedParameter(const std::string& name, bool value) {
132  pimpl->writeParameter(name.c_str(), value);
133 }
134 // New versions (extensible for access for other types)
135 template<>
136 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, double value) {
137  pimpl->writeParameter(name.c_str(), value);
138 }
139 template<>
140 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, int value) {
141  pimpl->writeParameter(name.c_str(), value);
142 }
143 template<>
144 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, bool value) {
145  pimpl->writeParameter(name.c_str(), value);
146 }
147 template<>
148 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, const std::string& value) {
149  pimpl->writeParameter(name.c_str(), value);
150 }
151 template<>
152 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, std::string value) {
153  pimpl->writeParameter(name.c_str(), value);
154 }
155 
156 template<>
157 int VT_EXPORT DeviceParameters::getNamedParameter(const std::string& name) {
158  return pimpl->readIntParameter(name.c_str());
159 }
160 template<>
161 double VT_EXPORT DeviceParameters::getNamedParameter(const std::string& name) {
162  return pimpl->readDoubleParameter(name.c_str());
163 }
164 template<>
165 bool VT_EXPORT DeviceParameters::getNamedParameter(const std::string& name) {
166  return pimpl->readBoolParameter(name.c_str());
167 }
168 #endif
169 
170 #if VISIONTRANSFER_CPLUSPLUS_VERSION >= 201103L
171 bool DeviceParameters::hasParameter(const std::string& name) const {
172  return pimpl->hasParameter(name);
173 }
174 
175 Parameter DeviceParameters::getParameter(const std::string& name) const {
176  return pimpl->getParameter(name); // copied here
177 }
178 
179 ParameterSet DeviceParameters::getParameterSet() const {
180  return pimpl->getParameterSet(); // copied here
181 }
182 
183 void DeviceParameters::setParameterUpdateCallback(std::function<void(const std::string& uid)> callback) {
184  pimpl->setParameterUpdateCallback(callback);
185 }
186 
187 DeviceParameters::TransactionLock::TransactionLock(DeviceParameters::Pimpl* pimpl)
188 : pimpl(pimpl) {
189  pimpl->transactionStartQueue();
190 }
191 
192 DeviceParameters::TransactionLock::~TransactionLock() {
193  pimpl->transactionCommitQueue();
194 }
195 
196 std::unique_ptr<DeviceParameters::TransactionLock> DeviceParameters::transactionLock() {
197  return std::make_unique<DeviceParameters::TransactionLock>(pimpl);
198 }
199 
200 #endif
201 
202 /******************** Implementation in pimpl class *******************/
203 
204 DeviceParameters::Pimpl::Pimpl(const char* address, const char* service)
205  : paramTrans(address, service) {
206 }
207 
208 DeviceParameters::Pimpl::Pimpl(const DeviceInfo& device)
209  : paramTrans(device.getIpAddress().c_str(), "7683") {
210 }
211 
212 int DeviceParameters::Pimpl::readIntParameter(const char* id) {
213  return paramTrans.readIntParameter(id);
214 }
215 
216 double DeviceParameters::Pimpl::readDoubleParameter(const char* id) {
217  return paramTrans.readDoubleParameter(id);
218 }
219 
220 bool DeviceParameters::Pimpl::readBoolParameter(const char* id) {
221  return paramTrans.readBoolParameter(id);
222 }
223 
224 void DeviceParameters::Pimpl::writeIntParameter(const char* id, int value) {
225  paramTrans.writeParameterTransactionGuarded(id, value);
226 }
227 
228 void DeviceParameters::Pimpl::writeDoubleParameter(const char* id, double value) {
229  paramTrans.writeParameterTransactionGuarded(id, value);
230 }
231 
232 void DeviceParameters::Pimpl::writeBoolParameter(const char* id, bool value) {
233  paramTrans.writeParameterTransactionGuarded(id, value);
234 }
235 
236 void DeviceParameters::Pimpl::writeBoolParameterUnguarded(const char* id, bool value) {
237  paramTrans.writeParameterTransactionUnguarded(id, value);
238 }
239 
240 std::map<std::string, ParameterInfo> DeviceParameters::Pimpl::getAllParameters() {
241  serverSideEnumeration = paramTrans.getAllParameters();
242  return serverSideEnumeration;
243 }
244 
245 bool DeviceParameters::Pimpl::hasParameter(const std::string& name) const {
246  auto const& paramSet = paramTrans.getParameterSet();
247  return paramSet.count(name) != 0;
248 }
249 
250 Parameter const& DeviceParameters::Pimpl::getParameter(const std::string& name) const {
251  auto const& paramSet = paramTrans.getParameterSet();
252  if (paramSet.count(name)) {
253  return paramSet.at(name);
254  } else {
255  throw ParameterException(std::string("Invalid or inaccessible parameter name: ") + name);
256  }
257 }
258 
259 ParameterSet const& DeviceParameters::Pimpl::getParameterSet() const {
260  return paramTrans.getParameterSet();
261 }
262 
263 void DeviceParameters::Pimpl::setParameterUpdateCallback(std::function<void(const std::string& uid)> callback) {
264  paramTrans.setParameterUpdateCallback(callback);
265 }
266 
267 void DeviceParameters::Pimpl::transactionStartQueue() {
268  paramTrans.transactionStartQueue();
269 }
270 
271 void DeviceParameters::Pimpl::transactionCommitQueue() {
272  paramTrans.transactionCommitQueue();
273 }
274 
275 } // namespace
276 
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:98
void setParameter(const std::string &name, T value)
Set a parameter by name. ParameterException for invalid names or values.
double at(unsigned int x)
Definition: parameter.h:435
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.
Allied Vision