Skip to content

Commit b87fc6b

Browse files
committed
(Feat) Added renaming feature for names that cannot be written out in cpp such as operators or numbers.
1 parent 5d386b4 commit b87fc6b

File tree

8 files changed

+360
-5
lines changed

8 files changed

+360
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/bin/*
66
/obj/*
77
/test.txt
8+
SimpleJSON_build_log.html

parse/jsd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "jsd_atomic.h"
1212
#include "jsd_optional.h"
1313
#include "jsd_fusion_adapted_struct.h"
14-
#include "jsd_object.h"
14+
#include "jsd_object.h"
15+
#include "jsd_renamed.h"
1516

1617
#endif

parse/jsd_renamed.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef JSD_RENAME_H_INCLUDED
2+
#define JSD_RENAME_H_INCLUDED
3+
4+
#include "jsd_core.h"
5+
6+
#include "../utility/rename.h"
7+
8+
namespace JSON
9+
{
10+
template <typename T, typename Name>
11+
void parse(rename <T, Name>& value, std::string const& name, PropertyTree const& object, ParsingOptions const& options = DEFAULT_PARSER_OPTIONS)
12+
{
13+
// this is kinda dirty.
14+
std::string relName;
15+
auto pos = name.find_last_of(".");
16+
if (pos == std::string::npos)
17+
relName = Name::c_str;
18+
else
19+
relName = name.substr(0, pos + 1) + Name::c_str;
20+
21+
// now parse
22+
parse(value.getValue(), relName, object, options);
23+
}
24+
}
25+
26+
#endif // JSD_RENAME_H_INCLUDED

stringify/iterator_traits.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef ITERATOR_TRAITS_H_INCLUDED
2-
#define ITERATOR_TRAITS_H_INCLUDED
1+
#ifndef JSS_ITERATOR_TRAITS_H_INCLUDED
2+
#define JSS_ITERATOR_TRAITS_H_INCLUDED
33

44
#include <iterator>
55
#include <type_traits>

stringify/jss.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
#include "jss_atomic.h"
2828
#include "jss_smart_ptr.h"
2929
#include "jss_check.h"
30-
#include "jss_optional.h"
30+
#include "jss_optional.h"
31+
#include "jss_renamed.h"
3132

32-
#include "jss_undefine.h"
33+
// #include "jss_undefine.h"
3334

3435
#endif // JSS_H_INCLUDED

stringify/jss_renamed.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef JSS_RENAMED_H_INCLUDED
2+
#define JSS_RENAMED_H_INCLUDED
3+
4+
#include "jss_core.h"
5+
6+
#include "../utility/rename.h"
7+
8+
namespace JSON
9+
{
10+
template <typename T, typename Name>
11+
std::ostream& stringify(std::ostream& stream, std::string const&, rename <T, Name> const& value, StringificationOptions options = DEFAULT_OPTIONS)
12+
{
13+
constexpr const auto name = Name::c_str;
14+
WRITE_NAME(stream);
15+
options.ignore_name = true;
16+
return JSON::stringify(stream, name, value.getValue(), options);
17+
}
18+
}
19+
20+
#endif // JSS_RENAMED_H_INCLUDED

utility/meta_util.h

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#ifndef UTILITY_META_UTIL_H_INCLUDED
2+
#define UTILITY_META_UTIL_H_INCLUDED
3+
4+
#include <type_traits>
5+
6+
namespace JSON
7+
{
8+
namespace internal
9+
{
10+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
11+
// PACK
12+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
13+
template <typename...>
14+
struct pack {};
15+
16+
template <typename Pack>
17+
struct pack_size;
18+
19+
template <typename... List>
20+
struct pack_size <pack <List...>> {
21+
enum {
22+
value = sizeof...(List)
23+
};
24+
};
25+
26+
template <std::size_t Index, typename Pack>
27+
struct pack_element;
28+
29+
template <typename Head, typename... Tail>
30+
struct pack_element <0, pack <Head, Tail...>> {
31+
using type = Head;
32+
};
33+
34+
template <std::size_t Index, typename Head, typename... Tail>
35+
struct pack_element <Index, pack <Head, Tail...>>
36+
: pack_element <Index-1, pack <Tail...>>
37+
{
38+
};
39+
40+
template <typename, typename>
41+
struct concat
42+
{ };
43+
44+
template <typename ... Ts, typename ... Us>
45+
struct concat <pack <Ts...>, pack <Us...>> {
46+
using type = pack <Ts..., Us...>;
47+
};
48+
49+
template <typename T, typename U>
50+
using concat_t = typename concat <T, U>::type;
51+
52+
template <typename Pack>
53+
struct pop_back
54+
{ };
55+
56+
template <>
57+
struct pop_back <pack <>> {
58+
using type = pack <>;
59+
};
60+
61+
template <typename T>
62+
struct pop_back <pack <T>> {
63+
using type = pack <>;
64+
};
65+
66+
template <typename T, typename ... List>
67+
struct pop_back <pack <T, List...>> {
68+
using type = concat_t <pack <T>, typename pop_back <pack <List...>>::type>;
69+
};
70+
71+
template <typename Pack>
72+
using pop_back_t = typename pop_back <Pack>::type;
73+
74+
template <typename Pack, template <typename...> class Function>
75+
struct apply { };
76+
77+
template <template <typename...> class Function, typename... List>
78+
struct apply <pack <List...>, Function> {
79+
using type = Function <List...>;
80+
};
81+
82+
template <typename Pack, template <typename...> class Function>
83+
using apply_t = typename apply <Pack, Function>::type;
84+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
85+
// TRIM
86+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
87+
template <typename Pack, template <typename> class Predicate, bool Abort = false>
88+
struct trim_right {
89+
// Abort == false
90+
using type = typename std::conditional <Predicate<typename pack_element <pack_size <Pack>::value - 1, Pack>::type>::value,
91+
typename trim_right <pop_back_t<Pack>, Predicate>::type,
92+
Pack>::type;
93+
};
94+
95+
template <typename Pack, template <typename> class Predicate>
96+
struct trim_right <Pack, Predicate, true> {
97+
using type = Pack;
98+
};
99+
100+
template <template <typename> class Predicate>
101+
struct trim_right <pack <>, Predicate, false> {
102+
using type = pack <>;
103+
};
104+
105+
template <typename Pack, template <typename> class Predicate>
106+
using trim_right_t = typename trim_right <Pack, Predicate>::type;
107+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
108+
// CEXPR_STRING
109+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
110+
template <char C>
111+
using char_ = std::integral_constant <char, C>;
112+
113+
template <typename... List>
114+
struct cexpr_string
115+
{
116+
using size_type = std::make_signed_t <std::size_t>;
117+
118+
using type = pack <List...>;
119+
constexpr static const size_type size = sizeof...(List);
120+
constexpr static const size_type length = size;
121+
122+
template <size_type N>
123+
struct at {
124+
static_assert(N < size, "Index out of bounds");
125+
126+
constexpr static const char value = pack_element <N, type>::type::value;
127+
using type = typename pack_element <N, type>::type;
128+
};
129+
130+
template <size_type N>
131+
using at_t = typename at <N>::type;
132+
133+
constexpr static const char c_str[] = {
134+
List::value..., '\0'
135+
};
136+
137+
constexpr static const size_type npos = -1;
138+
};
139+
140+
template <typename... List>
141+
constexpr const char cexpr_string <List...>::c_str[];
142+
143+
template <typename Char>
144+
struct is_zero {
145+
constexpr static const bool value = (Char::value == 0);
146+
};
147+
148+
#define SPLIT_1(s, x) JSON::internal::char_<( x < sizeof(s) ? s[x] : '\0' )>
149+
#define SPLIT_4(s, x) SPLIT_1 (s, x), SPLIT_1 (s, x+1) , SPLIT_1 (s, x+2) , SPLIT_1 (s, x+3)
150+
#define SPLIT_16(s, x) SPLIT_4 (s, x), SPLIT_4 (s, x+4) , SPLIT_4 (s, x+8) , SPLIT_4 (s, x+12)
151+
#define SPLIT_64(s, x) SPLIT_16 (s, x), SPLIT_16 (s, x+16) , SPLIT_16 (s, x+32) , SPLIT_16 (s, x+48)
152+
#define SPLIT_256(s, x) SPLIT_64 (s, x), SPLIT_64 (s, x+64) , SPLIT_64 (s, x+128) , SPLIT_64 (s, x+194)
153+
#define SPLIT_1024(s, x) SPLIT_256(s, x), SPLIT_256(s, x+256), SPLIT_256(s, x+512), SPLIT_256(s, x+768)
154+
155+
#define STRING_IMPL(str, n) JSON::internal::apply_t <JSON::internal::trim_right_t <JSON::internal::pack <SPLIT_##n(str, 0)>, JSON::internal::is_zero>, JSON::internal::cexpr_string>
156+
157+
#define SHORT_STRING(str) STRING_IMPL(str, 16)
158+
#define STRING(str) STRING_IMPL(str, 64)
159+
#define LONG_STRING(str) STRING_IMPL(str, 256) // HEAVY
160+
#define LONG_LONG_STRING(str) STRING_IMPL(str, 1024) // ULTRA HEAVY
161+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
162+
}
163+
164+
165+
}
166+
167+
168+
#endif // UTILITY_META_UTIL_H_INCLUDED

utility/rename.h

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#ifndef UTILITY_RENAME_H_INCLUDED
2+
#define UTILITY_RENAME_H_INCLUDED
3+
4+
#include "../stringify/jss_core.h"
5+
#include "../stringify/jss_object.h"
6+
7+
#include "../parse/jsd_core.h"
8+
#include "../parse/jsd_object.h"
9+
10+
#include "meta_util.h"
11+
12+
#include <string>
13+
#include <utility>
14+
#include <type_traits>
15+
16+
namespace JSON
17+
{
18+
template <typename T, typename Name>
19+
class rename
20+
{
21+
public:
22+
using type = T;
23+
using name = Name;
24+
25+
operator type&()
26+
{
27+
return objectValue_;
28+
}
29+
30+
operator type() const
31+
{
32+
return objectValue_;
33+
}
34+
35+
type getValue() const
36+
{
37+
return objectValue_;
38+
}
39+
40+
type& getValue()
41+
{
42+
return objectValue_;
43+
}
44+
45+
void setValue(type const& objectValue) const
46+
{
47+
objectValue_ = objectValue;
48+
}
49+
50+
rename <T, Name>& operator=(type const& objectValue)
51+
{
52+
objectValue_ = objectValue;
53+
return *this;
54+
}
55+
56+
rename <T, Name>& operator=(type&& objectValue)
57+
{
58+
objectValue_ = objectValue;
59+
return *this;
60+
}
61+
62+
template <typename OtherName>
63+
rename <T, Name>& operator=(rename <T, OtherName> const& other)
64+
{
65+
if (this == &other)
66+
return;
67+
68+
objectValue_ = other.objectValue_;
69+
return *this;
70+
}
71+
72+
template <typename OtherName>
73+
rename <T, Name>& operator=(rename <T, OtherName>&& other)
74+
{
75+
objectValue_ = other.objectValue_;
76+
return *this;
77+
}
78+
79+
template <typename OtherName>
80+
rename(rename <T, OtherName> const& other)
81+
: objectValue_(other.objectValue_)
82+
{
83+
}
84+
85+
template <typename OtherName>
86+
rename(rename <T, OtherName>&& other)
87+
: objectValue_(std::move(other.objectValue_))
88+
{
89+
}
90+
91+
rename(type objectValue)
92+
: objectValue_(std::move(objectValue))
93+
{
94+
}
95+
96+
template <typename U = type>
97+
rename(typename std::enable_if <std::is_default_constructible <U>::value>::type* = nullptr)
98+
: objectValue_()
99+
{
100+
101+
}
102+
103+
~rename() = default;
104+
105+
constexpr static const char* getName()
106+
{
107+
return name::c_str;
108+
}
109+
110+
/*
111+
std::ostream& stringify(std::ostream& stream, StringificationOptions options) const
112+
{
113+
return JSON::stringify(stream, std::string(name::c_str), objectValue_, options);
114+
}
115+
*/
116+
117+
private:
118+
type objectValue_;
119+
};
120+
121+
template <typename T>
122+
struct is_renamed
123+
{
124+
enum {
125+
value = false
126+
};
127+
};
128+
129+
template <typename T, typename Name>
130+
struct is_renamed <rename <T, Name>>
131+
{
132+
enum {
133+
value = true
134+
};
135+
};
136+
}
137+
138+
#endif // UTILITY_RENAME_H_INCLUDED

0 commit comments

Comments
 (0)