MCPcopy Create free account
hub / github.com/BehaviorTree/BehaviorTree.CPP / addConverter

Method addConverter

include/behaviortree_cpp/json_export.h:150–188  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

148
149template <typename T>
150inline void JsonExporter::addConverter()
151{
152 // we need to get the name of the type
153 nlohmann::json const js = T{};
154 // we insert both the name obtained from JSON and demangle
155 if(js.contains("__type"))
156 {
157 type_names_.insert({ std::string(js["__type"]), BT::TypeInfo::Create<T>() });
158 }
159 type_names_.insert({ BT::demangle(typeid(T)), BT::TypeInfo::Create<T>() });
160
161 ToJonConverter to_converter = [](const BT::Any& entry, nlohmann::json& dst) {
162 dst = *const_cast<BT::Any&>(entry).castPtr<T>();
163 };
164 to_json_converters_.insert({ typeid(T), to_converter });
165
166 FromJonConverter from_converter = [](const nlohmann::json& src) -> Entry {
167 T value = src.get<T>();
168 return { BT::Any(value), BT::TypeInfo::Create<T>() };
169 };
170
171 from_json_converters_.insert({ typeid(T), from_converter });
172
173 //---- include vectors of T
174 ToJonConverter to_array_converter = [](const BT::Any& entry, nlohmann::json& dst) {
175 dst = *const_cast<BT::Any&>(entry).castPtr<std::vector<T>>();
176 };
177 to_json_converters_.insert({ typeid(std::vector<T>), to_array_converter });
178
179 FromJonConverter from_array_converter = [](const nlohmann::json& src) -> Entry {
180 std::vector<T> value;
181 for(const auto& item : src)
182 {
183 value.push_back(item.get<T>());
184 }
185 return { BT::Any(value), BT::TypeInfo::Create<std::vector<T>>() };
186 };
187 from_json_array_converters_.insert({ typeid(T), from_array_converter });
188}
189
190template <typename T>
191inline void JsonExporter::addConverter(

Callers

nothing calls this directly

Calls 5

stringFunction · 0.85
demangleFunction · 0.85
AnyClass · 0.85
moveFunction · 0.85
push_backMethod · 0.80

Tested by

no test coverage detected