ros2_control - rolling
handle.hpp
1 // Copyright 2020 PAL Robotics S.L.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef HARDWARE_INTERFACE__HANDLE_HPP_
16 #define HARDWARE_INTERFACE__HANDLE_HPP_
17 
18 #include <string>
19 #include <utility>
20 
21 #include "hardware_interface/macros.hpp"
22 #include "hardware_interface/visibility_control.h"
23 
24 namespace hardware_interface
25 {
28 {
29 public:
31  const std::string & prefix_name, const std::string & interface_name,
32  double * value_ptr = nullptr)
33  : prefix_name_(prefix_name), interface_name_(interface_name), value_ptr_(value_ptr)
34  {
35  }
36 
37  explicit ReadOnlyHandle(const std::string & interface_name)
38  : interface_name_(interface_name), value_ptr_(nullptr)
39  {
40  }
41 
42  explicit ReadOnlyHandle(const char * interface_name)
43  : interface_name_(interface_name), value_ptr_(nullptr)
44  {
45  }
46 
47  ReadOnlyHandle(const ReadOnlyHandle & other) = default;
48 
49  ReadOnlyHandle(ReadOnlyHandle && other) = default;
50 
51  ReadOnlyHandle & operator=(const ReadOnlyHandle & other) = default;
52 
53  ReadOnlyHandle & operator=(ReadOnlyHandle && other) = default;
54 
55  virtual ~ReadOnlyHandle() = default;
56 
58  inline operator bool() const { return value_ptr_ != nullptr; }
59 
60  const std::string get_name() const { return prefix_name_ + "/" + interface_name_; }
61 
62  const std::string & get_interface_name() const { return interface_name_; }
63 
64  [[deprecated(
65  "Replaced by get_name method, which is semantically more correct")]] const std::string
66  get_full_name() const
67  {
68  return get_name();
69  }
70 
71  const std::string & get_prefix_name() const { return prefix_name_; }
72 
73  double get_value() const
74  {
75  THROW_ON_NULLPTR(value_ptr_);
76  return *value_ptr_;
77  }
78 
79 protected:
80  std::string prefix_name_;
81  std::string interface_name_;
82  double * value_ptr_;
83 };
84 
86 {
87 public:
89  const std::string & prefix_name, const std::string & interface_name,
90  double * value_ptr = nullptr)
91  : ReadOnlyHandle(prefix_name, interface_name, value_ptr)
92  {
93  }
94 
95  explicit ReadWriteHandle(const std::string & interface_name) : ReadOnlyHandle(interface_name) {}
96 
97  explicit ReadWriteHandle(const char * interface_name) : ReadOnlyHandle(interface_name) {}
98 
99  ReadWriteHandle(const ReadWriteHandle & other) = default;
100 
101  ReadWriteHandle(ReadWriteHandle && other) = default;
102 
103  ReadWriteHandle & operator=(const ReadWriteHandle & other) = default;
104 
105  ReadWriteHandle & operator=(ReadWriteHandle && other) = default;
106 
107  virtual ~ReadWriteHandle() = default;
108 
109  void set_value(double value)
110  {
111  THROW_ON_NULLPTR(this->value_ptr_);
112  *this->value_ptr_ = value;
113  }
114 };
115 
117 {
118 public:
119  StateInterface(const StateInterface & other) = default;
120 
121  StateInterface(StateInterface && other) = default;
122 
123  using ReadOnlyHandle::ReadOnlyHandle;
124 };
125 
127 {
128 public:
130 
135  CommandInterface(const CommandInterface & other) = delete;
136 
137  CommandInterface(CommandInterface && other) = default;
138 
139  using ReadWriteHandle::ReadWriteHandle;
140 };
141 
142 } // namespace hardware_interface
143 
144 #endif // HARDWARE_INTERFACE__HANDLE_HPP_
Definition: handle.hpp:127
CommandInterface(const CommandInterface &other)=delete
CommandInterface copy constructor is actively deleted.
A handle used to get and set a value on a given interface.
Definition: handle.hpp:28
Definition: handle.hpp:86
Definition: handle.hpp:117
Definition: actuator.hpp:31