Advertisement
Guest User

Untitled

a guest
Feb 19th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.44 KB | None | 0 0
  1. #pragma once
  2. // Copyright (c) 2011, Alf P. Steinbach
  3.  
  4.  
  5. //--------------------------------------------------------- Dependencies:
  6.  
  7. #include <progrock/cpp/u/natural_encoding.h>                // CPP_NATURAL_ENCODING, RawEncodingUnitFor
  8. #include <progrock/cpp/c++11_emulation.h>                   // CPP_STATIC_ASSERT, CPP_NOEXCEPT
  9. #include <progrock/cpp/size_types.h>                        // cpp::Size, cpp::bitsPerByte
  10.  
  11. #include <locale>       // std::char_traits
  12. #include <utility>      // comparison operators
  13.  
  14.  
  15. //--------------------------------------------------------- Interface:
  16.  
  17. // You might '#define U CPP_U' within a separately compiled file, for convenience.
  18. // There is some risk that a macro U might conflict with e.g. template parameter.
  19.  
  20. #if     CPP_NATURAL_ENCODING == CPP_ENCODING_UTF16
  21.     CPP_STATIC_ASSERT( sizeof( wchar_t ) == 2 );        // E.g. Windows.
  22. #   define CPP_U_ENCODING ::progrock::cpp::u::NaturalEncoding::utf16
  23. #   define CPP_U( aLiteral ) ::progrock::cpp::u::typed( L##aLiteral )
  24. #elif   CPP_NATURAL_ENCODING == CPP_ENCODING_UTF8
  25. #   define CPP_U_ENCODING ::progrock::cpp::u::NaturalEncoding::utf8
  26. #   define CPP_U( aLiteral ) ::progrock::cpp::u::typed( aLiteral )
  27. #else
  28. #   error "The natural encoding for this OS is not supported, sorry."
  29. #endif
  30.  
  31. namespace progrock { namespace cpp { namespace u {
  32.     using namespace std::rel_ops;       // operator!= etc.
  33.  
  34.     NaturalEncoding::Enum const    encoding    = CPP_U_ENCODING;
  35.  
  36.     //typedef detail::EncodingTraits< encoding >  Traits;
  37.     //
  38.     // Using the traits here brings in too much indirection for Visual C++ to handle:
  39.     //
  40.     //typedef Traits::Raw::Unit              RawEncodingUnit;
  41.     //typedef Traits::Raw::ExtendedUnit      RawExtendedEncodingUnit;
  42.     //typedef Traits::Unit                   EncodingUnit;
  43.     //typedef Traits::ExtendedUnit           ExtendedEncodingUnit;
  44.  
  45.     typedef RawEncodingUnitFor< encoding >::Type            RawEncodingUnit;
  46.     typedef std::char_traits<RawEncodingUnit>::int_type     RawExtendedEncodingUnit;
  47.     typedef enum: RawEncodingUnit {}                        EncodingUnit;
  48.     typedef enum: RawExtendedEncodingUnit {}                ExtendedEncodingUnit;
  49.  
  50.     CPP_STATIC_ASSERT( sizeof( EncodingUnit ) == sizeof( RawEncodingUnit ) );
  51.     CPP_STATIC_ASSERT( sizeof( ExtendedEncodingUnit ) == sizeof( RawExtendedEncodingUnit ) );
  52.  
  53.     inline RawEncodingUnit raw( EncodingUnit const v ) CPP_NOEXCEPT
  54.     {
  55.         return v;
  56.     }
  57.  
  58.     inline RawExtendedEncodingUnit raw( ExtendedEncodingUnit const v ) CPP_NOEXCEPT
  59.     {
  60.         return v;
  61.     }
  62.  
  63.     inline RawEncodingUnit* raw( EncodingUnit* p ) CPP_NOEXCEPT
  64.     {
  65.         return reinterpret_cast< RawEncodingUnit* >( p );
  66.     }
  67.  
  68.     inline RawEncodingUnit const* raw( EncodingUnit const* p ) CPP_NOEXCEPT
  69.     {
  70.         return reinterpret_cast< RawEncodingUnit const* >( p );
  71.     }
  72.  
  73.     template< Size size >
  74.     inline RawEncodingUnit (&raw( EncodingUnit (&s)[size] )) [size]
  75.     {
  76.         return reinterpret_cast< RawEncodingUnit (&)[size] >( s );
  77.     }
  78.  
  79.     template< Size size >
  80.     inline RawEncodingUnit const (&raw( EncodingUnit const (&s)[size] ) CPP_NOEXCEPT)[size]
  81.     {
  82.         return reinterpret_cast< RawEncodingUnit const (&)[size] >( s );
  83.     }
  84.  
  85.     enum Adl {};
  86.  
  87.     inline EncodingUnit typed( RawEncodingUnit const v ) CPP_NOEXCEPT
  88.     {
  89.         return EncodingUnit( v );
  90.     }
  91.  
  92.     inline EncodingUnit typed( RawEncodingUnit const v, Adl ) CPP_NOEXCEPT
  93.     {
  94.         return EncodingUnit( v );
  95.     }
  96.  
  97.     inline EncodingUnit* typed( RawEncodingUnit* const p ) CPP_NOEXCEPT
  98.     {
  99.         return reinterpret_cast< EncodingUnit* >( p );
  100.     }
  101.  
  102.     inline EncodingUnit* typed( RawEncodingUnit* const p, Adl ) CPP_NOEXCEPT
  103.     {
  104.         return reinterpret_cast< EncodingUnit* >( p );
  105.     }
  106.  
  107.     inline EncodingUnit const* typed( RawEncodingUnit const* const p ) CPP_NOEXCEPT
  108.     {
  109.         return reinterpret_cast< EncodingUnit const* >( p );
  110.     }
  111.  
  112.     inline EncodingUnit const* typed( RawEncodingUnit const* const p, Adl ) CPP_NOEXCEPT
  113.     {
  114.         return reinterpret_cast< EncodingUnit const* >( p );
  115.     }
  116.  
  117.     template< Size size >
  118.     inline EncodingUnit (&typed( RawEncodingUnit (&s)[size] ) CPP_NOEXCEPT)[size]
  119.     {
  120.         return reinterpret_cast< EncodingUnit (&)[size] >( s );
  121.     }
  122.  
  123.     template< Size size >
  124.     inline EncodingUnit (&typed( RawEncodingUnit (&s)[size], Adl ) CPP_NOEXCEPT)[size]
  125.     {
  126.         return reinterpret_cast< EncodingUnit (&)[size] >( s );
  127.     }
  128.  
  129.     template< Size size >
  130.     inline EncodingUnit const (&typed( RawEncodingUnit const (&s)[size] ) CPP_NOEXCEPT)[size]
  131.     {
  132.         return reinterpret_cast< EncodingUnit const (&)[size] >( s );
  133.     }
  134.  
  135.     template< Size size >
  136.     inline EncodingUnit const (&typed( RawEncodingUnit const (&s)[size], Adl ) CPP_NOEXCEPT)[size]
  137.     {
  138.         return reinterpret_cast< EncodingUnit const (&)[size] >( s );
  139.     }
  140.  
  141. } } }  // namespace progrock::cpp::u
  142.  
  143. namespace std {
  144.  
  145.     // Requirements specified by C++11 §21.2.1/1 table 62.
  146.     template<>
  147.     struct char_traits< ::progrock::cpp::u::EncodingUnit >
  148.     {
  149.     private:
  150.         typedef ::progrock::cpp::u::Adl             AdlCppU;
  151.         typedef ::progrock::cpp::u::RawEncodingUnit     RawEncodingUnit;
  152.  
  153.         static AdlCppU const    cppU    = AdlCppU();
  154.  
  155.     public:
  156.         typedef ::progrock::cpp::u::EncodingUnit            char_type;
  157.         typedef ::progrock::cpp::u::ExtendedEncodingUnit    int_type;
  158.  
  159.         typedef std::char_traits< RawEncodingUnit >  Std;
  160.  
  161.         typedef Std::off_type                  off_type;
  162.         typedef Std::pos_type                  pos_type;
  163.         typedef Std::state_type                state_type;
  164.  
  165.         static bool eq( char_type a, char_type b ) CPP_NOEXCEPT
  166.         { return (a == b); }
  167.  
  168.         static bool lt( char_type a, char_type b ) CPP_NOEXCEPT
  169.         { return (a < b); }
  170.  
  171.         static int compare( char_type const* s1, char_type const* s2, size_t n )
  172.         { return Std::compare( raw( s1 ), raw( s2 ), n ); }
  173.  
  174.         static size_t length( char_type const* s )
  175.         { return Std::length( raw( s ) ); }
  176.  
  177.         static char_type const* find( char_type const* s, size_t n, char_type const a )
  178.         { return typed( Std::find( raw( s ), n, raw( a ) ), cppU ); }
  179.  
  180.         static char_type* move( char_type* s1, char_type const* s2, size_t n )
  181.         { return typed( Std::move( raw( s1 ), raw( s2 ), n ), cppU ); }
  182.  
  183.         static char_type* copy( char_type* s1, char_type const* s2, size_t n )
  184.         { return typed( Std::copy( raw( s1 ), raw( s2 ), n ), cppU ); }
  185.  
  186.         static void assign( char_type& c1, char_type const c2 ) CPP_NOEXCEPT
  187.         { c1 = c2; }
  188.  
  189.         static char_type* assign( char_type* s, size_t n, char_type const a )
  190.         { return typed( Std::assign( raw( s ), n, raw( a ) ), cppU ); }
  191.  
  192.         static int_type not_eof( int_type const c ) CPP_NOEXCEPT
  193.         { return int_type( Std::not_eof( Std::int_type( c ) ) ); }
  194.  
  195.         static char_type to_char_type( int_type const c ) CPP_NOEXCEPT
  196.         { return typed( raw( c ), cppU ); }
  197.  
  198.         static int_type to_int_type( char_type const c ) CPP_NOEXCEPT
  199.         { return int_type( c ); }
  200.  
  201.         static bool eq_int_type( int_type const c1, int_type const c2 ) CPP_NOEXCEPT
  202.         { return (c1 == c2); }
  203.  
  204.         static int_type eof() CPP_NOEXCEPT
  205.         { return int_type( Std::eof() ); }
  206.     };
  207.  
  208. }  // namespace std
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement