Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLUtils/src/ICLUtils/Any.h ** 00010 ** Module : ICLUtils ** 00011 ** Authors: Christof Elbrechter ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #pragma once 00032 00033 #include <ICLUtils/CompatMacros.h> 00034 #include <ICLUtils/StringUtils.h> 00035 #include <cstring> 00036 00037 namespace icl{ 00038 namespace utils{ 00039 00041 00109 struct Any : public std::string{ 00111 inline Any(){} 00112 00114 00119 template<class T> 00120 inline Any(const T &t):std::string(str(t)){} 00121 00123 00127 template<class T> 00128 inline operator T() const{ 00129 return as<T>(); 00130 } 00131 00133 00145 template<class T> 00146 inline T as() const{ 00147 return parse<T>(*this); 00148 } 00149 00151 friend inline std::ostream& operator<<(std::ostream &s, const Any &any){ 00152 return s << (const std::string&)any; 00153 } 00154 00156 friend inline std::istream& operator>>(std::istream &s, Any &any){ 00157 return s >> (std::string&)any; 00158 } 00159 00160 private: 00162 Any(size_t n, char c):std::string(n,c){} 00163 00164 public: 00165 00167 00168 template<class T> 00169 static T* ptr(const Any &any){ 00170 return *(T**)(&any[0]); 00171 } 00172 00174 00175 template<class T> 00176 static Any ptr(const T *p){ 00177 Any any(std::string(sizeof(void*),'\0')); 00178 *((const void**)&any[0]) = p; 00179 return any; 00180 } 00181 00182 }; 00183 00185 template<> 00186 inline std::vector<float> Any::as<std::vector<float> >() const{ 00187 const size_t l = std::string::length(); 00188 if(l < sizeof(int)) throw ICLException("cannot convert Any to std::vector<float> size must be at least sizeof(int)"); 00189 const icl8u *p = (const icl8u*)&std::string::operator[](0); 00190 int size = *((const int*)p); 00191 p += sizeof(int); 00192 if(l != sizeof(int) + sizeof(float) * size){ 00193 throw ICLException("error converting Any to std::vector<float> unexpected size"); 00194 } 00195 return std::vector<float>((const float*)p, ((const float*)p) + size); 00196 } 00197 00198 template<> 00199 inline Any::Any(const std::vector<float> &v){ 00200 std::string::resize(sizeof(int) + v.size() * sizeof(float)); 00201 icl8u *p = (icl8u*)&std::string::operator[](0); 00202 *((int*)p) = v.size(); 00203 memcpy(p+sizeof(int),v.data(), v.size()*sizeof(float)); 00204 } 00205 00206 template<> 00207 inline std::vector<int> Any::as<std::vector<int> >() const{ 00208 const size_t l = std::string::length(); 00209 if(l < sizeof(int)) throw ICLException("cannot convert Any to std::vector<int> size must be at least sizeof(int)"); 00210 const icl8u *p = (const icl8u*)&std::string::operator[](0); 00211 int size = *((const int*)p); 00212 p += sizeof(int); 00213 if(l != sizeof(int) + sizeof(int) * size){ 00214 throw ICLException("error converting Any to std::vector<int> unexpected size"); 00215 } 00216 return std::vector<int>((const int*)p, ((const int*)p) + size); 00217 } 00218 00219 template<> 00220 inline Any::Any(const std::vector<int> &v){ 00221 std::string::resize(sizeof(int) + v.size() * sizeof(int)); 00222 icl8u *p = (icl8u*)&std::string::operator[](0); 00223 *((int*)p) = v.size(); 00224 memcpy(p+sizeof(int),v.data(), v.size()*sizeof(int)); 00225 } 00226 00230 } // namespace utils 00231 } 00232