Skip to content

Commit e52218a

Browse files
committed
Fixed generation of C++17 code to work with more compilers. Putting
member names as globals allows it to work. Sheilded them with a namespace of object name prefixed with "sybols_"
1 parent f1cda01 commit e52218a

18 files changed

Lines changed: 152 additions & 105 deletions

CMakeLists.txt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,18 @@ link_directories( ${Boost_LIBRARY_DIRS} )
5454
set( HEADER_FILES
5555
${HEADER_FOLDER}/curl_t.h
5656
${HEADER_FOLDER}/json_to_cpp.h
57+
${HEADER_FOLDER}/json_to_cpp_config.h
5758
${HEADER_FOLDER}/ti_value.h
58-
${HEADER_FOLDER}/ti_array.h
59-
${HEADER_FOLDER}/ti_base.h
60-
${HEADER_FOLDER}/ti_boolean.h
61-
${HEADER_FOLDER}/ti_integral.h
62-
${HEADER_FOLDER}/ti_kv.h
63-
${HEADER_FOLDER}/ti_null.h
64-
${HEADER_FOLDER}/ti_object.h
65-
${HEADER_FOLDER}/ti_real.h
66-
${HEADER_FOLDER}/ti_string.h
67-
${HEADER_FOLDER}/ti_types.h
59+
${HEADER_FOLDER}/types/ti_array.h
60+
${HEADER_FOLDER}/types/ti_base.h
61+
${HEADER_FOLDER}/types/ti_boolean.h
62+
${HEADER_FOLDER}/types/ti_integral.h
63+
${HEADER_FOLDER}/types/ti_kv.h
64+
${HEADER_FOLDER}/types/ti_null.h
65+
${HEADER_FOLDER}/types/ti_object.h
66+
${HEADER_FOLDER}/types/ti_real.h
67+
${HEADER_FOLDER}/types/ti_string.h
68+
${HEADER_FOLDER}/types/ti_types.h
6869
)
6970

7071
set( SOURCE_FILES

include/json_to_cpp.h

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,8 @@
2727

2828
#include <daw/daw_string_view.h>
2929

30-
namespace daw::json_to_cpp {
31-
struct config_t final {
32-
bool enable_jsonlink = true;
33-
std::ostream *header_stream = nullptr;
34-
std::ostream *cpp_stream = nullptr;
35-
std::string root_object_name;
36-
boost::filesystem::path cpp_path;
37-
boost::filesystem::path json_path;
38-
std::vector<std::vector<std::string>> kv_paths;
39-
bool hide_null_only;
40-
bool use_string_view;
41-
bool has_cpp20;
42-
43-
std::ostream &header_file( );
44-
std::ostream &cpp_file( );
45-
46-
inline bool path_matches( std::vector<std::string> const & cur_path ) const {
47-
for( auto const & kv_path: kv_paths ) {
48-
if( kv_path == cur_path ) {
49-
return true;
50-
}
51-
}
52-
return false;
53-
}
54-
}; // config_t
30+
#include "json_to_cpp_config.h"
5531

32+
namespace daw::json_to_cpp {
5633
void generate_cpp( daw::string_view json_string, config_t &config );
5734
} // namespace daw::json_to_cpp

include/json_to_cpp_config.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2019 Darrell Wright
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files( the "Software" ), to
7+
// deal in the Software without restriction, including without limitation the
8+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and / or
9+
// sell copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in
13+
// all copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
#pragma once
24+
25+
#include <iostream>
26+
#include <string>
27+
#include <vector>
28+
29+
namespace daw::json_to_cpp {
30+
struct config_t final {
31+
bool enable_jsonlink = true;
32+
std::ostream *header_stream = nullptr;
33+
std::ostream *cpp_stream = nullptr;
34+
std::string root_object_name;
35+
std::string type_prefix{};
36+
std::string type_suffix{};
37+
std::optional<std::string> path{};
38+
boost::filesystem::path cpp_path;
39+
boost::filesystem::path json_path;
40+
std::vector<std::vector<std::string>> kv_paths;
41+
bool hide_null_only;
42+
bool use_string_view;
43+
bool has_cpp20;
44+
45+
std::ostream &header_file( );
46+
std::ostream &cpp_file( );
47+
48+
inline bool path_matches( std::vector<std::string> const &cur_path ) const {
49+
for( auto const &kv_path : kv_paths ) {
50+
if( kv_path == cur_path ) {
51+
return true;
52+
}
53+
}
54+
return false;
55+
}
56+
};
57+
} // namespace daw::json_to_cpp

include/ti_value.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <daw/daw_poly_var.h>
3333
#include <daw/daw_visit.h>
3434

35-
#include "ti_types.h"
35+
#include "types/ti_types.h"
3636

3737
namespace daw::json_to_cpp::types {
3838
struct ti_value {
@@ -57,10 +57,12 @@ namespace daw::json_to_cpp::types {
5757
[]( auto const &item ) { return item.name( ); } );
5858
}
5959

60-
inline std::string json_name( std::string member_name, bool use_cpp20 ) const noexcept {
61-
return daw::visit_nt( value, [&member_name, use_cpp20]( auto const &item ) {
62-
return item.json_name( member_name, use_cpp20 );
63-
} );
60+
inline std::string json_name( std::string member_name,
61+
bool use_cpp20, daw::string_view parent_name ) const noexcept {
62+
return daw::visit_nt( value,
63+
[&member_name, use_cpp20, parent_name]( auto const &item ) {
64+
return item.json_name( member_name, use_cpp20, parent_name );
65+
} );
6466
}
6567

6668
inline std::string array_member_info( ) const {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ namespace daw::json_to_cpp::types {
6060
ti_array &operator=( ti_array const &rhs );
6161

6262
std::string name( ) const;
63-
std::string json_name( daw::string_view member_name, bool use_cpp20 ) const;
63+
std::string json_name( daw::string_view member_name, bool use_cpp20, daw::string_view parent_name ) const;
6464
std::string array_member_info( ) const;
6565
};
6666
} // namespace daw::json_to_cpp::types
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ namespace daw::json_to_cpp::types::impl {
6464
return ( std::is_same_v<Args0, Args1> and ... );
6565
}
6666

67-
inline std::string format_member_name( daw::string_view name, bool use_cpp20 ) {
67+
inline std::string format_member_name( daw::string_view name, bool use_cpp20, daw::string_view parent_name ) {
6868
if( use_cpp20 ) {
6969
return "\"" + name + "\"";
7070
}
71-
return name;
71+
if( parent_name.empty( ) ) {
72+
return name;
73+
}
74+
std::string result = "symbols_" + parent_name.to_string( ) + "::" + name;
75+
return result;
7276
}
7377
} // namespace daw::json_to_cpp::types::impl
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ namespace daw::json_to_cpp::types {
4646
}
4747

4848
inline static std::string json_name( daw::string_view member_name,
49-
bool use_cpp20 ) noexcept {
50-
return "json_bool<" + impl::format_member_name( member_name, use_cpp20 ) +
49+
bool use_cpp20, daw::string_view parent_name ) noexcept {
50+
return "json_bool<" + impl::format_member_name( member_name, use_cpp20, parent_name ) +
5151
">";
5252
}
5353
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ namespace daw::json_to_cpp::types {
4747
return "json_number<no_name, int64_t>";
4848
}
4949

50-
inline static std::string json_name( daw::string_view member_name, bool use_cpp20 ) noexcept {
51-
return "json_number<" + impl::format_member_name( member_name, use_cpp20 ) + ", int64_t>";
50+
inline static std::string json_name( daw::string_view member_name, bool use_cpp20, daw::string_view parent_name ) noexcept {
51+
return "json_number<" + impl::format_member_name( member_name, use_cpp20, parent_name ) + ", int64_t>";
5252
}
5353
};
5454
} // namespace daw::json_to_cpp::types

include/ti_kv.h renamed to include/types/ti_kv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ namespace daw::json_to_cpp::types {
6565
std::string name( ) const;
6666
std::string array_member_info( ) const;
6767

68-
std::string json_name( daw::string_view member_name, bool use_cpp20 ) const;
68+
std::string json_name( daw::string_view member_name, bool use_cpp20, daw::string_view parent_name ) const;
6969
};
7070
} // namespace daw::json_to_cpp::types
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ namespace daw::json_to_cpp::types {
4747
return "json_custom<no_name>";
4848
}
4949

50-
static inline std::string json_name( daw::string_view member_name, bool use_cpp20 ) noexcept {
51-
return "json_custom<" + impl::format_member_name( member_name, use_cpp20 ) + ">";
50+
static inline std::string json_name( daw::string_view member_name, bool use_cpp20, daw::string_view parent_name ) noexcept {
51+
return "json_custom<" + impl::format_member_name( member_name, use_cpp20, parent_name ) + ">";
5252
}
5353
};
5454
} // namespace daw::json_to_cpp::types

0 commit comments

Comments
 (0)