C++ (Qt)// внутренняя переменная OpenGL с именем name будет установлена в значение valvoid SetFloatGL( const char * name, float val ); // для массива intvoid SetIntArrayGL( const char * name, const int * src, int count ); и.т.п.
C++ (Qt)struct CVarGL { CVarGL( int type = type_None ) : mType(type) {} virtual CVarGL( void ) {} static void SetInt( const char * name, int val ); static void SetFloat( const char * name, float val ); static void SetIntArray( const char * name, int * val, size_t count ); int mType; static std::map <const char *, CVarGL *> mMap;}; struct CVarIntGL : public CVarGL { CVarIntGL( int val ) : CVarGL(type_Int), mData(val) {} int mData;}; // и.т.д (подробности легко понятны)
C++ (Qt)class Var{public: virtual void apply() = 0;}; class VarInt : public Var{public: VarInt( int val ); virtual void apply(); private: int m_val;}; class VarFloat : public Var{public: VarInt( float val ); virtual void apply(); private: float m_val;}; // ------------------------------// Заполнили командыstd::list<Var*> varList; varList.push_back( new VarInt( 10 ) );varList.push_back( new VarFloat( 123.45 ) );... // ------------------------------// Испольнили командыforeach( Var *v, varList ){ v->apply()}
C++ (Qt)class Var{public: virtual void apply() = 0;};
C++ (Qt)template <class T, size_t type> struct CVar : public СVarGL { CVar( void ) { mType = type; } T mData;};
C++ (Qt)class VarInt : public Var{public: VarInt( const std::string &name, int val ) : m_name( name ), m_val( val ) {} virtual void apply() { SetIntGL( m_name.c_str(), m_val ); }private: std::string m_name; int m_val;};
C++ (Qt)#include <iostream>#include <list> void setIntGL( const char *name, int val ){ std::cout << "Call setIntGL( " << name << ", " << val << " )" << std::endl;} void setFloatGL( const char *name, float val ){ std::cout << "Call setFloatGL( " << name << ", " << val << " )" << std::endl;} class Var{public: virtual void apply() = 0;}; template<typename T, void (*func)( const char *, T )>class NumVar : public Var{public: NumVar( const std::string &name, T val ) : m_name( name ), m_val( val ) {} virtual void apply() { (*func)( m_name.c_str(), m_val ); } private: std::string m_name; T m_val;}; int main ( int argc, char *argv[] ){ typedef std::list<Var*> VarList; VarList varList; varList.push_back( new NumVar<int, setIntGL>( "Var1", 10 ) ); varList.push_back( new NumVar<float, setFloatGL>( "Var2", 10.123 ) ); for( VarList::iterator it = varList.begin(); it != varList.end(); ++it ) (*it)->apply(); }
C++ (Qt)template<typename T, void (*func)( const char *, T )>
C++ (Qt)#include <QtGui>#include <map>#include <cassert> struct CAbstractVar { virtual ~CAbstractVar( void ) {}}; template <class T> struct CVar : public CAbstractVar { T mData;}; typedef std::map <const char *, CAbstractVar *> TVarMap; template <class T>T & GetVar( TVarMap & theMap, const char * name ) { TVarMap::iterator it = theMap.find(name); if (it != theMap.end()) { assert(&typeid(*(it->second)) == &typeid(CVar <T>));// assert(dynamic_cast <CVar <T> *> (it->second) != 0); // или так return ((CVar <T> *) it->second)->mData; } else { CVar <T> * ptr = new CVar <T>; theMap[name] = ptr; return ptr->mData; } } int main(int argc, char **argv){ (void) argc; (void) argv; TVarMap theMap; GetVar<int> (theMap, "Int1") = 1; int test = GetVar<int> (theMap, "Int1"); GetVar<float> (theMap, "Float1") = 2; int test2 = GetVar<float> (theMap, "Int1"); // проверка на вылет return 0;}