PLearn 0.1
ObjectConversions.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // ObjectConversions.h
00004 //
00005 // Copyright (C) 2005 Nicolas Chapados 
00006 // 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 // 
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************      
00036    * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 
00037    ******************************************************* */
00038 
00039 // Authors: Nicolas Chapados
00040 
00044 #ifndef ObjectConversions_INC
00045 #define ObjectConversions_INC
00046 
00047 // From PLearn
00048 #include <plearn/base/Array.h>
00049 #include <plearn/base/TypeTraits.h>
00050 
00051 // From Boost
00052 #include <boost/type_traits.hpp>
00053 
00054 namespace PLearn {
00055 
00056 class Object;
00057 
00058 
00059 //#####  isConvertibleToObjectPtr  ############################################
00060 
00065 template <class T>
00066 inline bool isConvertibleToObjectPtr(const T&)
00067 {
00068     return boost::is_convertible< typename boost::remove_cv<T>::type,
00069                                   const Object* >::value
00070         || boost::is_convertible< typename boost::remove_cv<T>::type*,
00071                                   const Object* >::value ;
00072 }
00073 
00074 template <class T>
00075 inline bool isConvertibleToObjectPtr(const PP<T>& x)
00076 {
00077     return isConvertibleToObjectPtr((T*)0);
00078 }
00079 
00080 template <class T>
00081 inline bool isConvertibleToObjectPtr(const Array<T>& x)
00082 {
00083     return isConvertibleToObjectPtr((T*)0);
00084 }
00085 
00086 template <class T>
00087 inline bool isConvertibleToObjectPtr(const TVec<T>& x)
00088 {
00089     return isConvertibleToObjectPtr((T*)0);
00090 }
00091 
00092 template <class T>
00093 inline bool isConvertibleToObjectPtr(const Array< PP<T> >& x)
00094 {
00095     return isConvertibleToObjectPtr((T*)0);
00096 }
00097 
00098 template <class T>
00099 inline bool isConvertibleToObjectPtr(const TVec< PP<T> >& x)
00100 {
00101     return isConvertibleToObjectPtr((T*)0);
00102 }
00103 
00104 
00105 //#####  indexableObjectSize  #################################################
00106 
00121 template <class T>
00122 inline int indexableObjectSize(const T& x)
00123 {
00124     return 0;
00125 }
00126 
00127 template <class T>
00128 inline int indexableObjectSize(const Array<T>& x)
00129 {
00130     return (x.size() > 0? x.size() : -1);
00131 }
00132 
00133 template <class T>
00134 inline int indexableObjectSize(const TVec<T>& x)
00135 {
00136     return (x.size() > 0? x.size() : -1);
00137 }
00138 
00139 
00140 //#####  toObjectPtr  #########################################################
00141 
00142 // Hack to work with earlier versions of Boost
00143 typedef boost::is_convertible<int,int>::type  boost_true_type;
00144 typedef boost::is_convertible<void,int>::type boost_false_type;
00145 
00146 template <class T>
00147 Object* toObjectPtrImpl(const T&, const boost_false_type&)
00148 {
00149     PLERROR("Attempting to perform impossible conversion from type '%s' to Object*",
00150             TypeTraits<T>::name().c_str());
00151     return 0;
00152 }
00153 
00154 template <class T>
00155 Object* toObjectPtrImpl(const T& x, const boost_true_type&)
00156 {
00157     return const_cast<Object*>(static_cast<const Object*>(x));
00158 }
00159 
00160 
00169 template<class T>
00170 inline Object* toObjectPtr(const T& x)
00171 {
00172     typedef typename boost::remove_cv<T>::type T_nocv;
00173     typedef typename boost::is_convertible<T_nocv,  const Object*> T_isconv;
00174     typedef typename boost::is_convertible<T_nocv*, const Object*> pT_isconv;
00175 
00176     if (pT_isconv::value)
00177         return toObjectPtrImpl(&x, pT_isconv());
00178     else
00179         // May end up in the PLerror branch -- this is wanted
00180         return toObjectPtrImpl(x, T_isconv());
00181 }
00182 
00183 template<class T>
00184 inline Object* toObjectPtr(const PP<T>& x)
00185 {
00186     if (x.isNotNull())
00187         return toObjectPtr(*static_cast<T *>(x));
00188     else
00189         return 0;
00190     
00191 }
00192 
00193 
00194 //#####  toIndexedObjectPtr  ##################################################
00195 
00201 template<class T>
00202 Object* toIndexedObjectPtr(const Array<T> &x, int i)
00203 {
00204     return toObjectPtr(static_cast<T &>(x[i]));
00205 }
00206 
00207 template<class T>
00208 Object* toIndexedObjectPtr(const TVec<T> &x, int i)
00209 {
00210     return toObjectPtr(static_cast<T &>(x[i]));
00211 }
00212 
00213 template<class T>
00214 Object *toIndexedObjectPtr(const T&, int) // Never to be called stub
00215 {
00216     PLERROR("toIndexedObjectPtr() - Object is not indexable"); return 0;
00217 }
00218 
00219 } // end of namespace PLearn
00220 
00221 #endif
00222 
00223 
00224 /*
00225   Local Variables:
00226   mode:c++
00227   c-basic-offset:4
00228   c-file-style:"stroustrup"
00229   c-file-offsets:((innamespace . 0)(inline-open . 0))
00230   indent-tabs-mode:nil
00231   fill-column:79
00232   End:
00233 */
00234 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines