Image Component Library (ICL)
Any.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/Any.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/StringUtils.h>
35 #include <cstring>
36 
37 namespace icl{
38  namespace utils{
39 
41 
109  struct Any : public std::string{
111  inline Any(){}
112 
114 
119  template<class T>
120  inline Any(const T &t):std::string(str(t)){}
121 
123 
127  template<class T>
128  inline operator T() const{
129  return as<T>();
130  }
131 
133 
145  template<class T>
146  inline T as() const{
147  return parse<T>(*this);
148  }
149 
151  friend inline std::ostream& operator<<(std::ostream &s, const Any &any){
152  return s << (const std::string&)any;
153  }
154 
156  friend inline std::istream& operator>>(std::istream &s, Any &any){
157  return s >> (std::string&)any;
158  }
159 
160  private:
162  Any(size_t n, char c):std::string(n,c){}
163 
164  public:
165 
167 
168  template<class T>
169  static T* ptr(const Any &any){
170  return *(T**)(&any[0]);
171  }
172 
174 
175  template<class T>
176  static Any ptr(const T *p){
177  Any any(std::string(sizeof(void*),'\0'));
178  *((const void**)&any[0]) = p;
179  return any;
180  }
181 
182  };
183 
185  template<>
186  inline std::vector<float> Any::as<std::vector<float> >() const{
187  const size_t l = std::string::length();
188  if(l < sizeof(int)) throw ICLException("cannot convert Any to std::vector<float> size must be at least sizeof(int)");
189  const icl8u *p = (const icl8u*)&std::string::operator[](0);
190  int size = *((const int*)p);
191  p += sizeof(int);
192  if(l != sizeof(int) + sizeof(float) * size){
193  throw ICLException("error converting Any to std::vector<float> unexpected size");
194  }
195  return std::vector<float>((const float*)p, ((const float*)p) + size);
196  }
197 
198  template<>
199  inline Any::Any(const std::vector<float> &v){
200  std::string::resize(sizeof(int) + v.size() * sizeof(float));
201  icl8u *p = (icl8u*)&std::string::operator[](0);
202  *((int*)p) = v.size();
203  memcpy(p+sizeof(int),v.data(), v.size()*sizeof(float));
204  }
205 
206  template<>
207  inline std::vector<int> Any::as<std::vector<int> >() const{
208  const size_t l = std::string::length();
209  if(l < sizeof(int)) throw ICLException("cannot convert Any to std::vector<int> size must be at least sizeof(int)");
210  const icl8u *p = (const icl8u*)&std::string::operator[](0);
211  int size = *((const int*)p);
212  p += sizeof(int);
213  if(l != sizeof(int) + sizeof(int) * size){
214  throw ICLException("error converting Any to std::vector<int> unexpected size");
215  }
216  return std::vector<int>((const int*)p, ((const int*)p) + size);
217  }
218 
219  template<>
220  inline Any::Any(const std::vector<int> &v){
221  std::string::resize(sizeof(int) + v.size() * sizeof(int));
222  icl8u *p = (icl8u*)&std::string::operator[](0);
223  *((int*)p) = v.size();
224  memcpy(p+sizeof(int),v.data(), v.size()*sizeof(int));
225  }
226 
230  } // namespace utils
231 }
232 
Any()
Empty constructor.
Definition: Any.h:111
Any(size_t n, char c)
for direct creation of the parent string (used in the static ptr methods)
Definition: Any.h:162
undocument this line if you encounter any issues!
Definition: Any.h:37
Ipp8u icl8u
8Bit unsigned integer type for the ICL
Definition: BasicTypes.h:64
T as() const
explict cast method (if implicit cast is ambiguous)
Definition: Any.h:146
Any(const T &t)
real constructor with any given source type
Definition: Any.h:120
friend std::istream & operator>>(std::istream &s, Any &any)
remembers the istream-operator<< that Any is of type std::string
Definition: Any.h:156
static Any ptr(const T *p)
this method can be used to create an any that contains a binary encoded pointer
Definition: Any.h:176
static T * ptr(const Any &any)
this method can be used to extract a pointer that was encoded as Any
Definition: Any.h:169
std::string str(const T &t)
convert a data type into a string using an std::ostringstream instance
Definition: StringUtils.h:136
Base class for Exception handling in the ICL.
Definition: Exception.h:42
friend std::ostream & operator<<(std::ostream &s, const Any &any)
remembers the ostream-operator<< that Any is of type std::string
Definition: Any.h:151
Simple generic data type implementation that uses a string based data representation.
Definition: Any.h:109