-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTypeWrapper.hpp
95 lines (71 loc) · 2.2 KB
/
TypeWrapper.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
#include <typelib/typemodel.hh>
#include <typelib/value.hh>
#include <map>
#include <string>
#include <memory>
namespace orocos_cpp {
/**
* @brief This wrapper gived convinoent access to the data of a typelib type without linking the actual type
*
* The constructor creates a map so that a call on e.g. /base/samples/Joints data linke thsi is possible
* Typelib::Value joints;
* TypeWrapper jointwrapper(joints);
* TypeWrapper element = jointwrapper["elements"][0];
* std::cout << element.toString() << std::endl;
*
* see main5.cpp for an example to get values from an unidentified port
*
*
*/
class TypeWrapper : public std::map<std::string, std::shared_ptr<TypeWrapper> > {
public:
TypeWrapper(Typelib::Value& value);
bool isValid() {
return value.getData();
}
/**
* @brief prints the whole type introspection to the console
*/
void printType() {
printType("");
}
const Typelib::Value& getTypelibValue() {
return value;
}
std::string getTypelibTypeName() {
return value.getType().getName();
}
template<class TYPE> TYPE* getAs(const std::string& type_name) {
if (value.getType().getName() == type_name) {
return reinterpret_cast<TYPE*>(value.getData());
}
return nullptr;
}
std::string toString();
double toDouble();
// double toValue();
/**
* @brief overload operator to skip the shared_ptr content in the map of the base class
*/
TypeWrapper& operator[](const std::string& key) {
auto type = this->find(key);
if (type != this->end()) {
// std::shared_ptr<TypeWrapper> wrap = std::map<std::string, std::shared_ptr<TypeWrapper>>::operator[](key);
return *(type->second);
}
throw(std::runtime_error(key + " is not a valid field of this type"));
}
protected:
void printType(std::string ident);
private:
// void add(Typelib::Value& value);
std::string numericToString();
std::string containerToString();
void addCompound();
void addContainer();
void addArray();
Typelib::Type::Category category;
Typelib::Value value;
};
} // namespace orocos_cpp