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 Anonymous
#ifndef _STRINGCASTER_H__
#define _STRINGCASTER_H__

///////////////////////////////////////////////////////////////////////

#include <string>
#include <sstream>

///////////////////////////////////////////////////////////////////////

class StringCaster {
    std::string str;
    static std::stringstream tempStrStream;
public:
    // ctor
    StringCaster() {}
    StringCaster( std::string t ) : str(t) {}
    // cctor
    StringCaster( const StringCaster& sc ) : str(sc.str) {}

    StringCaster& operator=( StringCaster& sc ) { 
        str = sc.str;
        return *this;
    }
    StringCaster& operator=( std::string& s ) { 
        str = s;
        return *this;
    }
    template< class T >
        T& To() { 
            static T output;    
            tempStrStream.clear();
            tempStrStream.str( str );        
            tempStrStream >> output;
            return output;
        }
    template<>
        bool& To() {
            static bool output;
            output = (str=="true"?true:false);
            return output;
        }
    template<>
        std::string& To() {
            return str;
        }
};

std::stringstream StringCaster::tempStrStream;

///////////////////////////////////////////////////////////////////////

#endif