blob: d9afcb5353782153901865eae8f8a983d1de9676 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// (C) Copyright Gennadiy Rozental 2004-2008.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5
6// See http://www.boost.org/libs/test for the library home page.
7//
8// File : $RCSfile$
9//
10// Version : $Revision: 49312 $
11//
12// Description : generic char traits class; wraps std::char_traits
13// ***************************************************************************
14
Jeff Thompson3d613fd2013-10-15 15:39:04 -070015#ifndef NDNBOOST_TEST_BCS_CHAR_TRAITS_HPP_071894GER
16#define NDNBOOST_TEST_BCS_CHAR_TRAITS_HPP_071894GER
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070017
18// Boost
19#include <ndnboost/config.hpp>
20#include <ndnboost/detail/workaround.hpp>
21#include <ndnboost/test/detail/config.hpp>
22#include <ndnboost/type_traits/add_const.hpp>
23
24// STL
25#include <string> // std::char_traits
26#include <cstddef> // std::size_t
27
28#include <ndnboost/test/detail/suppress_warnings.hpp>
29
30//____________________________________________________________________________//
31
32namespace ndnboost {
33
34namespace unit_test {
35
36namespace ut_detail {
37
38template<typename CharT> struct bcs_base_char { typedef CharT type; };
39
40template<> struct bcs_base_char<char const> { typedef char type; };
41template<> struct bcs_base_char<unsigned char> { typedef char type; };
Jeff Thompson3d613fd2013-10-15 15:39:04 -070042#if !NDNBOOST_WORKAROUND(__BORLANDC__, NDNBOOST_TESTED_AT(0x551))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070043template<> struct bcs_base_char<unsigned char const> { typedef char type; };
44#endif
45
46template<> struct bcs_base_char<wchar_t const> { typedef wchar_t type; };
47
48// ************************************************************************** //
49// ************** bcs_char_traits ************** //
50// ************************************************************************** //
51
52template<typename CharT>
53struct bcs_char_traits_impl
54{
Jeff Thompson3d613fd2013-10-15 15:39:04 -070055#if NDNBOOST_WORKAROUND(__BORLANDC__, NDNBOOST_TESTED_AT(0x564))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070056 typedef CharT const const_char;
57#else
58 typedef typename ndnboost::add_const<CharT>::type const_char;
59#endif
60 static bool eq( CharT c1, CharT c2 )
61 {
62 return c1 == c2;
63 }
64 static bool lt( CharT c1, CharT c2 )
65 {
66 return c1 < c2;
67 }
68
69 static int compare( const_char* cstr1, const_char* cstr2, std::size_t n )
70 {
71 while( n > 0 ) {
72 if( !eq( *cstr1, *cstr2 ) )
73 return lt( *cstr1, *cstr2 ) ? -1 : 1;
74 ++cstr1;
75 ++cstr2;
76 --n;
77 }
78
79 return 0;
80 }
81
82 static std::size_t length( const_char* cstr )
83 {
84 const_char null_char = CharT();
85
86 const_char* ptr = cstr;
87 while( !eq( *ptr, null_char ) )
88 ++ptr;
89
90 return ptr - cstr;
91 }
92
93 static const_char* find( const_char* s, std::size_t n, CharT c )
94 {
95 while( n > 0 ) {
96 if( eq( *s, c ) )
97 return s;
98
99 ++s;
100 --n;
101 }
102 return 0;
103 }
104};
105
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700106#ifdef NDNBOOST_CLASSIC_IOSTREAMS
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700107template<typename CharT>
108struct char_traits_with_find : std::string_char_traits<CharT> {
109 static CharT const* find( CharT const* s, std::size_t n, CharT c )
110 {
111 while( n > 0 ) {
112 if( eq( *s, c ) )
113 return s;
114
115 ++s;
116 --n;
117 }
118 return 0;
119 }
120};
121
122template<> struct bcs_char_traits_impl<char> : char_traits_with_find<char> {};
123template<> struct bcs_char_traits_impl<wchar_t> : char_traits_with_find<wchar_t> {};
124#else
125template<> struct bcs_char_traits_impl<char> : std::char_traits<char> {};
126template<> struct bcs_char_traits_impl<wchar_t> : std::char_traits<wchar_t> {};
127#endif
128
129template<typename CharT>
130class bcs_char_traits : public bcs_char_traits_impl<CharT> {
131 typedef typename ut_detail::bcs_base_char<CharT>::type the_base_char;
132public:
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700133#ifdef NDNBOOST_CLASSIC_IOSTREAMS
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700134 typedef std::basic_string<the_base_char, std::string_char_traits<the_base_char> > std_string;
135#else
136 typedef std::basic_string<the_base_char, std::char_traits<the_base_char> > std_string;
137#endif
138};
139
140} // namespace ut_detail
141
142} // namespace unit_test
143
144} // namespace ndnboost
145
146//____________________________________________________________________________//
147
148#include <ndnboost/test/detail/enable_warnings.hpp>
149
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700150#endif // NDNBOOST_TEST_BCS_CHAR_TRAITS_HPP_071894GER