fork download
  1. #pragma once
  2. // Copyright (c) 2011, 2012 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/data/Size.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. typedef RawEncodingUnit RawCh; // For associative mnemonic value, although misleading!
  51. typedef EncodingUnit Ch; // For associative mnemonic value, although misleading!
  52.  
  53.  
  54. CPP_STATIC_ASSERT( sizeof( EncodingUnit ) == sizeof( RawEncodingUnit ) );
  55. CPP_STATIC_ASSERT( sizeof( ExtendedEncodingUnit ) == sizeof( RawExtendedEncodingUnit ) );
  56.  
  57. namespace detail {
  58. inline RawEncodingUnit raw( EncodingUnit const v ) CPP_NOEXCEPT
  59. {
  60. return v;
  61. }
  62.  
  63. inline RawExtendedEncodingUnit raw( ExtendedEncodingUnit const v ) CPP_NOEXCEPT
  64. {
  65. return v;
  66. }
  67.  
  68. template< class TpEncodingUnit > // Templating to de-emphasize re overload resolution.
  69. inline RawEncodingUnit* raw( TpEncodingUnit* p ) CPP_NOEXCEPT;
  70.  
  71. template<>
  72. inline RawEncodingUnit* raw<EncodingUnit>( EncodingUnit* p ) CPP_NOEXCEPT
  73. {
  74. return reinterpret_cast< RawEncodingUnit* >( p );
  75. }
  76.  
  77. template< class TpEncodingUnit > // Templating to de-emphasize re overload resolution.
  78. inline RawEncodingUnit const* raw( TpEncodingUnit const* p ) CPP_NOEXCEPT;
  79.  
  80. template<>
  81. inline RawEncodingUnit const* raw<EncodingUnit>( EncodingUnit const* p ) CPP_NOEXCEPT
  82. {
  83. return reinterpret_cast< RawEncodingUnit const* >( p );
  84. }
  85.  
  86. inline EncodingUnit typed( RawEncodingUnit const v ) CPP_NOEXCEPT
  87. {
  88. return EncodingUnit( v );
  89. }
  90.  
  91. inline EncodingUnit* typed( RawEncodingUnit* const p ) CPP_NOEXCEPT
  92. {
  93. return reinterpret_cast< EncodingUnit* >( p );
  94. }
  95.  
  96. inline EncodingUnit const* typed( RawEncodingUnit const* const p ) CPP_NOEXCEPT
  97. {
  98. return reinterpret_cast< EncodingUnit const* >( p );
  99. }
  100. } // namespace detail
  101.  
  102. template< Size size >
  103. inline RawEncodingUnit (&raw( EncodingUnit (&s)[size] )) [size]
  104. {
  105. return reinterpret_cast< RawEncodingUnit (&)[size] >( s );
  106. }
  107.  
  108. template< Size size >
  109. inline RawEncodingUnit const (&raw( EncodingUnit const (&s)[size] ) CPP_NOEXCEPT)[size]
  110. {
  111. return reinterpret_cast< RawEncodingUnit const (&)[size] >( s );
  112. }
  113.  
  114. template< size_t size >
  115. inline EncodingUnit (&typed( RawEncodingUnit (&s)[size] ) CPP_NOEXCEPT)[size]
  116. {
  117. return reinterpret_cast< EncodingUnit (&)[size] >( s );
  118. }
  119.  
  120. template< size_t size >
  121. inline EncodingUnit const (&typed( RawEncodingUnit const (&s)[size] ) CPP_NOEXCEPT)[size]
  122. {
  123. return reinterpret_cast< EncodingUnit const (&)[size] >( s );
  124. }
  125.  
  126. template< class Arg >
  127. inline auto raw( Arg&& arg ) -> decltype( detail::raw( arg ) )
  128. {
  129. return detail::raw( arg );
  130. }
  131.  
  132. template< class Arg >
  133. inline auto typed( Arg&& arg ) -> decltype( detail::typed( arg ) )
  134. {
  135. return detail::typed( arg );
  136. }
  137.  
  138. enum Adl {};
  139.  
  140. inline EncodingUnit typed( RawEncodingUnit const v, Adl ) CPP_NOEXCEPT
  141. {
  142. return EncodingUnit( v );
  143. }
  144.  
  145. inline EncodingUnit* typed( RawEncodingUnit* const p, Adl ) CPP_NOEXCEPT
  146. {
  147. return reinterpret_cast< EncodingUnit* >( p );
  148. }
  149.  
  150. inline EncodingUnit const* typed( RawEncodingUnit const* const p, Adl ) CPP_NOEXCEPT
  151. {
  152. return reinterpret_cast< EncodingUnit const* >( p );
  153. }
  154.  
  155. template< size_t size >
  156. inline EncodingUnit (&typed( RawEncodingUnit (&s)[size], Adl ) CPP_NOEXCEPT)[size]
  157. {
  158. return reinterpret_cast< EncodingUnit (&)[size] >( s );
  159. }
  160.  
  161. template< size_t size >
  162. inline EncodingUnit const (&typed( RawEncodingUnit const (&s)[size], Adl ) CPP_NOEXCEPT)[size]
  163. {
  164. return reinterpret_cast< EncodingUnit const (&)[size] >( s );
  165. }
  166.  
  167. } } } // namespace progrock::cpp::u
  168.  
  169. namespace std {
  170.  
  171. // Requirements specified by C++11 §21.2.1/1 table 62.
  172. template<>
  173. struct char_traits< ::progrock::cpp::u::EncodingUnit >
  174. {
  175. private:
  176. typedef ::progrock::cpp::u::Adl AdlCppU;
  177. typedef ::progrock::cpp::u::RawEncodingUnit RawEncodingUnit;
  178.  
  179. static AdlCppU const cppU = AdlCppU();
  180.  
  181. public:
  182. typedef ::progrock::cpp::u::EncodingUnit char_type;
  183. typedef ::progrock::cpp::u::ExtendedEncodingUnit int_type;
  184.  
  185. typedef std::char_traits< RawEncodingUnit > Std;
  186.  
  187. typedef Std::off_type off_type;
  188. typedef Std::pos_type pos_type;
  189. typedef Std::state_type state_type;
  190.  
  191. static bool eq( char_type a, char_type b ) CPP_NOEXCEPT
  192. { return (a == b); }
  193.  
  194. static bool lt( char_type a, char_type b ) CPP_NOEXCEPT
  195. { return (a < b); }
  196.  
  197. static int compare( char_type const* s1, char_type const* s2, size_t n )
  198. { return Std::compare( raw( s1 ), raw( s2 ), n ); }
  199.  
  200. static size_t length( char_type const* s )
  201. { return Std::length( raw( s ) ); }
  202.  
  203. static char_type const* find( char_type const* s, size_t n, char_type const a )
  204. { return typed( Std::find( raw( s ), n, raw( a ) ), cppU ); }
  205.  
  206. static char_type* move( char_type* s1, char_type const* s2, size_t n )
  207. { return typed( Std::move( raw( s1 ), raw( s2 ), n ), cppU ); }
  208.  
  209. static char_type* copy( char_type* s1, char_type const* s2, size_t n )
  210. { return typed( Std::copy( raw( s1 ), raw( s2 ), n ), cppU ); }
  211.  
  212. static void assign( char_type& c1, char_type const c2 ) CPP_NOEXCEPT
  213. { c1 = c2; }
  214.  
  215. static char_type* assign( char_type* s, size_t n, char_type const a )
  216. { return typed( Std::assign( raw( s ), n, raw( a ) ), cppU ); }
  217.  
  218. static int_type not_eof( int_type const c ) CPP_NOEXCEPT
  219. { return int_type( Std::not_eof( Std::int_type( c ) ) ); }
  220.  
  221. static char_type to_char_type( int_type const c ) CPP_NOEXCEPT
  222. { return typed( raw( c ), cppU ); }
  223.  
  224. static int_type to_int_type( char_type const c ) CPP_NOEXCEPT
  225. { return int_type( c ); }
  226.  
  227. static bool eq_int_type( int_type const c1, int_type const c2 ) CPP_NOEXCEPT
  228. { return (c1 == c2); }
  229.  
  230. static int_type eof() CPP_NOEXCEPT
  231. { return int_type( Std::eof() ); }
  232. };
  233.  
  234. } // namespace std
  235.  
  236. namespace progrock { namespace cpp { namespace u {
  237.  
  238. typedef std::char_traits< Ch > ChTraits;
  239.  
  240. } } } // namespace progrock::cpp::u
  241.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:9: warning: #pragma once in main file
prog.cpp:7:104: error: progrock/cpp/u/natural_encoding.h: No such file or directory
prog.cpp:8:95: error: progrock/cpp/c++11_emulation.h: No such file or directory
prog.cpp:9:84: error: progrock/cpp/data/Size.h: No such file or directory
prog.cpp:127: warning: identifier ‘decltype’ will become a keyword in C++0x
prog.cpp:21: error: expected constructor, destructor, or type conversion before ‘(’ token
prog.cpp:34: error: ‘NaturalEncoding’ has not been declared
prog.cpp:34: error: expected constructor, destructor, or type conversion before ‘const’
prog.cpp:45: error: expected initializer before ‘<’ token
prog.cpp:46: error: ‘RawEncodingUnit’ was not declared in this scope
prog.cpp:46: error: template argument 1 is invalid
prog.cpp:46: error: expected initializer before ‘RawExtendedEncodingUnit’
prog.cpp:47: error: expected identifier before ‘:’ token
prog.cpp:47: error: expected unqualified-id before ‘:’ token
prog.cpp:47: error: expected constructor, destructor, or type conversion before ‘;’ token
prog.cpp:48: error: expected identifier before ‘:’ token
prog.cpp:48: error: expected unqualified-id before ‘:’ token
prog.cpp:48: error: expected constructor, destructor, or type conversion before ‘;’ token
prog.cpp:50: error: ‘RawEncodingUnit’ does not name a type
prog.cpp:51: error: ‘EncodingUnit’ does not name a type
prog.cpp:54: error: expected constructor, destructor, or type conversion before ‘(’ token
prog.cpp:55: error: expected constructor, destructor, or type conversion before ‘(’ token
prog.cpp:58: error: ‘RawEncodingUnit’ does not name a type
prog.cpp:63: error: ‘RawExtendedEncodingUnit’ does not name a type
prog.cpp:69: error: expected initializer before ‘*’ token
prog.cpp:72: error: expected initializer before ‘*’ token
prog.cpp:78: error: expected initializer before ‘const’
prog.cpp:81: error: expected initializer before ‘const’
prog.cpp:86: error: ‘EncodingUnit’ does not name a type
prog.cpp:91: error: expected initializer before ‘*’ token
prog.cpp:96: error: expected initializer before ‘const’
prog.cpp:102: error: ‘Size’ has not been declared
prog.cpp:103: error: ISO C++ forbids declaration of ‘RawEncodingUnit’ with no type
prog.cpp:103: error: ‘progrock::cpp::u::RawEncodingUnit’ declared as an ‘inline’ variable
prog.cpp:103: error: template declaration of ‘int progrock::cpp::u::RawEncodingUnit’
prog.cpp:103: error: ‘s’ was not declared in this scope
prog.cpp:103: error: there are no arguments to ‘EncodingUnit’ that depend on a template parameter, so a declaration of ‘EncodingUnit’ must be available
prog.cpp:103: error: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
prog.cpp:108: error: ‘Size’ has not been declared
prog.cpp:109: error: expected initializer before ‘const’
prog.cpp:115: error: ISO C++ forbids declaration of ‘EncodingUnit’ with no type
prog.cpp:115: error: ‘progrock::cpp::u::EncodingUnit’ declared as an ‘inline’ variable
prog.cpp:115: error: template declaration of ‘int progrock::cpp::u::EncodingUnit’
prog.cpp:115: error: ‘s’ was not declared in this scope
prog.cpp:115: error: there are no arguments to ‘RawEncodingUnit’ that depend on a template parameter, so a declaration of ‘RawEncodingUnit’ must be available
prog.cpp:115: error: expected `)' before ‘CPP_NOEXCEPT’
prog.cpp:121: error: expected initializer before ‘const’
prog.cpp:127: error: expected ‘,’ or ‘...’ before ‘&&’ token
prog.cpp:127: error: expected initializer before ‘->’ token
prog.cpp:133: error: expected ‘,’ or ‘...’ before ‘&&’ token
prog.cpp:133: error: expected initializer before ‘->’ token
prog.cpp:140: error: ‘EncodingUnit’ does not name a type
prog.cpp:145: error: expected initializer before ‘*’ token
prog.cpp:150: error: expected initializer before ‘const’
prog.cpp:156: error: ISO C++ forbids declaration of ‘EncodingUnit’ with no type
prog.cpp:156: error: ‘progrock::cpp::u::EncodingUnit’ declared as an ‘inline’ variable
prog.cpp:156: error: template declaration of ‘int progrock::cpp::u::EncodingUnit’
prog.cpp:156: error: ‘s’ was not declared in this scope
prog.cpp:156: error: there are no arguments to ‘RawEncodingUnit’ that depend on a template parameter, so a declaration of ‘RawEncodingUnit’ must be available
prog.cpp:156: error: expected primary-expression before ‘)’ token
prog.cpp:156: error: expected `)' before ‘CPP_NOEXCEPT’
prog.cpp:162: error: expected initializer before ‘const’
prog.cpp:173: error: ‘EncodingUnit’ is not a member of ‘progrock::cpp::u’
prog.cpp:173: error: ‘EncodingUnit’ is not a member of ‘progrock::cpp::u’
prog.cpp:173: error: template argument 1 is invalid
prog.cpp:238: error: ‘Ch’ was not declared in this scope
prog.cpp:238: error: template argument 1 is invalid
prog.cpp:238: error: invalid type in declaration before ‘;’ token
stdout
Standard output is empty