00001 /** @file tcl/conversions.h tcl conversion functions for basic types */ 00002 00003 /////////////////////////////////////////////////////////////////////// 00004 // 00005 // Copyright (c) 2001-2004 California Institute of Technology 00006 // Copyright (c) 2004-2007 University of Southern California 00007 // Rob Peters <rjpeters at usc dot edu> 00008 // 00009 // created: Wed Jul 11 08:57:31 2001 00010 // commit: $Id: conversions.h 11876 2009-10-22 15:53:06Z icore $ 00011 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/tcl/conversions.h $ 00012 // 00013 // -------------------------------------------------------------------- 00014 // 00015 // This file is part of GroovX 00016 // [http://ilab.usc.edu/rjpeters/groovx/] 00017 // 00018 // GroovX is free software; you can redistribute it and/or modify it 00019 // under the terms of the GNU General Public License as published by 00020 // the Free Software Foundation; either version 2 of the License, or 00021 // (at your option) any later version. 00022 // 00023 // GroovX is distributed in the hope that it will be useful, but 00024 // WITHOUT ANY WARRANTY; without even the implied warranty of 00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00026 // General Public License for more details. 00027 // 00028 // You should have received a copy of the GNU General Public License 00029 // along with GroovX; if not, write to the Free Software Foundation, 00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00031 // 00032 /////////////////////////////////////////////////////////////////////// 00033 00034 #ifndef GROOVX_TCL_CONVERSIONS_H_UTC20050628162420_DEFINED 00035 #define GROOVX_TCL_CONVERSIONS_H_UTC20050628162420_DEFINED 00036 00037 #include "rutz/traits.h" 00038 #include "tcl/obj.h" 00039 00040 typedef struct Tcl_Obj Tcl_Obj; 00041 00042 namespace rutz 00043 { 00044 class fstring; 00045 class value; 00046 template <class T> class fwd_iter; 00047 } 00048 00049 namespace nub 00050 { 00051 template <class T> class ref; 00052 template <class T> class soft_ref; 00053 } 00054 00055 namespace tcl 00056 { 00057 class obj; 00058 00059 /// Trait class for extracting an appropriate return-type from T. 00060 template <class T> 00061 struct returnable 00062 { 00063 // This machinery is simple to set up the rule that we want to 00064 // convert all rutz::value subclasses via strings. All other types 00065 // are converted directly. 00066 typedef typename rutz::select_if< 00067 rutz::is_sub_super<T, rutz::value>::result, 00068 rutz::fstring, T>::result_t 00069 type; 00070 }; 00071 00072 /// Specialization of tcl::returnable for const T. 00073 template <class T> 00074 struct returnable<const T> 00075 { 00076 typedef typename rutz::select_if< 00077 rutz::is_sub_super<T, rutz::value>::result, 00078 rutz::fstring, T>::result_t 00079 type; 00080 }; 00081 00082 /// Specialization of tcl::returnable for const T&. 00083 template <class T> 00084 struct returnable<const T&> 00085 { 00086 typedef typename rutz::select_if< 00087 rutz::is_sub_super<T, rutz::value>::result, 00088 rutz::fstring, T>::result_t 00089 type; 00090 }; 00091 00092 // 00093 // Functions for converting from Tcl objects to C++ types. 00094 // 00095 00096 /// Convert a tcl::obj to a native c++ object. 00097 /** Will select a matching aux_convert_to() overload. NOTE! Due to 00098 two-phase name lookup, this convert_to() definition must occur 00099 before ALL aux_convert_to() overloads. */ 00100 template <class T> 00101 inline typename returnable<T>::type convert_to( const tcl::obj& obj ) 00102 { 00103 return aux_convert_to(obj.get(), static_cast<typename returnable<T>::type*>(0)); 00104 } 00105 00106 /// Convert a tcl::obj to a native c++ object. 00107 /** Will select a matching aux_convert_to() overload. NOTE! Due to 00108 two-phase name lookup, this convert_to() definition must occur 00109 before ALL aux_convert_to() overloads. */ 00110 template <class T> 00111 inline typename returnable<T>::type convert_to( Tcl_Obj* obj ) 00112 { 00113 return aux_convert_to(obj, static_cast<typename returnable<T>::type*>(0)); 00114 } 00115 00116 // Note that the trailing pointer params are not actually used, they 00117 // are simply present to allow overload selection. See e.g. how 00118 // aux_convert_to() is called below in the implementation of 00119 // convert_to(). 00120 00121 int aux_convert_to(Tcl_Obj* obj, int*); 00122 unsigned int aux_convert_to(Tcl_Obj* obj, unsigned int*); 00123 long aux_convert_to(Tcl_Obj* obj, long*); 00124 unsigned long aux_convert_to(Tcl_Obj* obj, unsigned long*); 00125 long long aux_convert_to(Tcl_Obj* obj, long long*); 00126 bool aux_convert_to(Tcl_Obj* obj, bool*); 00127 double aux_convert_to(Tcl_Obj* obj, double*); 00128 float aux_convert_to(Tcl_Obj* obj, float*); 00129 const char* aux_convert_to(Tcl_Obj* obj, const char**); 00130 rutz::fstring aux_convert_to(Tcl_Obj* obj, rutz::fstring*); 00131 00132 inline 00133 Tcl_Obj* aux_convert_to(Tcl_Obj* obj, Tcl_Obj**) 00134 { return obj; } 00135 00136 inline 00137 tcl::obj aux_convert_to(Tcl_Obj* obj, tcl::obj*) 00138 { return tcl::obj(obj); } 00139 00140 // 00141 // Functions for converting from C++ types to Tcl objects. 00142 // 00143 00144 /// Convert a native c++ object to a tcl::obj. 00145 /** Will select a matching aux_convert_from() overload. NOTE! Due to 00146 two-phase name lookup, this convert_from() definition must occur 00147 before ALL aux_convert_from() overloads. */ 00148 template <class T> 00149 inline tcl::obj convert_from(const T& val) 00150 { 00151 return aux_convert_from(val); 00152 } 00153 00154 tcl::obj aux_convert_from(long long val); 00155 tcl::obj aux_convert_from(long val); 00156 tcl::obj aux_convert_from(unsigned long val); 00157 tcl::obj aux_convert_from(int val); 00158 tcl::obj aux_convert_from(unsigned int val); 00159 tcl::obj aux_convert_from(unsigned char val); 00160 tcl::obj aux_convert_from(bool val); 00161 tcl::obj aux_convert_from(double val); 00162 tcl::obj aux_convert_from(float val); 00163 tcl::obj aux_convert_from(const char* val); 00164 tcl::obj aux_convert_from(const rutz::fstring& val); 00165 tcl::obj aux_convert_from(const rutz::value& v); 00166 00167 inline 00168 tcl::obj aux_convert_from(Tcl_Obj* val) 00169 { return tcl::obj(val); } 00170 00171 inline 00172 tcl::obj aux_convert_from(tcl::obj val) 00173 { return val; } 00174 } 00175 00176 static const char __attribute__((used)) vcid_groovx_tcl_conversions_h_utc20050628162420[] = "$Id: conversions.h 11876 2009-10-22 15:53:06Z icore $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/tcl/conversions.h $"; 00177 #endif // !GROOVX_TCL_CONVERSIONS_H_UTC20050628162420_DEFINED