CVB++ 14.1
command_node.hpp
1#pragma once
2
3#include <cctype>
4
5#include "../global.hpp"
6
7#include "integer_base_node.hpp"
8#include "value_node.hpp"
9
11#include "_detail/iconfigurable_command_node.hpp"
12#include "_detail/ihas_value_config.hpp"
13#include "_detail/iconfigurable_register_node.hpp"
15
16namespace Cvb
17{
18CVB_BEGIN_INLINE_NS
19namespace GevServer
20{
22
25 : public ValueNode
27 , public Private::IConfigurableCommandNode
28 , public Private::IHasValueConfig<IntegerBaseNodePtr>
30{
31public:
32 explicit CommandNode(HandleGuard<Node> &&guard) : ValueNode(std::move(guard)) {}
33
44 static CommandNodePtr Create(const String &name, const GevServer::Namespace &nameSpace)
45 {
46 String okName = EnsureNodeNameOnly(name);
47 return std::make_shared<CommandNode>(HandleGuard<Node>(
48 CreateGSCommandNodeTyped(okName.data(), static_cast<CExports::TGSNamespace>(nameSpace))));
49 }
50
64 static CommandNodePtr Create(const String &name) { return Create(ParseName(name), ParseNamespace(name)); }
65
71 {
72 auto value = GetTerminalRegisterNode<CommandNode, IntegerBaseNodePtr>(
73 this, [](Node *node) { return dynamic_cast<Private::IConfigurableRegisterNode*>(node) ? true : false; });
74 if (value == nullptr)
76 else
77 return value->AccessMode();
78 }
79
85 {
86 auto value = GetTerminalRegisterNode<CommandNode, IntegerBaseNodePtr>(
87 this, [](Node *node) { return dynamic_cast<Private::IConfigurableRegisterNode*>(node) ? true : false; });
88 if (value == nullptr)
90 else
91 return value->CacheMode();
92 }
93
97 bool IsStreamable() const override { return false; }
98
99 [[noreturn]] void SetIsStreamable(const CExports::cvbint64_t & /*value*/) override
100 {
101 throw std::runtime_error("Command nodes cannot be streamed");
102 }
103
107 bool IsDone() const
108 {
109 return (NativeCall<CExports::cvbbool_t>(
110 [&](CExports::cvbbool_t &value) { return CVB_CALL_CAPI(GSNGetAsBoolean(Handle(), value)); }))
111 ? true
112 : false;
113 }
114
118 void Execute()
119 {
120 NativeCall([&]() { return CVB_CALL_CAPI(GSNSetAsBoolean(Handle(), true)); });
121 }
122
130 template <class Rep, class Period> std::chrono::duration<Rep, Period> PollingTime() const
131 {
132 auto timespan = GetInfoAsInt(NodeInfo::PollingTime);
133 if (timespan > 0)
134 return std::chrono::duration<Rep, Period>(timespan);
135 else
137 }
138
146 template <class Rep, class Period> void SetPollingTime(const std::chrono::duration<Rep, Period> &timespan)
147 {
149 SetInfoAsInt(NodeInfo::PollingTime, -1);
150 else
151 SetInfoAsInt(NodeInfo::PollingTime, std::chrono::duration_cast<std::chrono::milliseconds>(timespan).count());
152 }
153
163 template <class T> T CommandConfig() const
164 {
165 throw std::runtime_error("requested command config type must be derived from IntegerBaseNode or "
166 "of integral type");
167 }
168
182 template <class T> void SetCommandConfig(const T & /*value*/)
183 {
184
185 throw std::runtime_error("requested command config type must be derived from "
186 "IntegerBaseNode or "
187 "of type std::int64_t");
188 }
189
190 IntegerBaseNodePtr ValueConfigAsNode() const override;
191
201 template <class T> T ValueConfig() const
202 {
203 throw std::runtime_error("requested value config type must be derived from IntegerBaseNode or be "
204 "of type int64_t");
205 }
206
220 template <class T> void SetValueConfig(const T & /*value*/)
221 {
222 throw std::runtime_error("requested value config type must be derived from "
223 "IntegerBaseNode or be of integral type");
224 }
225
230 void FromString(const String &value) override
231 {
232 if (value.empty())
233 throw std::runtime_error("String must not be empty");
234
235 std::string str(Internal::CastToAscii(value));
236 std::transform(str.begin(), str.end(), str.begin(), [](char ch)
237 {
238 return static_cast<char>(std::tolower(ch));
239 });
240 std::istringstream is(str);
241 bool b = false;
242 is >> std::boolalpha >> b;
243 if (b)
244 Execute();
245 }
246
251 String ToString() const override { return IsDone() ? CVB_LIT("true") : CVB_LIT("false"); }
252};
253
254template <> inline std::int64_t CommandNode::CommandConfig<std::int64_t>() const
255{
256 return GetInfoAsInt(NodeInfo::OnValue);
257}
258
259template <> inline IntegerBaseNodePtr CommandNode::CommandConfig<IntegerBaseNodePtr>() const
260{
261 return GetInfoAs<IntegerBaseNode>(NodeInfo::OnValue);
262}
263
264template <> inline void CommandNode::SetCommandConfig<std::int64_t>(const std::int64_t &value)
265{
266 SetInfoAsInt(NodeInfo::OnValue, value);
267}
268
269template <> inline std::int64_t CommandNode::ValueConfig<std::int64_t>() const
270{
271 return GetInfoAsInt(NodeInfo::Value);
272}
273
274template <> inline void CommandNode::SetValueConfig<std::int64_t>(const std::int64_t &value)
275{
276 SetInfoAsInt(NodeInfo::Value, value);
277}
278
279}
280CVB_END_INLINE_NS
281}
A node that can be executed.
Definition: command_node.hpp:30
void Execute()
Executes this command.
Definition: command_node.hpp:118
static CommandNodePtr Create(const String &name, const GevServer::Namespace &nameSpace)
Creates a new CommandNode with the given name and nameSpace .
Definition: command_node.hpp:44
bool IsStreamable() const override
Commands are never streamable.
Definition: command_node.hpp:97
void SetPollingTime(const std::chrono::duration< Rep, Period > &timespan)
Sets the polling time of this value.
Definition: command_node.hpp:146
static CommandNodePtr Create(const String &name)
Creates a new CommandNode with the given name .
Definition: command_node.hpp:64
void SetValueConfig(const T &)
Sets the value configuration of this command node.
Definition: command_node.hpp:220
T CommandConfig() const
Gets and sets the command value configuration of this command node.
Definition: command_node.hpp:163
bool IsDone() const
If this command is readable, the execution state can be queried.
Definition: command_node.hpp:107
void FromString(const String &value) override
Executes if value is "true".
Definition: command_node.hpp:230
GenApi::CacheMode CacheMode() const override
Gets the cache mode by querying all ValueConfigs for it.
Definition: command_node.hpp:84
T ValueConfig() const
Gets and sets the value configuration of this command node.
Definition: command_node.hpp:201
GenApi::AccessMode AccessMode() const override
Gets the access mode by querying all ValueConfigs for it.
Definition: command_node.hpp:70
void SetCommandConfig(const T &)
Sets the command value configuration of this command node.
Definition: command_node.hpp:182
String ToString() const override
Gets the IsDone status as a string.
Definition: command_node.hpp:251
std::chrono::duration< Rep, Period > PollingTime() const
Gets the polling time of this value.
Definition: command_node.hpp:130
Basic GevServer node for device feature access.
Definition: decl_node.hpp:36
static GevServer::Namespace ParseNamespace(const String &name)
Gets the Namespace from the given name .
Definition: decl_node.hpp:567
static String EnsureNodeNameOnly(const String &name)
Throws if the given name has a namespace prefix.
Definition: decl_node.hpp:540
static String ParseName(const String &name)
Gets the name part of the given node name .
Definition: decl_node.hpp:588
void * Handle() const noexcept
Classic API node handle.
Definition: decl_node.hpp:100
Base class for all nodes that have a value.
Definition: value_node.hpp:24
CacheMode
Defines how the value is cached.
Definition: genapi.hpp:220
@ NoCache
No caching used.
AccessMode
Access possibility of the node.
Definition: genapi.hpp:185
@ ReadWrite
Node can be read and written to.
Namespace
The possible name spaces a node can be in.
Definition: gevserver.hpp:147
std::shared_ptr< IntegerBaseNode > IntegerBaseNodePtr
Convenience shared pointer for IntegerBaseNode.
Definition: gevserver.hpp:64
@ PollingTime
Gets the polling time in ms.
@ Value
Accesses the value configuration.
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24