RTC Toolkit  1.0.0
parameter.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_DATATASK_PARAMETER_HPP
13 #define RTCTK_DATATASK_PARAMETER_HPP
14 
15 #include <string>
16 #include <stdexcept>
18 
19 namespace rtctk::dataTask {
25 {
26  public:
27  ParameterNotSet(const std::string& par_name)
28  : rtctk::componentFramework::RtctkException("Parameter '" + par_name + "' not set!") {}
29 };
30 
32 {
33  public:
34  ParameterNotWriteable(const std::string& par_name)
35  : rtctk::componentFramework::RtctkException("Parameter '" + par_name + "' not writeable!") {}
36 };
37 
43 //TODO can we do some value range checking with traits as well here?
44 template<class T>
45 class Parameter
46 {
47 public:
48 
49  Parameter(std::string const& name)
50  : m_name(name)
51  , m_value()
52  , m_is_set(false)
53  , m_is_locked(false)
54  { }
55 
56  Parameter(std::string const& name, T const& value)
57  : m_name(name)
58  , m_value(value)
59  , m_is_set(true)
60  , m_is_locked(false)
61  { }
62 
67  void Set(T const& value)
68  {
69  if(m_is_locked) {
70  CII_THROW(ParameterNotWriteable, m_name);
71  }
72  m_value = value;
73  m_is_set = true;
74  }
75 
79  void Lock() {
80  m_is_locked = true;
81  }
82 
86  void Unlock() {
87  m_is_locked = false;
88  }
89 
90 
95  T& Get()
96  {
97  if(not m_is_set) {
98  CII_THROW(ParameterNotSet, m_name);
99  }
100  return m_value;
101  }
102 
107  bool IsSet() {
108  return m_is_set;
109  }
110 
114  void CheckSet() {
115  if(not m_is_set) {
116  CII_THROW(ParameterNotSet, m_name);
117  }
118  }
119 
120 private:
121  const std::string m_name; //< name of paramter
122  T m_value; //< value
123  bool m_is_set; //< bool is the value set
124  bool m_is_locked; //< bool is the value locked.
125 };
126 
127 } // namespace
128 
129 #endif
exceptions.hpp
Provides macros and utilities for exception handling.
wscript.name
name
Definition: wscript:15
rtctk::dataTask::Parameter::IsSet
bool IsSet()
checks if the value in paramter has been set
Definition: parameter.hpp:107
rtctk::dataTask::ParameterNotWriteable
Definition: parameter.hpp:32
rtctk::componentFramework::RtctkException::RtctkException
RtctkException() noexcept
Definition: exceptions.cpp:101
rtctk::dataTask::Parameter::Set
void Set(T const &value)
Set interface for setting values.
Definition: parameter.hpp:67
rtctk::dataTask::ParameterNotSet::ParameterNotSet
ParameterNotSet(const std::string &par_name)
Definition: parameter.hpp:27
rtctk::dataTask::Parameter::Unlock
void Unlock()
unlock interface
Definition: parameter.hpp:86
rtctk::componentFramework::RtctkException
The RtctkException class is the base class for all Rtctk exceptions.
Definition: exceptions.hpp:207
rtctk::dataTask::ParameterNotWriteable::ParameterNotWriteable
ParameterNotWriteable(const std::string &par_name)
Definition: parameter.hpp:34
rtctk::dataTask::ParameterNotSet
Definition: parameter.hpp:25
rtctk::dataTask::Parameter
Class for basic handling of Paramters in read thread Gives a simple interface for locking parameters ...
Definition: parameter.hpp:46
rtctk::dataTask
Definition: messageQueue.hpp:20
rtctk::dataTask::Parameter::CheckSet
void CheckSet()
Check if set if not set throw error.
Definition: parameter.hpp:114
rtctk::dataTask::Parameter::Parameter
Parameter(std::string const &name, T const &value)
Definition: parameter.hpp:56
rtctk::dataTask::Parameter::Parameter
Parameter(std::string const &name)
Definition: parameter.hpp:49
rtctk::dataTask::Parameter::Lock
void Lock()
lock parameter so cannot be changed while locked
Definition: parameter.hpp:79
rtctk
Definition: agnostictopicifPubSubTypes.cpp:31
rtctk::dataTask::Parameter::Get
T & Get()
get the value set in paramter
Definition: parameter.hpp:95