Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Any.h
Go to the documentation of this file.
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.GPL  **
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/StringUtils.h>
00034 #include <cstring>
00035 
00036 namespace icl{
00037   namespace utils{
00038     
00040 
00108     struct Any : public std::string{
00110       inline Any(){}
00111       
00113 
00118       template<class T>
00119       inline Any(const T &t):std::string(str(t)){}
00120       
00122 
00126       template<class T>
00127       inline operator T() const{ 
00128         return as<T>();
00129       }
00130       
00132 
00144       template<class T>
00145       inline T as() const{
00146         return parse<T>(*this);
00147       }
00148       
00150       friend inline std::ostream& operator<<(std::ostream &s, const Any &any){
00151         return s << (const std::string&)any;
00152       }
00153   
00155       friend inline std::istream& operator>>(std::istream &s, Any &any){
00156         return s >> (std::string&)any;
00157       }
00158   
00159       private:
00161       Any(size_t n, char c):std::string(n,c){}
00162   
00163       public:
00164   
00166 
00167       template<class T>
00168       static T* ptr(const Any &any){
00169         return *(T**)(&any[0]);
00170       }
00171   
00173 
00174       template<class T>
00175       static Any ptr(const T *p){
00176         Any any(std::string(sizeof(void*),'\0'));
00177         *((const void**)&any[0]) = p;
00178         return any;
00179       }
00180       
00181     };
00182     
00184     template<> 
00185     inline std::vector<float> Any::as<std::vector<float> >() const{
00186       const size_t l = std::string::length();
00187       if(l < sizeof(int)) throw ICLException("cannot convert Any to std::vector<float> size must be at least sizeof(int)");
00188       const icl8u *p = (const icl8u*)&std::string::operator[](0);
00189       int size = *((const int*)p);
00190       p += sizeof(int);
00191       if(l != sizeof(int) + sizeof(float) * size){
00192         throw ICLException("error converting Any to std::vector<float> unexpected size");
00193       }
00194       return std::vector<float>((const float*)p, ((const float*)p) + size);
00195     }
00196 
00197     template<>
00198     inline Any::Any(const std::vector<float> &v){
00199       std::string::resize(sizeof(int) + v.size() * sizeof(float));
00200       icl8u *p = (icl8u*)&std::string::operator[](0);
00201       *((int*)p) = v.size();
00202       memcpy(p+sizeof(int),v.data(), v.size()*sizeof(float));
00203     }
00204 
00205     template<> 
00206     inline std::vector<int> Any::as<std::vector<int> >() const{
00207       const size_t l = std::string::length();
00208       if(l < sizeof(int)) throw ICLException("cannot convert Any to std::vector<int> size must be at least sizeof(int)");
00209       const icl8u *p = (const icl8u*)&std::string::operator[](0);
00210       int size = *((const int*)p);
00211       p += sizeof(int);
00212       if(l != sizeof(int) + sizeof(int) * size){
00213         throw ICLException("error converting Any to std::vector<int> unexpected size");
00214       }
00215       return std::vector<int>((const int*)p, ((const int*)p) + size);
00216     }
00217 
00218     template<>
00219     inline Any::Any(const std::vector<int> &v){
00220       std::string::resize(sizeof(int) + v.size() * sizeof(int));
00221       icl8u *p = (icl8u*)&std::string::operator[](0);
00222       *((int*)p) = v.size();
00223       memcpy(p+sizeof(int),v.data(), v.size()*sizeof(int));
00224     }
00225 
00229   } // namespace utils
00230 }
00231 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines