ros2_control - jazzy
Loading...
Searching...
No Matches
handle.hpp
1// Copyright 2020 ros2_control development team
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 TRANSMISSION_INTERFACE__HANDLE_HPP_
16#define TRANSMISSION_INTERFACE__HANDLE_HPP_
17
18#include <optional>
19#include <string>
20#include "hardware_interface/macros.hpp"
21
23{
24class Handle
25{
26public:
27 Handle(const std::string & prefix_name, const std::string & interface_name, double * value_ptr)
28 : prefix_name_(prefix_name), interface_name_(interface_name), value_ptr_(value_ptr)
29 {
30 }
31
32 Handle(const Handle & other) = default;
33
34 Handle(Handle && other) = default;
35
36 Handle & operator=(const Handle & other) = default;
37
38 Handle & operator=(Handle && other) = default;
39
40 virtual ~Handle() = default;
41
43 inline operator bool() const { return value_ptr_ != nullptr; }
44
45 const std::string get_name() const { return prefix_name_ + "/" + interface_name_; }
46
47 const std::string & get_interface_name() const { return interface_name_; }
48
49 const std::string & get_prefix_name() const { return prefix_name_; }
50
51 double get_value() const
52 {
53 THROW_ON_NULLPTR(value_ptr_);
54 return *value_ptr_;
55 }
56
57 [[deprecated(
58 "For Transmission Handles use get_value() instead to retrieve the value. This method will be "
59 "removed by the ROS 2 Kilted Kaiju release.")]]
60 std::optional<double> get_optional() const
61 {
62 if (value_ptr_)
63 {
64 return *value_ptr_;
65 }
66 return std::nullopt;
67 }
68
69 bool set_value(double value)
70 {
71 THROW_ON_NULLPTR(this->value_ptr_);
72 *this->value_ptr_ = value;
73 return true;
74 }
75
76protected:
77 std::string prefix_name_;
78 std::string interface_name_;
79 double * value_ptr_ = nullptr;
80};
81
84{
85public:
86 using transmission_interface::Handle::Handle;
87};
88
91{
92public:
93 using transmission_interface::Handle::Handle;
94};
95
96} // namespace transmission_interface
97
98#endif // TRANSMISSION_INTERFACE__HANDLE_HPP_
Definition handle.hpp:25
Definition handle.hpp:91
Definition accessor.hpp:25