summaryrefslogtreecommitdiff
path: root/zen/string_traits.h
blob: 59da2f79e660022b8ca1afbe9314e02eb6ee3d9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// **************************************************************************
// * This file is part of the zenXML project. It is distributed under the   *
// * Boost Software License, Version 1.0. See accompanying file             *
// * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.       *
// * Copyright (C) 2011 ZenJu (zhnmju123 AT gmx.de)                         *
// **************************************************************************

#ifndef STRING_TRAITS_HEADER_813274321443234
#define STRING_TRAITS_HEADER_813274321443234

#include "type_tools.h"
#include "assert_static.h"


//uniform access to string-like types: classes and character arrays
namespace zen
{
/*
strBegin():
	std::wstring str(L"dummy");
	char array[] = "dummy";
	const wchar_t* iter  = strBegin(str);   //returns str.c_str()
	const char*    iter2 = strBegin(array); //returns array

strLength():
	strLength(str);   //equals str.size()
	strLength(array); //equals cStringLength(array)

StringTraits<>::CharType:
	StringTraits<std::wstring>::CharType  //equals wchar_t
	StringTraits<wchar_t[5]>  ::CharType  //equals wchar_t

StringTraits<>::isStringLike:
    StringTraits<const wchar_t*>::isStringLike; //equals "true"
	StringTraits<const int*>    ::isStringLike; //equals "false"

StringTraits<>::isStringClass:
	StringTraits<std::wstring>::isStringClass  //equals "true"
	StringTraits<wchar_t[5]>  ::isStringClass  //equals "false"
*/













//---------------------- implementation ----------------------
namespace implementation
{
template<typename T>
class HasValueTypedef
{
    typedef char Yes[1];
    typedef char No [2];

    template <typename U> class HelperTp {};

    //detect presence of a member type called value_type
    template <class U> static Yes& hasMemberValueType(HelperTp<typename U::value_type>*);
    template <class U> static  No& hasMemberValueType(...);

public:
    enum { result = sizeof(hasMemberValueType<T>(NULL)) == sizeof(Yes)
         };
};


template<typename T, bool isClassType>
class HasStringMembers
{
public:
    enum { result = false };
};

template<typename T>
class HasStringMembers<T, true>
{
    typedef char Yes[1];
    typedef char No [2];

    //detect presence of member functions (without specific restriction on return type, within T or one of it's base classes)
    template <typename U, U t> class HelperFn {};

    struct Fallback
    {
        int c_str;
        int length;
    };

    template <class U>
    struct Helper2 : public U, public Fallback {}; //U must be a class-type!

    //we don't know the exact declaration of the member attribute (may be in base class), but we know what NOT to expect:
    template <class U> static  No& hasMemberCstr(HelperFn<int Fallback::*, &Helper2<U>::c_str>*);
    template <class U> static Yes& hasMemberCstr(...);

    template <class U> static  No& hasMemberLength(HelperFn<int Fallback::*, &Helper2<U>::length>*);
    template <class U> static Yes& hasMemberLength(...);
public:
    enum { result = sizeof(hasMemberCstr  <T>(NULL)) == sizeof(Yes) &&
                    sizeof(hasMemberLength<T>(NULL)) == sizeof(Yes)
         };
};

template <class S, bool isStringClass> struct StringTraits2 { typedef EmptyType Result; }; //"StringTraits2": fix some VS bug with namespace and partial template specialization

template <class S> struct StringTraits2<S,       true > { typedef typename S::value_type Result; };
template <>        struct StringTraits2<char,    false> { typedef char                   Result; };
template <>        struct StringTraits2<wchar_t, false> { typedef wchar_t                Result; };
}


template <class S>
struct StringTraits
{
private:
    typedef typename RemoveRef    <S           >::Result NonRefType;
    typedef typename RemoveConst  <NonRefType  >::Result NonConstType;
    typedef typename RemoveArray  <NonConstType>::Result NonArrayType;
    typedef typename RemovePointer<NonArrayType>::Result NonPtrType;
    typedef typename RemoveConst  <NonPtrType  >::Result UndecoratedType; //handle "const char* const"
public:
    enum
    {
        isStringClass = implementation::HasStringMembers<NonConstType, implementation::HasValueTypedef<NonConstType>::result>::result
    };

    typedef typename implementation::StringTraits2<UndecoratedType, isStringClass>::Result CharType;

    enum
    {
        isStringLike = IsSameType<CharType, char>::result || IsSameType<CharType, wchar_t>::result
    };
};


namespace implementation
{
template <class C> inline
size_t cStringLength(const C* str) //strlen()
{
    assert_static((IsSameType<C, char>::result || IsSameType<C, wchar_t>::result));
    size_t len = 0;
    while (*str++ != 0)
        ++len;
    return len;
}
}


template <class S> inline
const typename StringTraits<S>::CharType* strBegin(const S& str, typename S::value_type dummy = 0) { return str.c_str(); } //SFINAE: T must be a "string"

template <class Char>
inline const typename StringTraits<Char>::CharType* strBegin(const Char* str) { return str; }
inline const char*    strBegin(const char& ch)    { return &ch; }
inline const wchar_t* strBegin(const wchar_t& ch) { return &ch; }


template <class S> inline
size_t strLength(const S& str, typename S::value_type dummy = 0) { return str.length(); } //SFINAE: T must be a "string"

template <class Char>
inline size_t strLength(const Char* str) { return implementation::cStringLength(str); }
inline size_t strLength(char)            { return 1; }
inline size_t strLength(wchar_t)         { return 1; }
}

#endif //STRING_TRAITS_HEADER_813274321443234
bgstack15