[ create a new paste ] login | about

Link: http://codepad.org/JgpRkecp    [ raw code | output | fork ]

C++, pasted on Sep 4:
#pragma once
// Copyright (c) 2011, Alf P. Steinbach


//--------------------------------------------------------- Dependencies:

#include <assert.h>
#include <string>
#include <sstream>
//#include "platform_adaption.h"


//--------------------------------------------------------- Interface:

namespace progrock{ namespace cppx{

    namespace narrow {
        using std::string;
        using std::ostringstream;

        template< class Type >
        inline string stringFrom( Type const& v );
    }    // namespace narrow

    namespace wide {
        using std::wstring;
        using std::wostringstream;

        template< class Type >
        inline wstring stringFrom( Type const& v );
    }    // namespace wide

    namespace detail {
        using std::basic_string;

        template< class Char >
        class S
        {
        private:
            typedef basic_string<Char>  String;
            String  s_;

            template< class Type >
            static String stringFrom( Type const& v );

            static Char const* stringFrom( Char const* s )
            {
                assert( s != 0 );
                return s;
            }

        public:
            template< class Type >
            S& operator<<( Type const& v )
            {
                s_ += stringFrom( v );
                return *this;
            }

            String const& str() const { return s_; }
            operator String const& () const { return str(); }

            Char const* cStr() const { return s_.c_str(); }
            operator Char const* () const { return cStr(); }
        };

        template<>
        template< class Type >
        std::string detail::S<char>::stringFrom( Type const& v )
        {
            return narrow::stringFrom( v );
        }

        template<>
        template< class Type >
        std::wstring detail::S<wchar_t>::stringFrom( Type const& v )
        {
            return wide::stringFrom( v );
        }
    }  // namespace detail

    namespace narrow {
        template< class Type >
        inline string stringFrom( Type const& v )
        {
            ostringstream   stream;

            stream << v;
            return stream.str();
        }

        template<>
        inline string stringFrom( string const& s )
        {
            return s;
        }

        typedef detail::S<char>     S;
    }  // namespace narrow

    namespace wide {
        template< class Type >
        inline wstring stringFrom( Type const& v )
        {
            wostringstream  stream;
            stream << v;
            return stream.str();
        }

        template<>
        inline wstring stringFrom( wstring const& s )
        {
            return s;
        }

        typedef detail::S<wchar_t>  S;
    }  // namespace wide

} }  // namespace progrock::cppx


Output:
1
Line 8: error: #pragma once in main file


Create a new paste based on this one


Comments: