ros2_control - galactic
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 & name, const std::string & interface_name, double * value_ptr = nullptr)
32  : name_(name), interface_name_(interface_name), value_ptr_(value_ptr)
33  {
34  }
35 
36  explicit ReadOnlyHandle(const std::string & interface_name)
37  : interface_name_(interface_name), value_ptr_(nullptr)
38  {
39  }
40 
41  explicit ReadOnlyHandle(const char * interface_name)
42  : interface_name_(interface_name), value_ptr_(nullptr)
43  {
44  }
45 
46  ReadOnlyHandle(const ReadOnlyHandle & other) = default;
47 
48  ReadOnlyHandle(ReadOnlyHandle && other) = default;
49 
50  ReadOnlyHandle & operator=(const ReadOnlyHandle & other) = default;
51 
52  ReadOnlyHandle & operator=(ReadOnlyHandle && other) = default;
53 
54  virtual ~ReadOnlyHandle() = default;
55 
57  inline operator bool() const { return value_ptr_ != nullptr; }
58 
59  const std::string & get_name() const { return name_; }
60 
61  const std::string & get_interface_name() const { return interface_name_; }
62 
63  const std::string get_full_name() const { return name_ + "/" + interface_name_; }
64 
65  double get_value() const
66  {
67  THROW_ON_NULLPTR(value_ptr_);
68  return *value_ptr_;
69  }
70 
71 protected:
72  std::string name_;
73  std::string interface_name_;
74  double * value_ptr_;
75 };
76 
78 {
79 public:
81  const std::string & name, const std::string & interface_name, double * value_ptr = nullptr)
82  : ReadOnlyHandle(name, interface_name, value_ptr)
83  {
84  }
85 
86  explicit ReadWriteHandle(const std::string & interface_name) : ReadOnlyHandle(interface_name) {}
87 
88  explicit ReadWriteHandle(const char * interface_name) : ReadOnlyHandle(interface_name) {}
89 
90  ReadWriteHandle(const ReadWriteHandle & other) = default;
91 
92  ReadWriteHandle(ReadWriteHandle && other) = default;
93 
94  ReadWriteHandle & operator=(const ReadWriteHandle & other) = default;
95 
96  ReadWriteHandle & operator=(ReadWriteHandle && other) = default;
97 
98  virtual ~ReadWriteHandle() = default;
99 
100  void set_value(double value)
101  {
102  THROW_ON_NULLPTR(this->value_ptr_);
103  *this->value_ptr_ = value;
104  }
105 };
106 
108 {
109 public:
110  StateInterface(const StateInterface & other) = default;
111 
112  StateInterface(StateInterface && other) = default;
113 
114  using ReadOnlyHandle::ReadOnlyHandle;
115 };
116 
118 {
119 public:
121 
126  CommandInterface(const CommandInterface & other) = delete;
127 
128  CommandInterface(CommandInterface && other) = default;
129 
130  using ReadWriteHandle::ReadWriteHandle;
131 };
132 
133 } // namespace hardware_interface
134 
135 #endif // HARDWARE_INTERFACE__HANDLE_HPP_
Definition: handle.hpp:118
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:78
Definition: handle.hpp:108
Definition: actuator.hpp:29