GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


A Paste by godecho
#include <iostream>
#include <map>
#include <string>
#include <sstream>
 
using std::cout;
using std::endl;
using std::string;
using std::map;
using std::stringstream;
 
class LexCastable    // abstract caster
{
    public:
 
        virtual ~LexCastable()    {}
 
        virtual void set(const string& str)            = 0;
        virtual string get()                const    = 0;
};

template<class tType>
struct Can_sstream
{
    static void constraints(tType a)
    {
        stringstream sstream;
        sstream << a;
        sstream >> a;
    }
    
    Can_sstream()
    {
        void(*p)(tType) = constraints;
    }
}; 

template<typename tType>
class LexCaster    : public LexCastable
{
    public:
        
        LexCaster(tType* value)    :    m_pValue(value)
        {
            Can_sstream<tType>();        // Constraints for template parameters
        }
         
        ~LexCaster()    {}
 
        void set(const string& str)
        {
            stringstream converter;
            converter << str;
            converter >> (*m_pValue);
        }
 
        string get()    const
        {
            stringstream converter;
            converter << (*m_pValue);
            return converter.str();
        }
 
    private:
 
        tType* m_pValue;
};
 
// A sort of auto_ptr with a special operator=
// Implementation probably not complete, but the important 
// stuff set get it working is here. Easily extended.
// Fits in a register too =P
 
class LexCastablePtr
{
    public:
        
        LexCastablePtr()    :    m_ptr(0) {}
        
        template <class tType>
        LexCastablePtr(tType* value) : m_ptr(new LexCaster<tType>(value))    {}    // Needed for 'naked' pointers.
     
        ~LexCastablePtr()
        {
            delete m_ptr;
        }
     
        template <class tType>
        LexCastable* operator=(tType* value)
        {
            delete m_ptr;  // Needed incase you switch what it is pointing set
            return m_ptr = new LexCaster<tType>(value);
        }
        
        LexCastable* operator->()
        {
            return m_ptr;
        }
    
    private:

        LexCastable* m_ptr;

        void operator=(LexCastablePtr rhs)
        {
            // do nothing
        }
};

int main(int argc, char** argv)
{
    map<char*, LexCastablePtr> vars;

    int        int1    = 0;
    float    float1    = 0.1f;

    vars["int1"]    = &int1;
    vars["float1"]    = &float1;

    cout << "int1: "    << vars["int1"]->get()        << endl;
    cout << "float1: "    << vars["float1"]->get()    << endl;

    vars["int1"]->set("42");
    vars["float1"]->set("15.1");
    
    cout << "int1: "    << vars["int1"]->get()        << endl;
    cout << "float1: "    << vars["float1"]->get()    << endl;

    vars["int1"] = &float1;
 
    cout << "int1: "    << vars["int1"]->get()        << endl;

    return 0;
}