libfoedus-core
FOEDUS Core Library
tinyxml_wrapper.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_EXTERNALIZE_TINYXML_WRAPPER_HPP_
19 #define FOEDUS_EXTERNALIZE_TINYXML_WRAPPER_HPP_
20 
21 #include <stdint.h>
22 #include <tinyxml2.h>
23 
24 #include <string>
25 
27 
28 namespace foedus {
29 namespace externalize {
36 template <typename T> struct TinyxmlGetter {
37  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, T* out);
38 };
39 template<> struct TinyxmlGetter<bool> {
40  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, bool *out) {
41  return element->QueryBoolText(out);
42  }
43 };
44 
45 template<> struct TinyxmlGetter<int64_t> {
46  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, int64_t *out) {
47  return element->QueryLongLongText(out);
48  }
49 };
50 template<> struct TinyxmlGetter<uint64_t> {
51  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, uint64_t *out) {
52  return element->QueryUnsignedLongLongText(out);
53  }
54 };
55 
56 template <typename T, typename LARGEST_TYPE, typename LARGEST_GETTER>
57 tinyxml2::XMLError get_smaller_int(const tinyxml2::XMLElement *element, T* out) {
58  LARGEST_TYPE tmp;
59  LARGEST_GETTER largest_getter;
60  tinyxml2::XMLError ret = largest_getter(element, &tmp);
61  if (ret != tinyxml2::XML_SUCCESS) {
62  return ret;
63  }
64  *out = static_cast<T>(tmp);
65  if (static_cast<LARGEST_TYPE>(*out) != tmp) {
66  return tinyxml2::XML_CAN_NOT_CONVERT_TEXT;
67  } else {
68  return tinyxml2::XML_SUCCESS;
69  }
70 }
71 
72 template<> struct TinyxmlGetter<int32_t> {
73  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, int32_t *out) {
74  return get_smaller_int<int32_t, int64_t, TinyxmlGetter<int64_t> >(element, out);
75  }
76 };
77 template<> struct TinyxmlGetter<uint32_t> {
78  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, uint32_t *out) {
79  return get_smaller_int<uint32_t, uint64_t, TinyxmlGetter<uint64_t> >(element, out);
80  }
81 };
82 template<> struct TinyxmlGetter<int16_t> {
83  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, int16_t *out) {
84  return get_smaller_int<int16_t, int64_t, TinyxmlGetter<int64_t> >(element, out);
85  }
86 };
87 template<> struct TinyxmlGetter<uint16_t> {
88  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, uint16_t *out) {
89  return get_smaller_int<uint16_t, uint64_t, TinyxmlGetter<uint64_t> >(element, out);
90  }
91 };
92 template<> struct TinyxmlGetter<int8_t> {
93  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, int8_t *out) {
94  return get_smaller_int<int8_t, int64_t, TinyxmlGetter<int64_t> >(element, out);
95  }
96 };
97 template<> struct TinyxmlGetter<uint8_t> {
98  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, uint8_t *out) {
99  return get_smaller_int<uint8_t, uint64_t, TinyxmlGetter<uint64_t> >(element, out);
100  }
101 };
102 
103 template<> struct TinyxmlGetter<std::string> {
104  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, std::string *out) {
105  const char* text = element->GetText();
106  if (text) {
107  *out = text;
108  } else {
109  out->clear();
110  }
111  return tinyxml2::XML_SUCCESS;
112  }
113 };
114 template <uint MAXLEN, typename CHAR>
115 struct TinyxmlGetter< assorted::FixedString<MAXLEN, CHAR> > {
116  tinyxml2::XMLError operator()(
117  const tinyxml2::XMLElement *element,
119  const char* text = element->GetText();
120  if (text) {
121  *out = text;
122  } else {
123  out->clear();
124  }
125  return tinyxml2::XML_SUCCESS;
126  }
127 };
128 template<> struct TinyxmlGetter<double> {
129  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, double *out) {
130  return element->QueryDoubleText(out);
131  }
132 };
133 template<> struct TinyxmlGetter<float> {
134  tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, float *out) {
135  return element->QueryFloatText(out);
136  }
137 };
138 
145 template <typename T> struct TinyxmlSetter {
146  void operator()(tinyxml2::XMLElement *element, const T &value) {
147  element->SetText(value);
148  }
149 };
150 template <> struct TinyxmlSetter<std::string> {
151  void operator()(tinyxml2::XMLElement *element, const std::string &value) {
152  element->SetText(value.c_str());
153  }
154 };
155 template <uint MAXLEN, typename CHAR>
156 struct TinyxmlSetter< assorted::FixedString<MAXLEN, CHAR> > {
157  void operator()(tinyxml2::XMLElement *element, const assorted::FixedString<MAXLEN, CHAR> &value) {
158  element->SetText(value.str().c_str());
159  }
160 };
161 
162 } // namespace externalize
163 } // namespace foedus
164 
165 #endif // FOEDUS_EXTERNALIZE_TINYXML_WRAPPER_HPP_
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, bool *out)
Functor to help use tinyxml2's Element SetText().
void operator()(tinyxml2::XMLElement *element, const assorted::FixedString< MAXLEN, CHAR > &value)
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, int8_t *out)
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, uint32_t *out)
STL namespace.
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, float *out)
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, int16_t *out)
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, uint64_t *out)
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, double *out)
Functor to help use tinyxml2's Element QueryXxxText().
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, uint8_t *out)
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, T *out)
void operator()(tinyxml2::XMLElement *element, const std::string &value)
std::basic_string< CHAR > str() const
Convert to a std::string object.
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, int32_t *out)
void operator()(tinyxml2::XMLElement *element, const T &value)
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, std::string *out)
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, assorted::FixedString< MAXLEN, CHAR > *out)
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, int64_t *out)
tinyxml2::XMLError get_smaller_int(const tinyxml2::XMLElement *element, T *out)
void clear() noexcept
Clear string.
An embedded string object of fixed max-length, which uses no external memory.
tinyxml2::XMLError operator()(const tinyxml2::XMLElement *element, uint16_t *out)