SMACC2
Loading...
Searching...
No Matches
introspection.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
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/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
22
23#include <boost/core/demangle.hpp>
24#include <boost/statechart/event.hpp>
25#include <boost/statechart/simple_state.hpp>
26#include <boost/statechart/state.hpp>
27
28#include <boost/mpl/for_each.hpp>
29#include <boost/mpl/list.hpp>
30#include <boost/mpl/transform.hpp>
31#include <memory>
32#include <rclcpp/rclcpp.hpp>
33#include <string>
34#include <typeinfo>
35
39
40#include "smacc2_msgs/msg/smacc_transition.hpp"
41
42namespace sc = boost::statechart;
43
44namespace smacc2
45{
46namespace introspection
47{
48using namespace boost;
50
52 const SmaccTransitionInfo & transition, smacc2_msgs::msg::SmaccTransition & transitionMsg);
53
54typedef std::allocator<boost::statechart::none> SmaccAllocator;
55
56template <class T>
57auto optionalNodeHandle(std::shared_ptr<T> & obj) -> T *
58{
59 return obj.get;
60}
61
62template <class T>
63auto optionalNodeHandle(boost::intrusive_ptr<T> & obj) -> T *
64{
65 return obj.get();
66}
67
68template <class T>
69auto optionalNodeHandle(T * obj) -> T *
70{
71 return obj;
72}
73
74inline std::string demangleSymbol(const char * name)
75{
76 // Boost allocates and releases the demangled name; the mangled name is returned when demangling fails.
77 return boost::core::demangle(name);
78}
79
80// Declared after the const char * overload on purpose: placed before it, this call resolved to itself.
81inline std::string demangleSymbol(const std::string & name) { return demangleSymbol(name.c_str()); }
82
83template <typename T>
84inline std::string demangleSymbol()
85{
86 return demangleSymbol(typeid(T).name());
87}
88
89template <class T>
90inline std::string demangledTypeName()
91{
92 return demangleSymbol(typeid(T).name());
93}
94
95inline std::string demangleType(const std::type_info * tinfo)
96{
97 return demangleSymbol(tinfo->name());
98}
99
100inline std::string demangleType(const std::type_info & tinfo)
101{
102 return demangleSymbol(tinfo.name());
103}
104
105template <typename...>
107{
108};
109
110//-------------------------------------------------------------------------
111template <typename T>
113{
114private:
115 typedef char YesType[1];
116 typedef char NoType[2];
117
118 template <typename C>
119 static YesType & test(decltype(&C::getEventLabel));
120 template <typename C>
121 static NoType & test(...);
122
123public:
124 enum
125 {
126 value = sizeof(test<T>(0)) == sizeof(YesType)
127 };
128};
129
130template <typename T>
131typename std::enable_if<HasEventLabel<T>::value, void>::type EventLabel(std::string & label)
132{
133 label = T::getEventLabel();
134}
135
136template <typename T>
137typename std::enable_if<!HasEventLabel<T>::value, void>::type EventLabel(std::string & label)
138{
139 label = "";
140}
141//-----------------------------------------------------------------------
142
143template <typename T>
145{
146private:
147 typedef char YesType[1];
148 typedef char NoType[2];
149
150 template <typename C>
151 static YesType & test(decltype(&C::getDefaultTransitionTag));
152 template <typename C>
153 static NoType & test(...);
154
155public:
156 enum
157 {
158 value = sizeof(test<T>(0)) == sizeof(YesType)
159 };
160};
161
162template <typename T>
163typename std::enable_if<HasAutomaticTransitionTag<T>::value, void>::type automaticTransitionTag(
164 std::string & transition_name)
165{
166 transition_name = T::getDefaultTransitionTag();
167}
168
169template <typename T>
170typename std::enable_if<!HasAutomaticTransitionTag<T>::value, void>::type automaticTransitionTag(
171 std::string & transition_name)
172{
173 transition_name = "";
174}
175
176//-------------------------------------------------
177template <typename T>
179{
180private:
181 typedef char YesType[1];
182 typedef char NoType[2];
183
184 template <typename C>
185 static YesType & test(decltype(&C::getDefaultTransitionType));
186 template <typename C>
187 static NoType & test(...);
188
189public:
190 enum
191 {
192 value = sizeof(test<T>(0)) == sizeof(YesType)
193 };
194};
195
196template <typename T>
197typename std::enable_if<HasAutomaticTransitionType<T>::value, void>::type automaticTransitionType(
198 std::string & transition_type)
199{
200 transition_type = T::getDefaultTransitionType();
201}
202
203template <typename T>
204typename std::enable_if<!HasAutomaticTransitionType<T>::value, void>::type automaticTransitionType(
205 std::string & transition_type)
206{
207 transition_type = demangledTypeName<DEFAULT>();
208}
209
210// there are many ways to implement this, for instance adding static methods to the types
211// PREEMPT is kept commented out on purpose; see default_transition_tags in smacc_types.hpp.
212typedef boost::mpl::list<SUCCESS, ABORT, CANCEL, /*PREEMPT,*/ CONTINUELOOP, ENDLOOP>
214
215//--------------------------------
216
217template <typename T>
218struct type_
219{
220 using type = T;
221};
222
223//---------------------------------------------
224template <typename T>
226{
227 using type = type_<T>;
228};
229
230template <typename TTransition>
232{
234
235 std::string * transitionTypeName;
236 template <typename T>
238 {
239 //RCLCPP_INFO_STREAM(nh_->get_logger(),"comparing.."<< demangleSymbol<T>() <<" vs " << demangleSymbol<TTransition>() );
240 if (std::is_base_of<T, TTransition>::value || std::is_same<T, TTransition>::value)
241 {
243 //RCLCPP_INFO(nh_->get_logger(),"YESS!");
244 }
245 }
246};
247
248template <typename TTransition>
249static std::string getTransitionType()
250{
251 std::string output;
252 CheckType<TTransition> op(&output);
253 using boost::mpl::_1;
254 using wrappedList = typename boost::mpl::transform<DEFAULT_TRANSITION_TYPES, _1>::type;
255
256 boost::mpl::for_each<wrappedList>(op);
257 return output;
258}
259
260} // namespace introspection
261} // namespace smacc2
static YesType & test(decltype(&C::getDefaultTransitionTag))
static YesType & test(decltype(&C::getDefaultTransitionType))
static YesType & test(decltype(&C::getEventLabel))
boost::mpl::list< SUCCESS, ABORT, CANCEL, CONTINUELOOP, ENDLOOP > DEFAULT_TRANSITION_TYPES
std::enable_if< HasAutomaticTransitionType< T >::value, void >::type automaticTransitionType(std::string &transition_type)
auto optionalNodeHandle(std::shared_ptr< T > &obj) -> T *
std::enable_if< HasEventLabel< T >::value, void >::type EventLabel(std::string &label)
static std::string getTransitionType()
std::allocator< boost::statechart::none > SmaccAllocator
std::string demangleSymbol()
std::enable_if< HasAutomaticTransitionTag< T >::value, void >::type automaticTransitionTag(std::string &transition_name)
std::string demangledTypeName()
std::string demangleType(const std::type_info *tinfo)
void transitionInfoToMsg(const SmaccTransitionInfo &transition, smacc2_msgs::msg::SmaccTransition &transitionMsg)
CheckType(std::string *transitionTypeName)