boolean_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_boolean_node.hpp"
12 #include "_detail/ihas_value_config.hpp"
14 
15 namespace Cvb
16 {
17 CVB_BEGIN_INLINE_NS
18 
19 namespace GevServer
20 {
22 
24  : public ValueNode
26  , public Private::IConfigurableBooleanNode
27  , public Private::IHasValueConfig<IntegerBaseNodePtr>
29 {
30 public:
31  BooleanNode(HandleGuard<Node> &&guard) noexcept : ValueNode(std::move(guard)) {}
32 
43  static BooleanNodePtr Create(const String &name, const GevServer::Namespace &nameSpace)
44  {
45  String okName = EnsureNodeNameOnly(name);
46  return std::make_shared<BooleanNode>(HandleGuard<Node>(
47  CExports::CreateGSBooleanNodeTyped(okName.data(), static_cast<CExports::TGSNamespace>(nameSpace))));
48  }
49 
63  static BooleanNodePtr Create(const String &name) { return Create(ParseName(name), ParseNamespace(name)); }
64 
69  GenApi::AccessMode AccessMode() const override
70  {
71  auto value = GetTerminalRegisterNode<BooleanNode, IntegerBaseNodePtr>(this, [](Node *node) {
72  auto hasValueConfig = dynamic_cast<Private::IHasValueConfig<IntegerBaseNodePtr> *>(node);
73  return (dynamic_cast<GenApi::Private::IRegisterNode *>(node) || !hasValueConfig->ValueConfigAsNode()) ? true
74  : false;
75  });
76  if (value == nullptr)
78  else
79  return value->AccessMode();
80  }
81 
86  GenApi::CacheMode CacheMode() const override
87  {
88  auto value = GetTerminalRegisterNode<BooleanNode, IntegerBaseNodePtr>(this, [](Node *node) {
89  auto hasValueConfig = dynamic_cast<Private::IHasValueConfig<IntegerBaseNodePtr> *>(node);
90  return (dynamic_cast<GenApi::Private::IRegisterNode *>(node) || !hasValueConfig->ValueConfigAsNode()) ? true
91  : false;
92  });
93  if (value == nullptr)
95  else
96  return value->CacheMode();
97  }
98 
103  template <class Rep, class Period> std::chrono::duration<Rep, Period> PollingTime() const
104  {
105  auto value = GetTerminalRegisterNode<BooleanNode, IntegerBaseNodePtr>(this, [](Node *node) {
106  auto isRegisterNode = dynamic_cast<GenApi::Private::IRegisterNode *>(node);
107  auto isCommandNode = dynamic_cast<GenApi::Private::ICommandNode *>(node);
108  return (isRegisterNode || isCommandNode) ? true : false;
109  });
110  if (value == nullptr)
112  else
113  return value->template PollingTime<Rep, Period>();
114  }
115 
120  std::int64_t OffValue() const { return GetInfoAsInt(NodeInfo::OffValue); }
121 
130  void SetOffValue(const std::int64_t &value) { SetInfoAsInt(NodeInfo::OffValue, value); }
131 
136  std::int64_t OnValue() const { return GetInfoAsInt(NodeInfo::OnValue); }
137 
146  void SetOnValue(const std::int64_t &value) { SetInfoAsInt(NodeInfo::OnValue, value); }
147 
149 
153  bool Value() const
154  {
155  return (NativeCall<CExports::cvbbool_t>(
156  [&](CExports::cvbbool_t &value) { return CVB_CALL_CAPI(GSNGetAsBoolean(Handle(), value)); }))
157  ? true
158  : false;
159  }
160 
162 
166  void SetValue(bool value)
167  {
168  NativeCall([&]() { return CVB_CALL_CAPI(GSNSetAsBoolean(Handle(), static_cast<CExports::cvbbool_t>(value))); });
169  }
170 
179  template <class T> T ValueConfig() const
180  {
181  throw std::runtime_error("requested value config type must be derived from IntegerBaseNode or be "
182  "of type int64_t");
183  }
184 
185  IntegerBaseNodePtr ValueConfigAsNode() const override;
186 
199  template <class T> void SetValueConfig(const T & /*value*/)
200  {
201  throw std::runtime_error("requested command config type must be derived from "
202  "IntegerBaseNode or be of integral type");
203  }
204 
209  void FromString(const String &value) override
210  {
211  if (value.empty())
212  throw std::runtime_error("String must not be empty");
213 
214  std::string str(Internal::CastToAscii(value));
215  std::transform(str.begin(), str.end(), str.begin(), [](char ch)
216  {
217  return static_cast<char>(std::tolower(ch));
218  });
219  std::istringstream is(str);
220  bool b;
221  is >> std::boolalpha >> b;
222  SetValue(b);
223  }
224 
231  String ToString() const override { return Value() ? CVB_LIT("true") : CVB_LIT("false"); }
232 };
233 
234 template <> inline std::int64_t BooleanNode::ValueConfig<std::int64_t>() const
235 {
236  return GetInfoAsInt(NodeInfo::Value);
237 }
238 
239 template <> inline void BooleanNode::SetValueConfig<std::int64_t>(const std::int64_t &value)
240 {
241  SetInfoAsInt(NodeInfo::Value, value);
242 }
243 }
244 CVB_END_INLINE_NS
245 }
void FromString(const String &value) override
Sets this node's value from the string value .
Definition: boolean_node.hpp:209
STL class.
Base class for all nodes that have a value.
Definition: value_node.hpp:27
static String ParseName(const String &name)
Gets the name part of the given node name .
Definition: decl_node.hpp:606
static BooleanNodePtr Create(const String &name)
Creates a new BooleanNode with the given name .
Definition: boolean_node.hpp:63
static BooleanNodePtr Create(const String &name, const GevServer::Namespace &nameSpace)
Creates a new BooleanNode with the given name and nameSpace .
Definition: boolean_node.hpp:43
static GevServer::Namespace ParseNamespace(const String &name)
Gets the Namespace from the given name .
Definition: decl_node.hpp:584
void SetValue(bool value)
Sets the value of this boolean node.
Definition: boolean_node.hpp:166
Namespace
The possible name spaces a node can be in.
Definition: gevserver.hpp:147
static String EnsureNodeNameOnly(const String &name)
Throws if the given name has a namespace prefix.
Definition: decl_node.hpp:557
STL class.
Root namespace for the Image Manager interface.
Definition: version.hpp:11
String ToString() const override
Gets the string representation of this node.
Definition: boolean_node.hpp:231
GenApi::AccessMode AccessMode() const override
Gets the access mode by querying all ValueConfigs for it.
Definition: boolean_node.hpp:69
GenApi::BooleanNode that is configurable.
Definition: boolean_node.hpp:23
std::chrono::duration< Rep, Period > PollingTime() const
Gets the polling time by querying all ValueConfigs for it.
Definition: boolean_node.hpp:103
void SetOnValue(const std::int64_t &value)
Sets the value to be written if GenApi::IBooleanNode.Value is set to true.
Definition: boolean_node.hpp:146
Node can be read and written to.
T ValueConfig() const
Gets the value configuration of this boolean node.
Definition: boolean_node.hpp:179
Boolean specific: accesses the integer value for false.
bool Value() const
Gets the value of this boolean node.
Definition: boolean_node.hpp:153
Accesses the value configuration.
Basic GevServer node for device feature access.
Definition: decl_node.hpp:37
void SetValueConfig(const T &)
Sets the value configuration of this boolean node.
Definition: boolean_node.hpp:199
GenApi::CacheMode CacheMode() const override
Gets the cache mode by querying all ValueConfigs for it.
Definition: boolean_node.hpp:86
std::int64_t OnValue() const
Gets the value to be written if GenApi::IBooleanNode.Value is set to true.
Definition: boolean_node.hpp:136
void SetOffValue(const std::int64_t &value)
Sets the value to be written if GenApi::IBooleanNode.Value is set to false.
Definition: boolean_node.hpp:130
std::int64_t OffValue() const
Gets the value to be written if GenApi::IBooleanNode.Value is set to false.
Definition: boolean_node.hpp:120
void * Handle() const noexcept
Classic API node handle.
Definition: decl_node.hpp:101
CacheMode
Defines how the value is cached.
Definition: genapi.hpp:219
AccessMode
Access possibility of the node.
Definition: genapi.hpp:184