Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members

to_string.h

00001 #ifndef s11n_TO_STRING_H_INCLUDED
00002 #define s11n_TO_STRING_H_INCLUDED 1
00003 // Author: stephan beal <stephan@s11n.net>
00004 // License: Public Domain
00005 
00006 #include <string>
00007 #include <iostream>
00008 #include <sstream>
00009 namespace s11n
00010 {
00011     /**
00012            to_string() and from_string() provide an interface for
00013            converting strings into arbitrary other types and vice
00014            versa.
00015 
00016            It supports any types which to which std::istream>>X and
00017            std::ostream<<X can be applied.
00018 
00019            One immediate use i can imagine for this class is
00020            converting arguments read in via the command-line or
00021            libreadline. Another is using it to simulate a multi-type
00022            std::map, where you store string representations of the
00023            data, which can be easily converted to the proper types via
00024            from_string().
00025 
00026            Tries to from_string() a value of type value_type
00027            from the string str.  Returns an object of type
00028            value_type, hopefully properly populated from the
00029            string, or errorVal if it cannot convert the value
00030            poperly.  As errorVal is necessarily
00031            client/case-specific, you must supply your own
00032            error value. The error value need not be an error
00033            at all: this parameter is often used as a default
00034            value when pulling objects out of (e.g.) 
00035            configuration object (with the intention being,
00036            "the one i ask for may not be there - in that case
00037            assume errorVal is the value i want").
00038 
00039            The default implementation supports any type for
00040            which iostreams would work, as it uses those to do
00041            it's dirty work.  You may, of course, specialize
00042            this class to de/serialize whatever you like. See the
00043            std::string specialization, below, for an example.
00044 
00045            Note that if you call from_string<T>(...) and T is
00046            one of std::string or char *, you will only get the
00047            first token from the string. To get around this,
00048            use the non-template forms, from_string( const
00049            std::string & ) and from_string( const char * ).
00050            This is a workaround until i learn how to properly
00051            specialize from_string<>() to handle string and
00052            (char *). i guess a Loki::TypeList would do the
00053            trick?
00054 
00055            This function is known to have problems with
00056            booleans:
00057 
00058            bool b = s11n::from_string( "foo", true );
00059 
00060            because bool is normally an int of some type and
00061            the compiler may complain about ambiguity or type
00062            loss or some such.
00063         */
00064     template <typename value_type>
00065         value_type from_string( const std::string & str, const value_type & errorVal )
00066     {
00067         std::istringstream is( str );
00068         if ( !is )
00069             return errorVal;
00070         value_type foo = value_type();
00071         if ( is >> foo )
00072               return foo;
00073           return errorVal;
00074     }
00075 
00076     /**
00077            A quasi-bogus overload to avoid the
00078            first-token-only problem of from_string<>().
00079         */
00080     std::string from_string( const char *str, const char *errorVal );
00081 
00082     /**
00083            A quasi-bogus overload to avoid the
00084            first-token-only problem of from_string<>().
00085         */
00086     std::string from_string( const std::string & str, const std::string & errorVal );
00087 
00088 
00089         /**
00090            Returns a string representation of the given object, which must be ostreamble.
00091         */
00092     template <typename value_type>
00093         std::string to_string( const value_type & obj )
00094     {
00095         std::ostringstream os;
00096                 os << std::fixed;
00097         os << obj;
00098         return os.str(  );
00099     }
00100 
00101         /**
00102            Overloader for strings-via-streams reasons.
00103         */
00104     std::string to_string( const char *obj );
00105         /**
00106            Overloader for strings-via-streams reasons.
00107         */
00108     std::string to_string( const std::string & obj );
00109 
00110 };              // namespace s11n
00111 
00112 #endif // s11n_TO_STRING_H_INCLUDED
00113 

Generated on Thu Jun 16 16:18:12 2005 for s11n by  doxygen 1.4.3-20050530