epoc32/include/stdapis/boost/lexical_cast.hpp
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
williamr@2
     1
#ifndef BOOST_LEXICAL_CAST_INCLUDED
williamr@2
     2
#define BOOST_LEXICAL_CAST_INCLUDED
williamr@2
     3
williamr@2
     4
// Boost lexical_cast.hpp header  -------------------------------------------//
williamr@2
     5
//
williamr@2
     6
// See http://www.boost.org for most recent version including documentation.
williamr@2
     7
// See end of this header for rights and permissions.
williamr@2
     8
//
williamr@2
     9
// what:  lexical_cast custom keyword cast
williamr@2
    10
// who:   contributed by Kevlin Henney,
williamr@2
    11
//        enhanced with contributions from Terje Slettebų,
williamr@2
    12
//        with additional fixes and suggestions from Gennaro Prota,
williamr@2
    13
//        Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
williamr@2
    14
//        and other Boosters
williamr@2
    15
// when:  November 2000, March 2003, June 2005
williamr@2
    16
williamr@2
    17
#include <cstddef>
williamr@2
    18
#include <string>
williamr@2
    19
#include <typeinfo>
williamr@2
    20
#include <boost/config.hpp>
williamr@2
    21
#include <boost/limits.hpp>
williamr@2
    22
#include <boost/throw_exception.hpp>
williamr@2
    23
#include <boost/type_traits/is_pointer.hpp>
williamr@2
    24
williamr@2
    25
#ifdef BOOST_NO_STRINGSTREAM
williamr@2
    26
#include <strstream>
williamr@2
    27
#else
williamr@2
    28
#include <sstream>
williamr@2
    29
#endif
williamr@2
    30
williamr@2
    31
#if defined(BOOST_NO_STRINGSTREAM) || \
williamr@2
    32
    defined(BOOST_NO_STD_WSTRING) || \
williamr@2
    33
    defined(BOOST_NO_STD_LOCALE) 
williamr@2
    34
#define DISABLE_WIDE_CHAR_SUPPORT
williamr@2
    35
#endif
williamr@2
    36
williamr@2
    37
namespace boost
williamr@2
    38
{
williamr@2
    39
    // exception used to indicate runtime lexical_cast failure
williamr@2
    40
    class bad_lexical_cast : public std::bad_cast
williamr@2
    41
    {
williamr@2
    42
    public:
williamr@2
    43
        bad_lexical_cast() :
williamr@2
    44
        source(&typeid(void)), target(&typeid(void))
williamr@2
    45
        {
williamr@2
    46
        }
williamr@2
    47
        bad_lexical_cast(
williamr@2
    48
            const std::type_info &source_type,
williamr@2
    49
            const std::type_info &target_type) :
williamr@2
    50
            source(&source_type), target(&target_type)
williamr@2
    51
        {
williamr@2
    52
        }
williamr@2
    53
        const std::type_info &source_type() const
williamr@2
    54
        {
williamr@2
    55
            return *source;
williamr@2
    56
        }
williamr@2
    57
        const std::type_info &target_type() const
williamr@2
    58
        {
williamr@2
    59
            return *target;
williamr@2
    60
        }
williamr@2
    61
        virtual const char *what() const throw()
williamr@2
    62
        {
williamr@2
    63
            return "bad lexical cast: "
williamr@2
    64
                   "source type value could not be interpreted as target";
williamr@2
    65
        }
williamr@2
    66
        virtual ~bad_lexical_cast() throw()
williamr@2
    67
        {
williamr@2
    68
        }
williamr@2
    69
    private:
williamr@2
    70
        const std::type_info *source;
williamr@2
    71
        const std::type_info *target;
williamr@2
    72
    };
williamr@2
    73
williamr@2
    74
    namespace detail // selectors for choosing stream character type
williamr@2
    75
    {
williamr@2
    76
        template<typename Type>
williamr@2
    77
        struct stream_char
williamr@2
    78
        {
williamr@2
    79
            typedef char type;
williamr@2
    80
        };
williamr@2
    81
williamr@2
    82
        #ifndef DISABLE_WIDE_CHAR_SUPPORT
williamr@2
    83
#if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
williamr@2
    84
        template<>
williamr@2
    85
        struct stream_char<wchar_t>
williamr@2
    86
        {
williamr@2
    87
            typedef wchar_t type;
williamr@2
    88
        };
williamr@2
    89
#endif
williamr@2
    90
williamr@2
    91
        template<>
williamr@2
    92
        struct stream_char<wchar_t *>
williamr@2
    93
        {
williamr@2
    94
            typedef wchar_t type;
williamr@2
    95
        };
williamr@2
    96
williamr@2
    97
        template<>
williamr@2
    98
        struct stream_char<const wchar_t *>
williamr@2
    99
        {
williamr@2
   100
            typedef wchar_t type;
williamr@2
   101
        };
williamr@2
   102
williamr@2
   103
        template<>
williamr@2
   104
        struct stream_char<std::wstring>
williamr@2
   105
        {
williamr@2
   106
            typedef wchar_t type;
williamr@2
   107
        };
williamr@2
   108
        #endif
williamr@2
   109
williamr@2
   110
        template<typename TargetChar, typename SourceChar>
williamr@2
   111
        struct widest_char
williamr@2
   112
        {
williamr@2
   113
            typedef TargetChar type;
williamr@2
   114
        };
williamr@2
   115
williamr@2
   116
        template<>
williamr@2
   117
        struct widest_char<char, wchar_t>
williamr@2
   118
        {
williamr@2
   119
            typedef wchar_t type;
williamr@2
   120
        };
williamr@2
   121
    }
williamr@2
   122
    
williamr@2
   123
    namespace detail // stream wrapper for handling lexical conversions
williamr@2
   124
    {
williamr@2
   125
        template<typename Target, typename Source>
williamr@2
   126
        class lexical_stream
williamr@2
   127
        {
williamr@2
   128
        private:
williamr@2
   129
            typedef typename widest_char<
williamr@2
   130
                typename stream_char<Target>::type,
williamr@2
   131
                typename stream_char<Source>::type>::type char_type;
williamr@2
   132
williamr@2
   133
        public:
williamr@2
   134
            lexical_stream()
williamr@2
   135
            {
williamr@2
   136
                stream.unsetf(std::ios::skipws);
williamr@2
   137
williamr@2
   138
                if(std::numeric_limits<Target>::is_specialized)
williamr@2
   139
                    stream.precision(std::numeric_limits<Target>::digits10 + 1);
williamr@2
   140
                else if(std::numeric_limits<Source>::is_specialized)
williamr@2
   141
                    stream.precision(std::numeric_limits<Source>::digits10 + 1);
williamr@2
   142
            }
williamr@2
   143
            ~lexical_stream()
williamr@2
   144
            {
williamr@2
   145
                #if defined(BOOST_NO_STRINGSTREAM)
williamr@2
   146
                stream.freeze(false);
williamr@2
   147
                #endif
williamr@2
   148
            }
williamr@2
   149
            bool operator<<(const Source &input)
williamr@2
   150
            {
williamr@2
   151
                return !(stream << input).fail();
williamr@2
   152
            }
williamr@2
   153
            template<typename InputStreamable>
williamr@2
   154
            bool operator>>(InputStreamable &output)
williamr@2
   155
            {
williamr@2
   156
                return !is_pointer<InputStreamable>::value &&
williamr@2
   157
                       stream >> output &&
williamr@2
   158
                       stream.get() ==
williamr@2
   159
#if defined(__GNUC__) && (__GNUC__<3) && defined(BOOST_NO_STD_WSTRING)
williamr@2
   160
// GCC 2.9x lacks std::char_traits<>::eof().
williamr@2
   161
// We use BOOST_NO_STD_WSTRING to filter out STLport and libstdc++-v3
williamr@2
   162
// configurations, which do provide std::char_traits<>::eof().
williamr@2
   163
    
williamr@2
   164
                           EOF;
williamr@2
   165
#else
williamr@2
   166
                           std::char_traits<char_type>::eof();
williamr@2
   167
#endif
williamr@2
   168
            }
williamr@2
   169
            bool operator>>(std::string &output)
williamr@2
   170
            {
williamr@2
   171
                #if defined(BOOST_NO_STRINGSTREAM)
williamr@2
   172
                stream << '\0';
williamr@2
   173
                #endif
williamr@2
   174
                output = stream.str();
williamr@2
   175
                return true;
williamr@2
   176
            }
williamr@2
   177
            #ifndef DISABLE_WIDE_CHAR_SUPPORT
williamr@2
   178
            bool operator>>(std::wstring &output)
williamr@2
   179
            {
williamr@2
   180
                output = stream.str();
williamr@2
   181
                return true;
williamr@2
   182
            }
williamr@2
   183
            #endif
williamr@2
   184
        private:
williamr@2
   185
            #if defined(BOOST_NO_STRINGSTREAM)
williamr@2
   186
            std::strstream stream;
williamr@2
   187
            #elif defined(BOOST_NO_STD_LOCALE)
williamr@2
   188
            std::stringstream stream;
williamr@2
   189
            #else
williamr@2
   190
            std::basic_stringstream<char_type> stream;
williamr@2
   191
            #endif
williamr@2
   192
        };
williamr@2
   193
    }
williamr@2
   194
williamr@2
   195
    #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
williamr@2
   196
williamr@2
   197
    // call-by-const reference version
williamr@2
   198
williamr@2
   199
    namespace detail
williamr@2
   200
    {
williamr@2
   201
        template<class T>
williamr@2
   202
        struct array_to_pointer_decay
williamr@2
   203
        {
williamr@2
   204
            typedef T type;
williamr@2
   205
        };
williamr@2
   206
williamr@2
   207
        template<class T, std::size_t N>
williamr@2
   208
        struct array_to_pointer_decay<T[N]>
williamr@2
   209
        {
williamr@2
   210
            typedef const T * type;
williamr@2
   211
        };
williamr@2
   212
    }
williamr@2
   213
williamr@2
   214
    template<typename Target, typename Source>
williamr@2
   215
    Target lexical_cast(const Source &arg)
williamr@2
   216
    {
williamr@2
   217
        typedef typename detail::array_to_pointer_decay<Source>::type NewSource;
williamr@2
   218
williamr@2
   219
        detail::lexical_stream<Target, NewSource> interpreter;
williamr@2
   220
        Target result;
williamr@2
   221
williamr@2
   222
        if(!(interpreter << arg && interpreter >> result))
williamr@2
   223
            throw_exception(bad_lexical_cast(typeid(NewSource), typeid(Target)));
williamr@2
   224
        return result;
williamr@2
   225
    }
williamr@2
   226
williamr@2
   227
    #else
williamr@2
   228
williamr@2
   229
    // call-by-value fallback version (deprecated)
williamr@2
   230
williamr@2
   231
    template<typename Target, typename Source>
williamr@2
   232
    Target lexical_cast(Source arg)
williamr@2
   233
    {
williamr@2
   234
        detail::lexical_stream<Target, Source> interpreter;
williamr@2
   235
        Target result;
williamr@2
   236
williamr@2
   237
        if(!(interpreter << arg && interpreter >> result))
williamr@2
   238
            throw_exception(bad_lexical_cast(typeid(Source), typeid(Target)));
williamr@2
   239
        return result;
williamr@2
   240
    }
williamr@2
   241
williamr@2
   242
    #endif
williamr@2
   243
}
williamr@2
   244
williamr@2
   245
// Copyright Kevlin Henney, 2000-2005. All rights reserved.
williamr@2
   246
//
williamr@2
   247
// Distributed under the Boost Software License, Version 1.0. (See
williamr@2
   248
// accompanying file LICENSE_1_0.txt or copy at
williamr@2
   249
// http://www.boost.org/LICENSE_1_0.txt)
williamr@2
   250
williamr@2
   251
#undef DISABLE_WIDE_CHAR_SUPPORT
williamr@2
   252
#endif