- Aldebaran documentation |
- C++ Libraries |
- index

libqi-api 2.8.7.4

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stringtypeinterface.hxx
Go to the documentation of this file.
1 #pragma once
2 /*
3 ** Copyright (C) 2013 Aldebaran Robotics
4 ** See COPYING for the license
5 */
6
7 #ifndef _QITYPE_DETAIL_TYPESTRING_HXX_
8 #define _QITYPE_DETAIL_TYPESTRING_HXX_
9
10 #include <algorithm>
11 #include <qi/os.hpp>
12 #include <qi/type/detail/structtypeinterface.hxx>
13
14 namespace qi
15 {
16 inline std::string StringTypeInterface::getString(void* storage)
17 {
18 ManagedRawString raw = get(storage);
19 std::string res(raw.first.first, raw.first.second);
20 if (raw.second)
21 raw.second(raw.first);
22 return res;
23 }
24
25 inline void StringTypeInterface::set(void** storage, const std::string& val)
26 {
27 set(storage, val.c_str(), val.size());
28 }
29
30 class QI_API StringTypeInterfaceImpl: public StringTypeInterface
31 {
32 public:
33 using Methods = DefaultTypeImplMethods<std::string, TypeByPointerPOD<std::string>>;
34 ManagedRawString get(void* storage) override
35 {
36 std::string* ptr = (std::string*)Methods::ptrFromStorage(&storage);
37 return ManagedRawString(RawString(const_cast<char*>(ptr->c_str()), ptr->size()),
38 Deleter());
39 }
40 void set(void** storage, const char* value, size_t sz) override
41 {
42 std::string* ptr = (std::string*)Methods::ptrFromStorage(storage);
43 ptr->assign(value, sz);
44 }
45
46 _QI_BOUNCE_TYPE_METHODS(Methods);
47 };
48
49 template<>
50 class TypeImpl<std::string>: public StringTypeInterfaceImpl
51 {};
52
53 class QI_API TypeCStringImpl: public StringTypeInterface
54 {
55 public:
56 ManagedRawString get(void* storage) override
57 {
58 return ManagedRawString(RawString((char*)storage, strlen((char*)storage)),
59 Deleter());
60 }
61 void set(void** storage, const char* ptr, size_t sz) override
62 {
63 *(char**)storage = qi::os::strdup(ptr);
64 }
65 void* clone(void* src) override
66 {
67 return qi::os::strdup((char*)src);
68 }
69 void destroy(void* src) override
70 {
71 free(src);
72 }
73 using Methods = DefaultTypeImplMethods<char*, TypeByValue<char*>>;
74 _QI_BOUNCE_TYPE_METHODS_NOCLONE(Methods);
75 };
76
77 template<>
78 class TypeImpl<char*>: public TypeCStringImpl
79 {};
80
81
82 template<int I> class TypeImpl<char [I]>: public StringTypeInterface
83 {
84 public:
85 void* clone(void* src) override
86 {
87 char* res = new char[I];
88 memcpy(res, src, I);
89 return res;
90 }
91 void destroy(void* ptr) override
92 {
93 delete[] (char*)ptr;
94 }
95 ManagedRawString get(void* storage) override
96 {
97 return ManagedRawString(RawString((char*)storage, I-1),
98 Deleter());
99 }
100 void set(void** storage, const char* ptr, size_t sz) override
101 {
102 qiLogCategory("qitype.typestring");
103 // haha...no
104 qiLogWarning() << "set on C array not implemented";
105 }
106
107 using Methods = DefaultTypeImplMethods<char[I], TypeByPointerPOD<char[I]>>;
108 _QI_BOUNCE_TYPE_METHODS_NOCLONE(Methods);
109 };
110
111 template<class Func, class... Args >
112 auto callWithInstance(Func&& f, Args&&... args)
113 -> decltype(std::forward<Func>(f)(std::forward<Args>(args)...))
114 {
115 return std::forward<Func>(f)(std::forward<Args>(args)...);
116 }
117
118 template<class Func, class Obj, class... Args >
119 auto callWithInstance(Func&& f, Obj&& o, Args&&... args)
120 -> decltype((std::forward<Obj>(o).*std::forward<Func>(f))(std::forward<Args>(args)...))
121 {
122 return (std::forward<Obj>(o).*std::forward<Func>(f))(std::forward<Args>(args)...);
123 }
124
125 inline StringTypeInterface::ManagedRawString makeManagedString(const std::string& s)
126 {
127 return StringTypeInterface::ManagedRawString(StringTypeInterface::RawString((char*)s.c_str(), s.size()),
128 StringTypeInterface::Deleter());
129 }
130
131 inline StringTypeInterface::ManagedRawString makeManagedString(std::string&& s)
132 {
133 const auto size = s.size() + 1;
134 auto strKeptAlive = new char[size]();
135 std::copy(begin(s), end(s), strKeptAlive);
136 return StringTypeInterface::ManagedRawString(StringTypeInterface::RawString(strKeptAlive, size),
137 [](const StringTypeInterface::RawString& rawStr){ delete[] rawStr.first; });
138 }
139
147 template<typename T, typename F> class TypeEquivalentString: public StringTypeInterface
148 {
149 public:
150 TypeEquivalentString(F f): _getter(f) {}
151 using Impl = DefaultTypeImplMethods<T, TypeByPointerPOD<T>>;
152
153 void set(void** storage, const char* ptr, size_t sz) override
154 {
155 T* inst = (T*)ptrFromStorage(storage);
156 *inst = T(std::string(ptr, sz));
157 }
158
159 ManagedRawString get(void* storage) override
160 {
161 T* ptr = (T*)Impl::ptrFromStorage(&storage);
162 return makeManagedString(callWithInstance(_getter, *ptr));
163 }
164
165 _QI_BOUNCE_TYPE_METHODS(Impl);
166 F _getter;
167 };
168
169 template<typename T, typename F>
170 StringTypeInterface* makeTypeEquivalentString(T*, F f)
171 {
172 return new TypeEquivalentString<T, F>(f);
173 }
174 }
175
179 #define QI_EQUIVALENT_STRING_REGISTER(type, func) \
180 static bool BOOST_PP_CAT(__qi_registration, __COUNTER__) QI_ATTR_UNUSED \
181 = qi::registerType(qi::typeId<type>(), qi::makeTypeEquivalentString((type*)0, func))
182
183 #endif // _QITYPE_DETAIL_TYPESTRING_HXX_
qi::TypeCStringImpl::set
void set(void **storage, const char *ptr, size_t sz) override
Set the value of the string.
Definition: stringtypeinterface.hxx:61
qi::TypeInterface::ptrFromStorage
virtual void * ptrFromStorage(void **)=0
qi::TypeImpl
Definition: typeimpl.hxx:300
qi::StringTypeInterface
Definition: typeinterface.hpp:104
_QI_BOUNCE_TYPE_METHODS_NOCLONE
#define _QI_BOUNCE_TYPE_METHODS_NOCLONE(Bounce)
Implement all methods of Type except clone/destroy as bouncers to Bouncer.
Definition: typeimpl.hxx:266
QI_API
#define QI_API
Definition: api.hpp:33
qi::StringTypeInterfaceImpl::set
void set(void **storage, const char *value, size_t sz) override
Set the value of the string.
Definition: stringtypeinterface.hxx:40
qi::makeTypeEquivalentString
StringTypeInterface * makeTypeEquivalentString(T *, F f)
Definition: stringtypeinterface.hxx:170
qi::TypeImpl< char[I]>::set
void set(void **storage, const char *ptr, size_t sz) override
Set the value of the string.
Definition: stringtypeinterface.hxx:100
structtypeinterface.hxx
qiLogCategory
#define qiLogCategory(Cat)
Definition: log.hpp:53
qi::StringTypeInterface::Deleter
boost::function< void(const RawString &)> Deleter
Definition: typeinterface.hpp:108
os.hpp
qi::TypeImpl< char[I]>::destroy
void destroy(void *ptr) override
Free all resources of a storage.
Definition: stringtypeinterface.hxx:91
qi::TypeEquivalentString::Impl
DefaultTypeImplMethods< T, TypeByPointerPOD< T >> Impl
Definition: stringtypeinterface.hxx:151
qi::makeManagedString
StringTypeInterface::ManagedRawString makeManagedString(const std::string &s)
Definition: stringtypeinterface.hxx:125
qi::StringTypeInterface::ManagedRawString
std::pair< RawString, Deleter > ManagedRawString
Definition: typeinterface.hpp:109
qiLogWarning
#define qiLogWarning(...)
Log in warning mode.
Definition: log.hpp:109
qi::StringTypeInterfaceImpl
Definition: stringtypeinterface.hxx:30
qi::TypeEquivalentString::TypeEquivalentString
TypeEquivalentString(F f)
Definition: stringtypeinterface.hxx:150
qi::TypeCStringImpl::destroy
void destroy(void *src) override
Free all resources of a storage.
Definition: stringtypeinterface.hxx:69
qi::TypeCStringImpl
Definition: stringtypeinterface.hxx:53
_QI_BOUNCE_TYPE_METHODS
#define _QI_BOUNCE_TYPE_METHODS(Bounce)
Implement all methods of Type as bouncers to Bouncer.
Definition: typeimpl.hxx:273
qi::TypeEquivalentString::_getter
F _getter
Definition: stringtypeinterface.hxx:166
qi::TypeEquivalentString::_QI_BOUNCE_TYPE_METHODS
_QI_BOUNCE_TYPE_METHODS(Impl)
qi::DefaultTypeImplMethods
Definition: typeimpl.hxx:228
qi::StringTypeInterface::set
void set(void **storage, const std::string &value)
Set the value of the string.
Definition: stringtypeinterface.hxx:25
qi::callWithInstance
auto callWithInstance(Func &&f, Args &&...args) -> decltype(std::forward< Func >(f)(std::forward< Args >(args)...))
Definition: stringtypeinterface.hxx:112
qi::StringTypeInterface::RawString
std::pair< char *, size_t > RawString
Definition: typeinterface.hpp:107
qi::src
T src(const std::atomic< T > &x)
Definition: atomic.hpp:318
qi::StringTypeInterface::getString
std::string getString(void *storage)
Get a copy of the string value.
Definition: stringtypeinterface.hxx:16
qi::TypeEquivalentString
Definition: stringtypeinterface.hxx:147
qi::DefaultTypeImplMethods::ptrFromStorage
static void * ptrFromStorage(void **storage)
Definition: typeimpl.hxx:238
qi::TypeImpl< char[I]>::clone
void * clone(void *src) override
Allocate a storage and copy the value given as an argument.
Definition: stringtypeinterface.hxx:85
qi::os::strdup
char * strdup(const char *src)
Implement POSIX compliant strdup.
qi::TypeCStringImpl::clone
void * clone(void *src) override
Allocate a storage and copy the value given as an argument.
Definition: stringtypeinterface.hxx:65
qi::TypeEquivalentString::set
void set(void **storage, const char *ptr, size_t sz) override
Set the value of the string.
Definition: stringtypeinterface.hxx:153