00001 /*************************************************************************** 00002 * * 00003 * (c) Art Tevs, MPI Informatik Saarbruecken * 00004 * mailto: <tevs@mpi-sb.mpg.de> * 00005 * * 00006 * This program is free software; you can redistribute it and/or modify * 00007 * it under the terms of the GNU General Public License as published by * 00008 * the Free Software Foundation; either version 2 of the License, or * 00009 * (at your option) any later version. * 00010 * * 00011 ***************************************************************************/ 00012 00013 #ifndef _NR_VARIADIC_ARGUMENT_H 00014 #define _NR_VARIADIC_ARGUMENT_H 00015 00016 //---------------------------------------------------------------------------------- 00017 // Includes 00018 //---------------------------------------------------------------------------------- 00019 #include "Prerequisities.h" 00020 00021 namespace nrEngine { 00022 00023 //! VariadicArgument - Class helping you to pass variable number of parameters to a scripting function 00024 /** 00025 * VariadicArgument - This class is usefull if you want to pass variable parameters to 00026 * a function of a script. You can surely easy use the calling function, 00027 * by providing a vector containing the data, but you have to create the vector 00028 * before. Herewith you just write (Class << data1 << data2 ... ) to create 00029 * temporary a list of contained data and pass it as an argument 00030 * 00031 * The variadic argument object could also be used to retrieve a resulting 00032 * value from a function. Such a result value would contain more than one 00033 * result which can then be casted to any type. 00034 * 00035 * \ingroup gp 00036 **/ 00037 class _NRExport VarArg { 00038 00039 public: 00040 00041 //! Default constructor 00042 VarArg(); 00043 00044 //! Constructor getting any element 00045 VarArg(const boost::any& p); 00046 00047 //! Copyconstructor 00048 VarArg(const VarArg& v); 00049 00050 //! Fill the list with new element 00051 VarArg& operator, (const boost::any&); 00052 00053 //! Get access to certain element 00054 boost::any& operator[](int index); 00055 00056 //! Get number of elements stored here 00057 uint32 size() const { return count; } 00058 00059 //! Stack based function to retrieve the elements form the list 00060 boost::any pop_front(); 00061 00062 //! Stack based function to retrieve the elements form the list 00063 boost::any pop_back(); 00064 00065 //! Empty the resulting list 00066 void empty(); 00067 00068 //! Push new elements into the argument list 00069 void push_back(const boost::any&); 00070 00071 //! Get an element of casted to a certain type 00072 template<class T> T get(int index){ 00073 return cast<T>(_get(index)); 00074 } 00075 00076 //! Cast the element to a certain type 00077 template<class T> T cast(const boost::any& p){ 00078 // we want to copy to the same type, so do nothing 00079 //if (p.type() == typeid(T) || typeid(T) == typeid(boost::any)) 00080 // return (T)p; 00081 00082 // in other cases, do any_cast and hope it works 00083 return boost::any_cast<T>(p); 00084 } 00085 00086 //! Convert the variable argument list into a vector of certain type 00087 template<class T> void convert(std::vector<T>& v) const { 00088 v.clear(); 00089 args::const_iterator it = mArgs.begin(); 00090 for (int32 i=0; i < count; it++,i++){ 00091 00092 // we want to copy to the same type, so do nothing 00093 if (it->type() == typeid(T)){ 00094 v.push_back(*it); 00095 00096 // we want to copy to type "any", so simple conversion 00097 }else if (typeid(T) == typeid(boost::any)){ 00098 boost::any t = *it; 00099 v.push_back(t); 00100 00101 // in other cases, do any_cast and hope it works 00102 }else{ 00103 v.push_back (boost::any_cast<T>(*it)); 00104 } 00105 00106 } 00107 //return v; 00108 } 00109 00110 private: 00111 00112 //! Type of the list we store the data in 00113 typedef std::list<boost::any> args; 00114 00115 //! here we store our data 00116 args mArgs; 00117 00118 //! here we store the number of elements 00119 int32 count; 00120 00121 //! Get access to certain element 00122 boost::any& _get(int index); 00123 00124 }; 00125 00126 }; 00127 00128 #endif