Skip to content

Commit e4b35ae

Browse files
committed
The first implementation of a builder class for complex interfaces
Signed-off-by: CursedRock17 <[email protected]>
1 parent 77ede02 commit e4b35ae

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#ifndef RCLCPP__PARAMETER_DESCRIPTOR_WRAPPER_HPP_
2+
#define RCLCPP__PARAMETER_DESCRIPTOR_WRAPPER_HPP_
3+
4+
// Standard library includes
5+
#include <functional>
6+
#include <utility>
7+
#include <memory>
8+
9+
// Additional ROS libraries needed
10+
#include "node.hpp"
11+
#include "rcl_interfaces/msg/describe_parameters.hpp"
12+
#include "rclcpp/node.hpp"
13+
14+
namespace rclcpp
15+
{
16+
17+
// Implments ParameterDesription class with builder design pattern
18+
class ParameterDescription
19+
{
20+
public:
21+
// List of classes the builder manages
22+
class FloatingPointDescription;
23+
24+
// Two constructors in case certain methods don't need to declare_parameters with a Type T in the form of a template
25+
ParameterDescription(std::string name, std::uint8_t type, std::string description, std::string additional_constraints, bool read_only. bool dynamic_typing) : m_name{name}, parameter_descriptor.type{type}, m_description{description}, m_additional_constraints{additional_constraints}, read_only{read_only}, dyanmic_typing{m_dyanmic_typing}{};
26+
27+
private:
28+
// The main descriptor object we're meant to adjust
29+
rcl_interfaces::msg::ParameterDescriptor parameter_descriptor = {};
30+
31+
// Copies all the information in ParameterDescriptor.msg - https://github.com/ros2/rcl_interfaces/blob/rolling/rcl_interfaces/msg/ParameterDescriptor.msg
32+
std::string m_name;
33+
std::string m_description;
34+
35+
// Parameter Constraints
36+
std::string m_additional_constraints;
37+
bool m_read_only;
38+
bool m_dynamic_typing;
39+
40+
};
41+
42+
class ParameterDescription::FloatingPointDescription
43+
{
44+
public:
45+
// Our Main build methods which will construct the base class
46+
ParameterDescription build() const;
47+
48+
//Builder Method - Mains
49+
FloatingPointDescription& SetName(std::string name);
50+
FloatingPointDescription& SetType(std::uint8_t type);
51+
FloatingPointDescription& SetDescription(std::string description);
52+
FloatingPointDescription& SetAdditionalConstraints(std::string constraints);
53+
FloatingPointDescription& SetReadOnly(bool read_only);
54+
FloatingPointDescription& SetDynamicTyping(bool dynamic_typing);
55+
56+
// Need the current node in order to begin the configuraiton state forit via the declare_parameter function which setups up the Node
57+
template<typename ParameterType>
58+
FloatingPointDescription& DeclareParameter<ParameterType>(ParameterType default_value, std::shared_ptr<rclcpp::Node> required_node)
59+
{
60+
required_node->declare_parameter<ParameterType>(m_name, default_value, parameter_descriptor);
61+
return *this;
62+
}
63+
// Extended build methods specific to this class
64+
FloatingPointDescription& SetMin(float min);
65+
FloatingPointDescription& SetMax(float max);
66+
FloatingPointDescription& SetStep(float step);
67+
68+
// Simplification Method - Outside of the base clss that the user should be able to set up easily, they should then be able to call a function which setups up the specifics for this class (The floating point parameter description)
69+
// Here we will have a difference between generic types and templated types
70+
FloatingPointDescription& SetFloatingPointDescription(float min, float max, float step);
71+
// We will again need access to the current development node to declare its parameters
72+
template<typename ParameterType>
73+
FloatingPointDescription& SetFloatingPointDescription(std::string name, ParameterType default_value, float min, float max, float step)
74+
{
75+
parameter_descriptor.floating_point_range.resize(1);
76+
parameter_descriptor.floating_point_range.at(0).from_value = min;
77+
parameter_descriptor.floating_point_range.at(0).to_value = max;
78+
parameter_descriptor.floating_point_range.at(0).step = step;
79+
80+
return *this;
81+
}
82+
83+
84+
private:
85+
rcl_interfaces::msg::ParameterDescriptor parameter_descriptor = {};
86+
std::string m_name{""};
87+
parameter_descriptor.type{rcl_interfaces::msg::ParameterType::PARAMETER_NOT_SET};
88+
std::string m_description{""};
89+
90+
std::string m_additional_constraints{""};
91+
bool m_read_only{false};
92+
bool m_dynamic_typing{false};
93+
94+
// This is a floating point so we'll use floating points in the range
95+
float m_min{0.0f};
96+
float m_max{1.0f};
97+
float m_step{0.0f};
98+
};
99+
100+
} // namespace rclcpp
101+
102+
#endif // headerguards for parameter_descriptor
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
3+
#include "../../include/rclcpp/parameter_descriptor_wrapper.hpp"
4+
5+
namespace rclcpp {
6+
// We use initializer lists in order to promote safety in uninitialzied state
7+
ParameterDescription(std::string name, std::uint8 type, std::string description, std::string additional_constraints, bool read_only. bool dynamic_typing) : m_name{name}, parameter_descriptor.type{type}, m_description{description}, m_additional_constraints{additional_constraints}, read_only{read_only}, dyanmic_typing{m_dyanmic_typing}
8+
{}
9+
10+
11+
// ParameterDescription - FloatingPointDescription
12+
// First the build methods to connect to the base class in the builder
13+
ParameterDescription FloatingPointDescription::build() const
14+
{
15+
return ParameterDescription(m_name, parameter_descriptor.type, m_description, m_additional_constraints, m_read_only, m_dynamic_typing)
16+
}
17+
18+
// Builder methods which set up the original class
19+
// They all follow the same format of initing the value given within the base class, then returning this current class
20+
FloatingPointDescription& FloatingPointDescription::SetName(std::string name)
21+
{
22+
m_name = name;
23+
return *this;
24+
}
25+
26+
FloatingPointDescription& FloatingPointDescription::SetType(std::uint8_t type)
27+
{
28+
parameter_descriptor.type = type;
29+
return *this;
30+
}
31+
32+
FloatingPointDescription& FloatingPointDescription::SetDescription(std::string description)
33+
{
34+
m_description = description;
35+
return *this;
36+
}
37+
38+
FloatingPointDescription& FloatingPointDescription::SetAdditionalConstraints(std::string constraints)
39+
{
40+
m_additional_constraints = constraints;
41+
return *this;
42+
}
43+
44+
FloatingPointDescription& FloatingPointDescription::SetReadOnly(bool read_only)
45+
{
46+
m_read_only = read_only;
47+
return *this;
48+
}
49+
50+
FloatingPointDescription& FloatingPointDescription::SetDynamicTyping(bool dynamic_typing)
51+
{
52+
m_dynamic_typing = dynamic_typing;
53+
return *this;
54+
}
55+
56+
// These are the extension for this class that don't have access in the main class so we'll initialize here
57+
FloatingPointDescription& FloatingPointDescription::SetMin(float min)
58+
{
59+
m_min = min;
60+
return *this;
61+
}
62+
63+
FloatingPointDescription& FloatingPointDescription::SetMax(float max)
64+
{
65+
m_max = max;
66+
return *this;
67+
}
68+
69+
FloatingPointDescription& FloatingPointDescription::SetStep(float step)
70+
{
71+
m_step = step;
72+
return *this;
73+
}
74+
75+
// Here is the Specific range function for this parameter description
76+
FloatingPointDescription& FloatingPointDescription::SetFloatingPointDescripion(float min, float max, float step)
77+
{
78+
parameter_descriptor.floating_point_range.resize(1);
79+
parameter_descriptor.floating_point_range.at(0).from_value = min;
80+
parameter_descriptor.floating_point_range.at(0).to_value = max;
81+
parameter_descriptor.floating_point_range.at(0).step = step;
82+
return *this;
83+
}
84+
85+
} // rclcpp namespace

0 commit comments

Comments
 (0)