summaryrefslogtreecommitdiff
path: root/zenXml/zenxml/cvrt_text.h
blob: b4f6fa2d41623648c124ceaf08b400054031d97b (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under    *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0          *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************

#ifndef CVRT_TEXT_H_018727339083427097434
#define CVRT_TEXT_H_018727339083427097434

#include <chrono>
//#include <zen/utf.h>
#include <zen/string_tools.h>


namespace zen
{
/**
\file
\brief Handle conversion of string-convertible types to and from std::string.

It is \b not required to call these functions directly. They are implicitly used by zen::XmlElement::getValue(),
zen::XmlElement::setValue(), zen::XmlElement::getAttribute() and zen::XmlElement::setAttribute().
\n\n
Conversions for the following user types are supported by default:
    - strings - std::string, std::wstring, char*, wchar_t*, char, wchar_t, etc..., all STL-compatible-string-classes
    - numbers - int, double, float, bool, long, etc..., all built-in numbers
    - STL containers - std::map, std::set, std::vector, std::list, etc..., all STL-compatible-containers
    - std::pair

You can add support for additional types via template specialization. \n\n
Specialize zen::readStruc() and zen::writeStruc() to enable conversion from structured user types to XML elements.
Specialize zen::readText() and zen::writeText() to enable conversion from string-convertible user types to std::string.
Prefer latter if possible since it does not only enable conversions from XML elements to user data, but also from and to XML attributes.
\n\n
<b> Example: </b> type "bool"
\code
namespace zen
{
template <> inline
void writeText(const bool& value, std::string& output)
{
    output = value ? "true" : "false";
}

template <> inline
bool readText(const std::string& input, bool& value)
{
    const std::string tmp = trimCpy(input);
    if (tmp == "true")
        value = true;
    else if (tmp == "false")
        value = false;
    else
        return false;
    return true;
}
}
\endcode
*/


///Convert text to user data - used by XML elements and attributes
/**
  \param input Input text.
  \param value Conversion target value.
  \return "true" if value was read successfully.
*/
template <class T> bool readText(const std::string& input, T& value);
///Convert user data into text - used by XML elements and attributes
/**
  \param value The value to be converted.
  \param output Output text.
*/
template <class T> void writeText(const T& value, std::string& output);


/* Different classes of data types:

-----------------------------
| structured                |  readStruc/writeStruc - e.g. string-convertible types, STL containers, std::pair, structured user types
| ------------------------- |
| | to-string-convertible | |  readText/writeText   - e.g. string-like types, all built-in arithmetic numbers, bool
| | ---------------       | |
| | | string-like |       | |  utfTo                - e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ...
| | ---------------       | |
| ------------------------- |
-----------------------------
*/















//------------------------------ implementation -------------------------------------
template <class T>
struct IsChronoDuration
{
private:
    using Yes = char[1];
    using No  = char[2];

    template <class Rep, class Period>
    static Yes& isDuration(std::chrono::duration<Rep, Period>);
    static  No& isDuration(...);
public:
    enum { value = sizeof(isDuration(std::declval<T>())) == sizeof(Yes) };
};


//Conversion from arbitrary types to text (for use with XML elements and attributes)
enum class TextType
{
    boolean,
    number,
    chrono,
    string,
    other,
};

template <class T>
struct GetTextType : std::integral_constant<TextType,
    std::is_same_v<T, bool>    ? TextType::boolean :
    isStringLike<T>           ? TextType::string : //string before number to correctly handle char/wchar_t -> this was an issue with Loki only!
    isArithmetic<T>           ? TextType::number : //
    IsChronoDuration<T>::value ? TextType::chrono :
    TextType::other> {};

//######################################################################################

template <class T, TextType type>
struct ConvertText;
/* -> expected interface
{
    void writeText(const T& value, std::string& output) const;
    bool readText(const std::string& input, T& value) const;
};
*/

//partial specialization: type bool
template <class T>
struct ConvertText<T, TextType::boolean>
{
    void writeText(bool value, std::string& output) const
    {
        output = value ? "true" : "false";
    }
    bool readText(const std::string& input, bool& value) const
    {
        const std::string tmp = trimCpy(input);
        if (tmp == "true")
            value = true;
        else if (tmp == "false")
            value = false;
        else
            return false;
        return true;
    }
};

//partial specialization: handle conversion for all built-in arithmetic types!
template <class T>
struct ConvertText<T, TextType::number>
{
    void writeText(const T& value, std::string& output) const
    {
        output = numberTo<std::string>(value);
    }
    bool readText(const std::string& input, T& value) const
    {
        value = stringTo<T>(input);
        return true;
    }
};

template <class T>
struct ConvertText<T, TextType::chrono>
{
    void writeText(const T& value, std::string& output) const
    {
        output = numberTo<std::string>(value.count());
    }
    bool readText(const std::string& input, T& value) const
    {
        value = T(stringTo<typename T::rep>(input));
        return true;
    }
};

//partial specialization: handle conversion for all string-like types!
template <class T>
struct ConvertText<T, TextType::string>
{
    void writeText(const T& value, std::string& output) const
    {
        output = utfTo<std::string>(value);
    }
    bool readText(const std::string& input, T& value) const
    {
        value = utfTo<T>(input);
        return true;
    }
};


//partial specialization: unknown type
template <class T>
struct ConvertText<T, TextType::other>
{
    //###########################################################################################################################################
    static_assert(sizeof(T) == -1);
    /*
        ATTENTION: The data type T is yet unknown to the zen::Xml framework!

        Please provide a specialization for T of the following two functions in order to handle conversions to XML elements and attributes

        template <> void zen::writeText(const T& value, std::string& output)
        template <> bool zen::readText(const std::string& input, T& value)

        If T is structured and cannot be converted to a text representation specialize these two functions to allow at least for conversions to XML elements:

        template <> void zen::writeStruc(const T& value, XmlElement& output)
        template <> bool zen::readStruc(const XmlElement& input, T& value)
    */
    //###########################################################################################################################################
};


template <class T> inline
void writeText(const T& value, std::string& output)
{
    ConvertText<T, GetTextType<T>::value>().writeText(value, output);
}


template <class T> inline
bool readText(const std::string& input, T& value)
{
    return ConvertText<T, GetTextType<T>::value>().readText(input, value);
}
}

#endif //CVRT_TEXT_H_018727339083427097434
bgstack15