00001 /* 00002 This file is part of the CVD Library. 00003 00004 Copyright (C) 2005 The Authors 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free Software 00018 Foundation, Inc., 00019 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 #ifndef CVD_INTERNAL_IO_PARAMETER_H 00022 #define CVD_INTERNAL_IO_PARAMETER_H 00023 00024 #include <string> 00025 #include <typeinfo> 00026 #include <map> 00027 #include <memory> 00028 00029 namespace CVD { 00030 00031 #ifndef DOXYGEN_IGNORE_INTERNAL 00032 namespace Internal{ 00033 class UntypedParameter; 00034 } 00035 template<class C=void> class Parameter; 00036 00037 template<> class Parameter<Internal::UntypedParameter> 00038 { 00039 virtual const void* get_data_pointer() const=0; 00040 00041 public: 00042 virtual Parameter* duplicate() const=0; 00043 00044 }; 00045 #endif 00046 00054 template<class C> class Parameter: public Parameter<Internal::UntypedParameter> 00055 { 00056 private: 00057 C parameter; 00058 public: 00059 Parameter(const C& c) 00060 :parameter(c) 00061 { 00062 } 00063 00064 Parameter* duplicate() const 00065 { 00066 return new Parameter(*this); 00067 } 00068 00069 virtual const void* get_data_pointer() const 00070 { 00071 return ¶meter; 00072 } 00073 }; 00074 00075 template<> class Parameter<void> 00076 { 00077 private: 00078 std::auto_ptr<Parameter<Internal::UntypedParameter> > param; 00079 00080 public: 00081 Parameter() 00082 {} 00083 00084 Parameter(const Parameter& p) 00085 { 00086 if(p.param.get()) 00087 std::auto_ptr<Parameter<Internal::UntypedParameter> >(p.param->duplicate()); 00088 } 00089 00090 template<class C> Parameter(const Parameter<C>& p) 00091 :param(std::auto_ptr<Parameter<Internal::UntypedParameter> >(static_cast<Parameter<Internal::UntypedParameter>*>(new Parameter<C>(p)))) 00092 {} 00093 00094 template<class C> Parameter& operator=(const Parameter<C>& p) 00095 { 00096 param = std::auto_ptr<Parameter<Internal::UntypedParameter> >(static_cast<Parameter<Internal::UntypedParameter>*>(new Parameter<C>(p))); 00097 return *this; 00098 } 00099 00100 template<class C> bool is() const 00101 { 00102 if(param.get() == 0) 00103 return 0; 00104 00105 const Parameter<C>* p = dynamic_cast<const Parameter<C>* >(param.get()); 00106 return p != 0; 00107 } 00108 00109 template<class C> const C& get() const 00110 { 00111 if(param.get() == 0) 00112 throw std::bad_cast(); 00113 00114 const Parameter<C>& p = dynamic_cast<const Parameter<C>& >(*param); 00115 return *(const C*) p.get_data_pointer(); 00116 } 00117 }; 00118 00119 } 00120 #endif