Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 08bebaf

Browse files
committed
Introduce Xml binding powered serializer
Current serialization implementation require to map the design to the desired xml files. Thus, xml can't be easily removed and the code is hard to test. This patch introduces a serializer, based on the current xmlSerializer library. This serializer uses an Xml bindings structure which describe the mapping of the data holder to the desired xml files. TODO: TESTS Signed-off-by: Jules Clero <[email protected]>
1 parent 080902b commit 08bebaf

File tree

8 files changed

+922
-0
lines changed

8 files changed

+922
-0
lines changed

xmlserializer/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,21 @@ install(FILES
6565
XmlElement.h
6666
XmlSerializingContext.h
6767
DESTINATION "include/xmlserializer")
68+
69+
if(BUILD_TESTING)
70+
find_path(CATCH_HEADER catch.hpp)
71+
include_directories(
72+
./
73+
include
74+
${CATCH_HEADER})
75+
76+
# Add unit test
77+
add_executable(xmlBindingUnitTest test/XmlBindingUnitTest.cpp)
78+
79+
target_link_libraries(xmlBindingUnitTest xmlserializer)
80+
add_test(NAME xmlBindingUnitTest
81+
COMMAND xmlBindingUnitTest)
82+
83+
# Custom function defined in the top-level CMakeLists
84+
set_test_env(xmlBindingUnitTest)
85+
endif()
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/*
2+
* Copyright (c) 2015, Intel Corporation
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation and/or
13+
* other materials provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its contributors
16+
* may be used to endorse or promote products derived from this software without
17+
* specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
#pragma once
31+
32+
#include <convert.hpp>
33+
34+
#include <list>
35+
#include <string>
36+
#include <memory>
37+
#include <functional>
38+
#include <stdexcept>
39+
#include <utility>
40+
41+
namespace core
42+
{
43+
namespace xml
44+
{
45+
namespace binding
46+
{
47+
48+
/** Data member setter to use during deserialization
49+
*
50+
* @tparam the type of the data to retrieve
51+
*/
52+
template <typename T> using Setter = std::function<void(T)>;
53+
template <typename T> using Getter = std::function<T()>;
54+
template <typename T> using Accessor = std::pair<Getter<T>, Setter<T> >;
55+
56+
template <typename T>
57+
Accessor<T> makeAccessor(T &attribute)
58+
{
59+
return make_pair(Getter<T>([&attribute](){return attribute;}),
60+
Setter<T>([&attribute](T value){attribute = value;}));
61+
}
62+
63+
64+
/** Type deduction helper structure TODO: describe*/
65+
template <typename T> struct Type {};
66+
67+
// @{
68+
/** Available type List */
69+
template<> struct Type<int> { using type = int; };
70+
template<> struct Type<long> { using type = long; };
71+
template<> struct Type<long long> { using type = long long; };
72+
template<> struct Type<unsigned> { using type = unsigned; };
73+
template<> struct Type<short unsigned> { using type = short unsigned; };
74+
template<> struct Type<unsigned long> { using type = unsigned long; };
75+
template<> struct Type<unsigned long long> { using type = unsigned long long; };
76+
template<> struct Type<float> { using type = float; };
77+
template<> struct Type<double> { using type = double; };
78+
template<> struct Type<long double> { using type = long double; };
79+
template<> struct Type<bool> { using type = bool; };
80+
template<> struct Type<std::string> { using type = std::string; };
81+
// @}
82+
83+
namespace details
84+
{
85+
/** Attribute Implementation forward declaration */
86+
template <typename T>
87+
class AttributeImpl;
88+
} /** details namespace */
89+
90+
/** Xml Attribute representation.
91+
* Allows to retrieve the data in an xml file and to set it in the right place
92+
* This is a general facade which allows to store any type of attributes in the same container
93+
*
94+
* @tparam the type of the data to retrieve
95+
*/
96+
class Attribute
97+
{
98+
public:
99+
100+
/** Attribute constructor
101+
* Allows to define a complete attributes. An attribute should contains methods
102+
* to store and retrieves data of an Object. This is done using Getter and Setter object
103+
*
104+
* TODO: param doc
105+
*
106+
*/
107+
template<class Type, typename GetterType, typename SetterType>
108+
Attribute(const std::string &name, Type &&/**type*/, GetterType &&getter, SetterType &&setter) :
109+
mAttributeImpl{new details::AttributeImpl<typename Type::type>(name,
110+
Getter<typename Type::type>(getter),
111+
Setter<typename Type::type>(setter))}
112+
{
113+
}
114+
115+
template<class T>
116+
Attribute(const std::string &name, Accessor<T> &&accessor) :
117+
Attribute(name, Type<T>{}, Getter<T>(accessor.first), Setter<T>(accessor.second))
118+
{
119+
}
120+
121+
122+
virtual ~Attribute() = default;
123+
124+
/** Retrieve the attribute name
125+
*
126+
* @return a string containing the name of the attribute
127+
*/
128+
virtual const std::string &getName() const
129+
{
130+
return mAttributeImpl->getName();
131+
}
132+
133+
/** Retrieve the raw attribute value
134+
*
135+
* @return a string the raw attribute value
136+
*/
137+
virtual std::string get() const
138+
{
139+
return mAttributeImpl->get();
140+
}
141+
142+
/** Set the raw attribute value
143+
*
144+
* @param[in] value the raw attribute value to set
145+
*/
146+
virtual void set(const std::string& value)
147+
{
148+
mAttributeImpl->set(value);
149+
}
150+
151+
protected:
152+
153+
/** Default constructor needed by implementenation subclass */
154+
Attribute() = default;
155+
156+
private:
157+
158+
/** Implementation older
159+
*
160+
* shared_ptr is required because unique_ptr can't be copied.
161+
* As Attribute are going to be in lists, there will be a copy
162+
* when initializing through an initializer_list (which always copy)
163+
*/
164+
const std::shared_ptr<Attribute> mAttributeImpl = nullptr;
165+
};
166+
167+
namespace details
168+
{
169+
170+
/** Effective Attribute implementation class
171+
* Provide a real handling of a particular type of Attribute
172+
*
173+
* @tparam the type of the data contained in the attribute
174+
*/
175+
template <typename T>
176+
class AttributeImpl : public Attribute
177+
{
178+
public:
179+
180+
AttributeImpl(const std::string &name, Getter<T> getter, Setter<T> setter) :
181+
mName(name), mGetter(getter), mSetter(setter)
182+
{
183+
}
184+
185+
virtual const std::string &getName() const override final
186+
{
187+
return mName;
188+
}
189+
190+
virtual std::string get() const override final
191+
{
192+
return std::to_string(mGetter());
193+
}
194+
195+
virtual void set(const std::string &rawValue) override final
196+
{
197+
T value;
198+
if (!convertTo(rawValue, value))
199+
{
200+
throw std::invalid_argument("Invalid type conversion during '" +
201+
mName + "' attribute set");
202+
}
203+
mSetter(value);
204+
}
205+
206+
private:
207+
208+
const std::string mName;
209+
210+
const Getter<T> mGetter;
211+
const Setter<T> mSetter;
212+
};
213+
214+
template <>
215+
void AttributeImpl<std::string>::set(const std::string &rawValue)
216+
{
217+
mSetter(rawValue);
218+
}
219+
220+
template <>
221+
std::string AttributeImpl<std::string>::get() const
222+
{
223+
return mGetter();
224+
}
225+
226+
template <>
227+
std::string AttributeImpl<bool>::get() const
228+
{
229+
return mGetter() ? "true" : "false";
230+
}
231+
232+
} /** details namespace */
233+
234+
/** Renaming commonly used list of attributes types */
235+
using Attributes = std::list<Attribute>;
236+
237+
} /** binding namespace */
238+
} /** xml namespace */
239+
} /** core namespace */

0 commit comments

Comments
 (0)