libqi-api  2.4.3.28
 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_DETAILS_TYPESTRING_HXX_
8 #define _QITYPE_DETAILS_TYPESTRING_HXX_
9 #include <qi/os.hpp>
11 
12 namespace qi
13 {
14  inline std::string StringTypeInterface::getString(void* storage)
15  {
16  ManagedRawString raw = get(storage);
17  std::string res(raw.first.first, raw.first.second);
18  if (raw.second)
19  raw.second(raw.first);
20  return res;
21  }
22 
23  inline void StringTypeInterface::set(void** storage, const std::string& val)
24  {
25  set(storage, val.c_str(), val.size());
26  }
27 
29  {
30  public:
31  typedef DefaultTypeImplMethods<std::string,
34  virtual ManagedRawString get(void* storage)
35  {
36  std::string* ptr = (std::string*)Methods::ptrFromStorage(&storage);
37  return ManagedRawString(RawString((char*)ptr->c_str(), ptr->size()),
38  Deleter());
39  }
40  virtual void set(void** storage, const char* value, size_t sz)
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 
54  {
55  public:
56  virtual ManagedRawString get(void* storage)
57  {
58  return ManagedRawString(RawString((char*)storage, strlen((char*)storage)),
59  Deleter());
60  }
61  virtual void set(void** storage, const char* ptr, size_t sz)
62  {
63  *(char**)storage = qi::os::strdup(ptr);
64  }
65  virtual void* clone(void* src)
66  {
67  return qi::os::strdup((char*)src);
68  }
69  virtual void destroy(void* src)
70  {
71  free(src);
72  }
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  virtual void* clone(void* src)
86  {
87  char* res = new char[I];
88  memcpy(res, src, I);
89  return res;
90  }
91  virtual void destroy(void* ptr)
92  {
93  delete[] (char*)ptr;
94  }
95  virtual ManagedRawString get(void* storage)
96  {
97  return ManagedRawString(RawString((char*)storage, I-1),
98  Deleter());
99  }
100  virtual void set(void** storage, const char* ptr, size_t sz)
101  {
102  qiLogCategory("qitype.typestring");
103  // haha...no
104  qiLogWarning() << "set on C array not implemented";
105  }
106 
107  typedef DefaultTypeImplMethods<char[I],
110  };
111 
119  template<typename T, typename F> class TypeEquivalentString: public StringTypeInterface
120  {
121  public:
124  virtual void set(void** storage, const char* ptr, size_t sz)
125  {
126  T* inst = (T*)ptrFromStorage(storage);
127  *inst = T(std::string(ptr, sz));
128  }
129  virtual ManagedRawString get(void* storage)
130  {
131  T* ptr = (T*)Impl::ptrFromStorage(&storage);
132  void* str = detail::fieldStorage(ptr, _getter);
133  const std::string& s = detail::fieldValue(ptr, _getter, &str);
134  return ManagedRawString(RawString((char*)s.c_str(), s.size()),
135  Deleter());
136  }
139  };
140 
141  template<typename T, typename F>
143  {
144  return new TypeEquivalentString<T, F>(f);
145  }
146 }
147 
151 #define QI_EQUIVALENT_STRING_REGISTER(type, func) \
152  static bool BOOST_PP_CAT(__qi_registration, __COUNTER__) \
153  = qi::registerType(typeid(type), qi::makeTypeEquivalentString((type*)0, func))
154 
155 #endif // _QITYPE_DETAILS_TYPESTRING_HXX_
virtual void * ptrFromStorage(void **)=0
virtual void * clone(void *src)
Allocate a storage and copy the value given as an argument.
DefaultTypeImplMethods< std::string, TypeByPointerPOD< std::string > > Methods
#define _QI_BOUNCE_TYPE_METHODS_NOCLONE(Bounce)
Implement all methods of Type except clone/destroy as bouncers to Bouncer.
Definition: typeimpl.hxx:268
#define QI_API
Definition: api.hpp:34
std::pair< RawString, Deleter > ManagedRawString
StringTypeInterface * makeTypeEquivalentString(T *, F f)
#define qiLogCategory(Cat)
Definition: log.hpp:46
DefaultTypeImplMethods< char[I], TypeByPointerPOD< char[I]> > Methods
#define qiLogWarning(...)
Log in warning mode.
Definition: log.hpp:102
DefaultTypeImplMethods< T, TypeByPointerPOD< T > > Impl
boost::function< void(const RawString &)> Deleter
virtual void destroy(void *ptr)
Free all resources of a storage.
detail::Accessor< A >::value_type & fieldValue(C *instance, A accessor, void **data)
virtual void destroy(void *src)
Free all resources of a storage.
std::pair< char *, size_t > RawString
#define _QI_BOUNCE_TYPE_METHODS(Bounce)
Implement all methods of Type as bouncers to Bouncer.
Definition: typeimpl.hxx:275
virtual void set(void **storage, const char *ptr, size_t sz)
Set the value of the string.
virtual void * clone(void *src)
Allocate a storage and copy the value given as an argument.
void set(void **storage, const std::string &value)
Set the value of the string.
virtual void set(void **storage, const char *ptr, size_t sz)
Set the value of the string.
DefaultTypeImplMethods< char *, TypeByValue< char * > > Methods
void * fieldStorage(C *inst, A accessor)
std::string getString(void *storage)
Get a copy of the string value.
static void * ptrFromStorage(void **storage)
Definition: typeimpl.hxx:238
virtual void set(void **storage, const char *value, size_t sz)
Set the value of the string.
virtual void set(void **storage, const char *ptr, size_t sz)
Set the value of the string.
char * strdup(const char *src)
Implement POSIX compliant strdup.