InMemoryParameter.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include <swarmio/data/Variant.pb.h>
5 #include <swarmio/data/discovery/Schema.pb.h>
6 
7 namespace swarmio::simulator
8 {
15  {
16  private:
17 
22  data::Variant _value;
23 
28  data::discovery::Type _type;
29 
34  bool _readOnly;
35 
40  std::string _path;
41 
42  public:
43 
50  InMemoryParameter(const std::string& path, bool value, bool readOnly = false)
51  : _readOnly(readOnly), _path(path)
52  {
54  _value.set_bool_value(value);
55  }
56 
63  InMemoryParameter(const std::string& path, const std::string& value, bool readOnly = false)
64  : _readOnly(readOnly), _path(path)
65  {
67  _value.set_string_value(value);
68  }
69 
76  InMemoryParameter(const std::string& path, const char* value, bool readOnly = false)
77  : _readOnly(readOnly), _path(path)
78  {
80  _value.set_string_value(value);
81  }
82 
89  InMemoryParameter(const std::string& path, int value, bool readOnly = false)
90  : _readOnly(readOnly), _path(path)
91  {
92  _type = data::discovery::Type::INT;
93  _value.set_int_value(value);
94  }
95 
102  InMemoryParameter(const std::string& path, double value, bool readOnly = false)
103  : _readOnly(readOnly), _path(path)
104  {
105  _type = data::discovery::Type::DOUBLE;
106  _value.set_double_value(value);
107  }
108 
114  virtual data::Variant Get(const std::string& path) override
115  {
116  if (path == _path)
117  {
118  return _value;
119  }
120  else
121  {
122  throw Exception("In memory parameter queried for unknown resource path.");
123  }
124  }
125 
131  virtual void Set(const std::string& path, const data::Variant& value) override
132  {
133  if (path == _path)
134  {
135  if (_readOnly)
136  {
137  throw Exception("Parameter is read-only.");
138  }
139  else
140  {
141  switch (value.value_case())
142  {
143  case data::Variant::ValueCase::kBoolValue:
144  if (_type == data::discovery::Type::BOOL)
145  {
146  _value = value;
147  }
148  else
149  {
150  throw Exception("Invalid type for parameter.");
151  }
152  break;
153 
154  case data::Variant::ValueCase::kDoubleValue:
155  if (_type == data::discovery::Type::DOUBLE)
156  {
157  _value = value;
158  }
159  else
160  {
161  throw Exception("Invalid type for parameter.");
162  }
163  break;
164 
165  case data::Variant::ValueCase::kIntValue:
166  if (_type == data::discovery::Type::INT)
167  {
168  _value = value;
169  }
170  else
171  {
172  throw Exception("Invalid type for parameter.");
173  }
174  break;
175 
176  case data::Variant::ValueCase::kStringValue:
177  if (_type == data::discovery::Type::STRING)
178  {
179  _value = value;
180  }
181  else
182  {
183  throw Exception("Invalid type for parameter.");
184  }
185  break;
186 
187  default:
188  throw Exception("Unknown type for parameter.");
189  }
190  }
191 
192  }
193  else
194  {
195  throw Exception("In memory parameter queried for unknown resource path.");
196  }
197  }
198 
204  virtual data::discovery::Field GetFieldDescriptor(const std::string& path) const override
205  {
206  if (path == _path)
207  {
208  data::discovery::Field field;
209  field.set_type(_type);
210  return field;
211  }
212  else
213  {
214  throw Exception("In memory parameter queried for unknown resource path.");
215  }
216  }
217 
223  virtual bool CanWrite(const std::string& path) const noexcept override
224  {
225  if (path == _path)
226  {
227  return !_readOnly;
228  }
229  else
230  {
231  return false;
232  }
233  }
234 
240  virtual bool CanRead(const std::string& path) const noexcept override
241  {
242  if (path == _path)
243  {
244  return true;
245  }
246  else
247  {
248  return false;
249  }
250  }
251 
257  const std::string& GetPath() const
258  {
259  return _path;
260  }
261  };
262 }
263 
virtual void Set(const std::string &path, const data::Variant &value) override
Set the current value of the target.
Abstract base class for registered keys.
Definition: Target.h:12
std::string _path
Resource path.
virtual bool CanWrite(const std::string &path) const noexceptoverride
Determines whether the value can be written.
virtual bool CanRead(const std::string &path) const noexceptoverride
Determines whether the value can be read.
data::discovery::Type _type
Type.
Exception class thrown by all library classes.
InMemoryParameter(const std::string &path, bool value, bool readOnly=false)
Create a new boolean in-memory parameter.
A strongly typed in-memory parameter target that can be made read-only.
const std::string & GetPath() const
Get the resource path of the parameter.
InMemoryParameter(const std::string &path, const std::string &value, bool readOnly=false)
Create a new string in-memory parameter.
virtual data::Variant Get(const std::string &path) override
Get the current value of the target.
virtual data::discovery::Field GetFieldDescriptor(const std::string &path) const override
Get the data type of the target.
InMemoryParameter(const std::string &path, int value, bool readOnly=false)
Create a new integer in-memory parameter.
InMemoryParameter(const std::string &path, double value, bool readOnly=false)
Create a new integer in-memory parameter.
InMemoryParameter(const std::string &path, const char *value, bool readOnly=false)
Create a new string in-memory parameter.


swarmros
Author(s):
autogenerated on Fri Apr 3 2020 03:42:48