Skip to content

Commit b2a8441

Browse files
committed
Changed file extension for all header files to hpp
1 parent e48eb5c commit b2a8441

30 files changed

+1685
-0
lines changed

parse/jsd.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef JSD_H_INCLUDED
2+
#define JSD_H_INCLUDED
3+
4+
#include "jsd_fundamental.hpp"
5+
#include "jsd_string.hpp"
6+
#include "jsd_container.hpp"
7+
#include "jsd_map.hpp"
8+
#include "jsd_unordered_map.hpp"
9+
#include "jsd_array.hpp"
10+
#include "jsd_set.hpp"
11+
#include "jsd_pair.hpp"
12+
#include "jsd_atomic.hpp"
13+
#include "jsd_optional.hpp"
14+
#include "jsd_fusion_adapted_struct.hpp"
15+
#include "jsd_object.hpp"
16+
#include "jsd_renamed.hpp"
17+
#include "jsd_unique_ptr.hpp"
18+
#include "jsd_shared_ptr.hpp"
19+
20+
#endif

parse/jsd_array.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef JSD_ARRAY_H_INCLUDED
2+
#define JSD_ARRAY_H_INCLUDED
3+
4+
#include "jsd_core.hpp"
5+
#include <array>
6+
#include <stdexcept>
7+
8+
namespace JSON
9+
{
10+
template <typename T, std::size_t N>
11+
void parse(std::array<T, N>& value, std::string const& name,
12+
PropertyTree const& object, ParsingOptions const& options = DEFAULT_PARSER_OPTIONS)
13+
{
14+
try
15+
{
16+
GET_CHILD(name, pt, {});
17+
18+
int pos = 0;
19+
for (auto const& i : pt)
20+
{
21+
T temp;
22+
parse(temp, "", i.second, options);
23+
if (pos != N)
24+
value[pos++] = temp;
25+
else if (options.invalidPropertyHandler != InvalidPropertyHandlingBehaviour::IGNORE_ALL_ERROR)
26+
throw std::out_of_range("there is more data to be read, but the array is full");
27+
}
28+
}
29+
catch (boost::property_tree::ptree_bad_data& exc)
30+
{
31+
DEFAULT_PROPERTY_ERROR_HANDLER({}, {});
32+
}
33+
catch (boost::property_tree::ptree_bad_path& exc)
34+
{
35+
DEFAULT_PATH_ERROR_HANDLER({}, {});
36+
}
37+
}
38+
}
39+
40+
#endif

parse/jsd_atomic.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef JSD_ATOMIC_H_INCLUDED
2+
#define JSD_ATOMIC_H_INCLUDED
3+
4+
#include "jsd_core.hpp"
5+
#include <atomic>
6+
7+
namespace JSON
8+
{
9+
template <typename T>
10+
void parse(std::atomic<T>& value, std::string const& name,
11+
PropertyTree const& object, ParsingOptions const& options = DEFAULT_PARSER_OPTIONS)
12+
{
13+
try
14+
{
15+
T temp;
16+
parse(temp, name, object, options);
17+
value.store(std::move(temp));
18+
}
19+
catch (boost::property_tree::ptree_bad_data& exc)
20+
{
21+
DEFAULT_PROPERTY_ERROR_HANDLER(T(), T());
22+
}
23+
catch (boost::property_tree::ptree_bad_path& exc)
24+
{
25+
DEFAULT_PATH_ERROR_HANDLER(T(), T());
26+
}
27+
}
28+
}
29+
30+
#endif

parse/jsd_check.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef JSD_CHECK_H_INCLUDED
2+
#define JSD_CHECK_H_INCLUDED
3+
4+
#include "jsd_core.hpp"
5+
6+
namespace JSON { namespace Internal {
7+
8+
template<typename T>
9+
class can_parse
10+
{
11+
template<typename U>
12+
static char check(...);
13+
template<typename U>
14+
static char (&check(decltype(parse(std::declval<U>(), std::declval<std::string const>(), std::declval<PropertyTree const>(), std::declval<ParsingOptions const>()))*))[2];
15+
public:
16+
static const bool value=sizeof(check<T>(0))==2;
17+
};
18+
19+
}
20+
}
21+
22+
#endif // JSD_CHECK_H_INCLUDED

parse/jsd_container.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef JSD_CONTAINER_H_INCLUDED
2+
#define JSD_CONTAINER_H_INCLUDED
3+
4+
#include "jsd_core.hpp"
5+
6+
namespace JSON
7+
{
8+
template <typename T, template <typename, class = std::allocator <T> > class ContainerT>
9+
void parse(ContainerT<T>& value, std::string const& name,
10+
PropertyTree const& object, ParsingOptions const& options = DEFAULT_PARSER_OPTIONS)
11+
{
12+
try
13+
{
14+
GET_CHILD(name, pt, {});
15+
for (auto const& i : pt)
16+
{
17+
T temp;
18+
parse(temp, "", i.second, options);
19+
value.emplace_back(std::move(temp));
20+
}
21+
}
22+
catch (boost::property_tree::ptree_bad_data& exc)
23+
{
24+
DEFAULT_PROPERTY_ERROR_HANDLER({}, {});
25+
}
26+
catch (boost::property_tree::ptree_bad_path& exc)
27+
{
28+
DEFAULT_PATH_ERROR_HANDLER({}, {});
29+
}
30+
}
31+
}
32+
33+
#endif

parse/jsd_convenience.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef JSD_CONVENIENC_H_INCLUDED
2+
#define JSD_CONVENIENC_H_INCLUDED
3+
4+
#include "jsd_core.hpp"
5+
#include "jsd_check.hpp"
6+
#include "jsd_options.hpp"
7+
8+
namespace JSON
9+
{
10+
template <typename T>
11+
void try_parse(T& obj, std::string const& name, PropertyTree const& tree, ParsingOptions const& options = DEFAULT_PARSER_OPTIONS,
12+
typename std::enable_if <Internal::can_parse<T>::value, void>::type* = nullptr)
13+
{
14+
parse(obj, name, tree, options);
15+
}
16+
17+
template <typename T>
18+
void try_parse(T&, std::string const&, PropertyTree const&, ParsingOptions const& = DEFAULT_PARSER_OPTIONS,
19+
typename std::enable_if <!Internal::can_parse<T>::value, int>::type* = nullptr)
20+
{
21+
static_assert (Internal::can_parse<T>::value, "the object you try to parse has no applicable interface");
22+
}
23+
}
24+
25+
#define json_deserialize(NAME) JSON::try_parse(#NAME, NAME)
26+
#define json_deserialize_opt(NAME, OPTIONS) JSON::try_parse(#NAME, NAME, OPTIONS)
27+
28+
#endif // JSD_CONVENIENCE_H_INCLUDED

parse/jsd_core.hpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#ifndef JSD_CORE_H_INCLUDED
2+
#define JSD_CORE_H_INCLUDED
3+
4+
#include "jsd_generic_parser.hpp"
5+
#include "jsd_options.hpp"
6+
7+
#include <type_traits>
8+
#include <utility>
9+
10+
namespace JSON
11+
{
12+
#define GET_VALUE(TYPE, NAME, TEMP, TAG_VALUE) \
13+
if (options.invalidPathHandler == InvalidPathHandlingBehaviour::IGNORE_ALL_ERROR || \
14+
options.invalidPathHandler == InvalidPathHandlingBehaviour::TAG) \
15+
{ \
16+
value = {}; \
17+
auto opt = object.tree.get_optional <TYPE> (NAME); \
18+
if (!opt) { \
19+
if (options.invalidPathHandler == InvalidPathHandlingBehaviour::IGNORE_ALL_ERROR) \
20+
return; \
21+
else { \
22+
value = TAG_VALUE; \
23+
return; \
24+
} \
25+
} \
26+
TEMP = opt.get(); \
27+
} \
28+
else \
29+
{ \
30+
TEMP = object.tree.get <TYPE> (NAME); \
31+
}
32+
// MAKRO END
33+
34+
#define GET_CHILD(NAME, RESULT, TAG_VALUE) \
35+
decltype(object.tree) RESULT;\
36+
if (options.invalidPathHandler == InvalidPathHandlingBehaviour::IGNORE_ALL_ERROR || \
37+
options.invalidPathHandler == InvalidPathHandlingBehaviour::TAG) \
38+
{ \
39+
value = {}; \
40+
auto opt = object.tree.get_child_optional (NAME); \
41+
if (!opt) { \
42+
if (options.invalidPathHandler == InvalidPathHandlingBehaviour::IGNORE_ALL_ERROR) \
43+
return; \
44+
else { \
45+
value = TAG_VALUE; \
46+
return; \
47+
} \
48+
} \
49+
RESULT = opt.get(); \
50+
} \
51+
else \
52+
{ \
53+
RESULT = object.tree.get_child (NAME); \
54+
}
55+
// MAKRO END
56+
57+
#define DEFAULT_PROPERTY_ERROR_HANDLER(DEFAULT_VALUE, TAG_VALUE) \
58+
switch (options.invalidPropertyHandler) { \
59+
case (InvalidPropertyHandlingBehaviour::DEFAULT): \
60+
value = DEFAULT_VALUE; \
61+
return; \
62+
case (InvalidPropertyHandlingBehaviour::TAG): \
63+
value = TAG_VALUE; \
64+
return; \
65+
case (InvalidPropertyHandlingBehaviour::IGNORE_ALL_ERROR): \
66+
return; \
67+
case (InvalidPropertyHandlingBehaviour::THROW): \
68+
throw exc; \
69+
}
70+
// MAKRO END
71+
72+
#define DEFAULT_PATH_ERROR_HANDLER(DEFAULT_VALUE, TAG_VALUE) \
73+
switch (options.invalidPathHandler) { \
74+
case (InvalidPathHandlingBehaviour::DEFAULT): \
75+
value = DEFAULT_VALUE; \
76+
return; \
77+
case (InvalidPathHandlingBehaviour::TAG): \
78+
value = TAG_VALUE; \
79+
return; \
80+
case (InvalidPathHandlingBehaviour::IGNORE_ALL_ERROR): \
81+
return; \
82+
case (InvalidPathHandlingBehaviour::THROW): \
83+
throw exc; \
84+
}
85+
// MAKRO END
86+
87+
inline std::string member_name(std::string const& base_name, std::string const& name)
88+
{
89+
if (base_name.empty())
90+
return "";
91+
else
92+
return base_name + "." + name;
93+
}
94+
}
95+
96+
#endif

parse/jsd_fundamental.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef JSD_FUNDAMENTAL_H_INCLUDED
2+
#define JSD_FUNDAMENTAL_H_INCLUDED
3+
4+
#include "jsd_core.hpp"
5+
6+
namespace JSON
7+
{
8+
template <typename T,
9+
class = typename std::enable_if< (std::is_arithmetic<T>::value && !std::is_same<T, char>::value && !std::is_same<T, wchar_t>::value)
10+
|| std::is_enum<T>::value >::type
11+
>
12+
void parse(T& value, std::string const& name,
13+
PropertyTree const& object, ParsingOptions const& options = DEFAULT_PARSER_OPTIONS)
14+
{
15+
try
16+
{
17+
GET_VALUE(T, name, value, T());
18+
}
19+
catch (boost::property_tree::ptree_bad_data& exc)
20+
{
21+
DEFAULT_PROPERTY_ERROR_HANDLER(T(), T());
22+
}
23+
catch (boost::property_tree::ptree_bad_path& exc)
24+
{
25+
DEFAULT_PATH_ERROR_HANDLER(T(), T());
26+
}
27+
}
28+
29+
void parse(char& value, std::string const& name,
30+
PropertyTree const& object, ParsingOptions const& options = DEFAULT_PARSER_OPTIONS);
31+
void parse(wchar_t& value, std::string const& name,
32+
PropertyTree const& object, ParsingOptions const& options = DEFAULT_PARSER_OPTIONS);
33+
}
34+
35+
#endif
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#ifndef JSD_FUSION_ADAPTED_STRUCT_H_INCLUDED
2+
#define JSD_FUSION_ADAPTED_STRUCT_H_INCLUDED
3+
4+
#include "jsd_core.hpp"
5+
#include "jsd_object.hpp"
6+
7+
#include <boost/fusion/mpl.hpp>
8+
#include <boost/fusion/adapted.hpp>
9+
#include <boost/fusion/include/at.hpp>
10+
#include <boost/fusion/include/for_each.hpp>
11+
#include <boost/mpl/range_c.hpp>
12+
#include <boost/mpl/for_each.hpp>
13+
#include <boost/phoenix/phoenix.hpp>
14+
#include <boost/fusion/include/size.hpp>
15+
16+
#include <iostream>
17+
#include <functional>
18+
#include <type_traits>
19+
20+
namespace JSON
21+
{
22+
template <typename T>
23+
class AdaptedParser
24+
{
25+
public:
26+
void operator()(T& object, std::string const& name, PropertyTree const& tree, ParsingOptions const& options) const
27+
{
28+
//! If you get an Error here, you likely forgot to use BOOST_FUSION_ADAPT_STRUCT !
29+
30+
typedef boost::mpl::range_c<
31+
int,
32+
0,
33+
boost::fusion::result_of::size<T>::type::value
34+
> range;
35+
36+
std::stringstream sstr;
37+
boost::mpl::for_each<range>(std::bind<void>
38+
(
39+
_helper(boost::fusion::result_of::size<T>::type::value),
40+
std::placeholders::_1,
41+
std::ref(object),
42+
std::ref(name),
43+
std::ref(tree),
44+
std::ref(options)
45+
));
46+
}
47+
private:
48+
class _helper
49+
{
50+
public:
51+
template<class Index>
52+
void operator()(Index, T& object, std::string const& name, PropertyTree const& tree, ParsingOptions const& options) const
53+
{
54+
if (name.empty())
55+
parse(boost::fusion::at<Index>(object),
56+
boost::fusion::extension::struct_member_name<T, Index::value>::call(),
57+
tree, options);
58+
else
59+
parse(boost::fusion::at<Index>(object),
60+
name + "." + boost::fusion::extension::struct_member_name<T, Index::value>::call(),
61+
tree, options);
62+
}
63+
_helper(int len) : len(len) {}
64+
private:
65+
int len;
66+
};
67+
};
68+
69+
template <typename Derived>
70+
struct Parsable
71+
{
72+
void parse(std::string const& name, PropertyTree const& tree, ParsingOptions const& options = {})
73+
{
74+
AdaptedParser<Derived> parser;
75+
parser(*static_cast <Derived*> (this), name, tree, options);
76+
}
77+
virtual ~Parsable() = default;
78+
};
79+
}
80+
81+
#endif // JSS_FUSION_ADAPTED_STRUCT_H_INCLUDED

0 commit comments

Comments
 (0)