ObjCreator.H

00001 /*! create objects from a ParamList */
00002 //////////////////////////////////////////////////////////////////////////
00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00004 // University of Southern California (USC) and the iLab at USC.         //
00005 // See http://iLab.usc.edu for information about this project.          //
00006 //////////////////////////////////////////////////////////////////////////
00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00009 // in Visual Environments, and Applications'' by Christof Koch and      //
00010 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00011 // pending; application number 09/912,225 filed July 23, 2001; see      //
00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00013 //////////////////////////////////////////////////////////////////////////
00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00015 //                                                                      //
00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00017 // redistribute it and/or modify it under the terms of the GNU General  //
00018 // Public License as published by the Free Software Foundation; either  //
00019 // version 2 of the License, or (at your option) any later version.     //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00024 // PURPOSE.  See the GNU General Public License for more details.       //
00025 //                                                                      //
00026 // You should have received a copy of the GNU General Public License    //
00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00029 // Boston, MA 02111-1307 USA.                                           //
00030 //////////////////////////////////////////////////////////////////////////
00031 //
00032 // Primary maintainer for this file: David J. Berg <dberg@usc.edu>
00033 // $HeadURL:svn://ilab.usc.edu/trunk/saliency/src/GenericUtils/ObjCreator.H$
00034 
00035 #ifndef UTIL_OBJCREATOR_H_DEFINED
00036 #define UTIL_OBJCREATOR_H_DEFINED
00037 
00038 #ifdef INVT_USE_CPPOX//we need c++ 0X features for this to work
00039 
00040 #include "GenericUtils/ParamList.H"
00041 //######################################################################
00042 //ObjCreator - create an object from a paramlist
00043 //
00044 //Usage:
00045 //
00046 //  //create an object from a paramlist
00047 //  ParamList<float, int> pl(4.4f, 5);
00048 //  auto pl1 = make_paramlist(1.0f, 0);
00049 //  Circle * c = ObjCreator<Circle>::create(pl);
00050 //  LINFO("%3.2f, %d", c->f, c->p);
00051 //  c = ObjCreator<Circle>::create(pl1);
00052 //  LINFO("%3.2f, %d", c->f, c->p);
00053 //
00054 // Note: see interesting implementation note below
00055 //######################################################################
00056 template <class T, class ConvertType = T>
00057 class ObjCreator
00058 {
00059 public:
00060   template <typename... Params>
00061   static T* create(const ParamList<Params...>& paramlist)
00062   {
00063     //if all the types in the paramlist are guarnteed to be different 
00064     //then this single line:
00065     //
00066     //T* t = new T(at<Params>(paramlist)...);
00067     //
00068     //replaces all the nasty specializations we have below. It works
00069     //great. Unfortunatly, the at<> function in ParamList.H will
00070     //always match the first type, so if there are repeats we won't
00071     //get the correct parameters. I can't seem to come up with any
00072     //reasonable tricks to get the expansion to keep track of who its
00073     //matched.... One could still make it work if they typeified there
00074     //types so that each was unique, but this is a whole other pain I
00075     //don't want to deal with.
00076     T* t = creator<ParamList<Params...>::length, T>::create(paramlist);
00077     ConvertType* ct = dynamic_cast<ConvertType*>(t);
00078     return (ct == NULL) ? NULL : ct;
00079   };
00080   
00081 private:
00082   //This private class does all the real work and ObjCreator just
00083   //serves as an interface. The parameters stored in the ParamList are
00084   //passed to T's constructor. Only the specializations that
00085   //corresponds to the correct param list length will get compiled.
00086   template <uint num, class U> struct creator;
00087   
00088   template <class U> struct creator<0, U> {
00089     template<typename... Params>
00090     static U* create(const ParamList<Params...>& pl)
00091     { return new U(); }; };
00092   
00093   template <class U> struct creator<1, U> {
00094     template<typename... Params>
00095     static U* create(const ParamList<Params...>& pl)
00096     {return new U(ParamListHelper::at<0>(pl)); }; };
00097   
00098   template <class U> struct creator<2, U> {
00099     template<typename... Params>
00100     static U* create(const ParamList<Params...>& pl)
00101     { using namespace ParamListHelper;
00102       return new U(at<0>(pl),at<1>(pl)); }; };
00103   
00104     template <class U> struct creator<3, U> {
00105       template<typename... Params>
00106       static U* create(const ParamList<Params...>& pl)
00107       { using namespace ParamListHelper;
00108         return new U(at<0>(pl),at<1>(pl),at<2>(pl)); }; };
00109   
00110   template <class U> struct creator<4, U> {
00111     template<typename... Params>
00112     static U* create(const ParamList<Params...>& pl)
00113     { using namespace ParamListHelper;
00114       return new U(at<0>(pl),at<1>(pl),at<2>(pl),at<3>(pl)); }; };
00115   
00116   template <class U> struct creator<5, U> {
00117     template<typename... Params>
00118     static U* create(const ParamList<Params...>& pl)
00119     { using namespace ParamListHelper;
00120       return new U(at<0>(pl),at<1>(pl),at<2>(pl),at<3>(pl),at<4>(pl)); }; };
00121 
00122   template <class U> struct creator<6, U> {
00123     template<typename... Params>
00124     static U* create(const ParamList<Params...>& pl)
00125     { using namespace ParamListHelper;
00126       return new U(at<0>(pl),at<1>(pl),at<2>(pl),at<3>(pl),at<4>(pl),
00127                    at<5>(pl)); }; };
00128 
00129   template <class U> struct creator<7, U> {
00130     template<typename... Params>
00131     static U* create(const ParamList<Params...>& pl)
00132     { using namespace ParamListHelper;
00133       return new U(at<0>(pl),at<1>(pl),at<2>(pl),at<3>(pl),at<4>(pl),
00134                    at<5>(pl), at<6>(pl)); }; };
00135 
00136   template <class U> struct creator<8, U> {
00137     template<typename... Params>
00138     static U* create(const ParamList<Params...>& pl)
00139     { using namespace ParamListHelper;
00140       return new U(at<0>(pl),at<1>(pl),at<2>(pl),at<3>(pl),at<4>(pl),
00141                    at<5>(pl),at<6>(pl),at<7>(pl)); }; };
00142 
00143   template <class U> struct creator<9, U> {
00144     template<typename... Params>
00145     static U* create(const ParamList<Params...>& pl)
00146     { using namespace ParamListHelper;
00147       return new U(at<0>(pl),at<1>(pl),at<2>(pl),at<3>(pl),at<4>(pl),
00148                    at<5>(pl),at<6>(pl),at<7>(pl),at<8>(pl)); }; };
00149 
00150   template <class U> struct creator<10, U> {
00151     template<typename... Params>
00152     static U* create(const ParamList<Params...>& pl)
00153     { using namespace ParamListHelper;
00154       return new U(at<0>(pl),at<1>(pl),at<2>(pl),at<3>(pl),at<4>(pl),
00155                    at<5>(pl),at<6>(pl),at<7>(pl),at<8>(pl),at<9>(pl)); }; };
00156 
00157   template <class U> struct creator<11, U> {
00158     template<typename... Params>
00159     static U* create(const ParamList<Params...>& pl)
00160     { using namespace ParamListHelper;
00161       return new U(at<0>(pl),at<1>(pl),at<2>(pl),at<3>(pl),at<4>(pl),
00162                    at<5>(pl),at<6>(pl),at<7>(pl),at<8>(pl),at<9>(pl),
00163                    at<10>(pl)); }; };
00164 
00165   template <class U> struct creator<12, U> {
00166     template<typename... Params>
00167     static U* create(const ParamList<Params...>& pl)
00168     { using namespace ParamListHelper;
00169       return new U(at<0>(pl),at<1>(pl),at<2>(pl),at<3>(pl),at<4>(pl),
00170                    at<5>(pl),at<6>(pl),at<7>(pl),at<8>(pl),at<9>(pl),
00171                    at<10>(pl),at<11>(pl)); }; };
00172 
00173   template <class U> struct creator<13, U> {
00174     template<typename... Params>
00175     static U* create(const ParamList<Params...>& pl)
00176     { using namespace ParamListHelper;
00177       return new U(at<0>(pl),at<1>(pl),at<2>(pl),at<3>(pl),at<4>(pl),
00178                    at<5>(pl),at<6>(pl),at<7>(pl),at<8>(pl),at<9>(pl),
00179                    at<10>(pl),at<11>(pl),at<12>(pl)); }; };
00180 
00181   template <class U> struct creator<14, U> {
00182     template<typename... Params>
00183     static U* create(const ParamList<Params...>& pl)
00184     { using namespace ParamListHelper;
00185       return new U(at<0>(pl),at<1>(pl),at<2>(pl),at<3>(pl),at<4>(pl),
00186                    at<5>(pl),at<6>(pl),at<7>(pl),at<8>(pl),at<9>(pl),
00187                    at<10>(pl),at<11>(pl),at<12>(pl),at<13>(pl)); }; };
00188 
00189 
00190   template <class U> struct creator<15, U> {
00191     template<typename... Params>
00192     static U* create(const ParamList<Params...>& pl)
00193     { using namespace ParamListHelper;
00194       return new U(at<0>(pl),at<1>(pl),at<2>(pl),at<3>(pl),at<4>(pl),
00195                    at<5>(pl),at<6>(pl),at<7>(pl),at<8>(pl),at<9>(pl),
00196                    at<10>(pl),at<11>(pl),at<12>(pl),at<13>(pl), at<15>(pl)); }; };
00197 };
00198 
00199 #endif //INVT_USE_CPPOX
00200 #endif//UTIL_OBJCREATOR_H_DEFINED
Generated on Sun May 8 08:40:39 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3