@@ -32,37 +32,37 @@ namespace daw {
3232 namespace json_to_cpp {
3333 bool enable_comments;
3434 bool enable_jsonlink;
35-
35+
3636 void config_t::set_links ( ) {
3737 link_boolean ( " enable_comments" , enable_comments );
3838 link_boolean ( " enable_jsonlink" , enable_jsonlink );
3939 }
4040
4141 config_t::config_t ( ):
42- daw::json::JsonLink<config_t >{ },
43- enable_comments{ true },
44- enable_jsonlink{ true } {
45-
46- set_links ( );
47- }
42+ daw::json::JsonLink<config_t >{ },
43+ enable_comments{ true },
44+ enable_jsonlink{ true } {
45+
46+ set_links ( );
47+ }
4848
4949 config_t ::~config_t ( ) { }
5050
5151 config_t::config_t ( config_t const & other ):
52- daw::json::JsonLink<config_t >{ },
53- enable_comments{ other.enable_comments },
54- enable_jsonlink{ other.enable_jsonlink } {
55-
56- set_links ( );
57- }
52+ daw::json::JsonLink<config_t >{ },
53+ enable_comments{ other.enable_comments },
54+ enable_jsonlink{ other.enable_jsonlink } {
55+
56+ set_links ( );
57+ }
5858
5959 config_t::config_t ( config_t && other ):
60- daw::json::JsonLink<config_t >{ },
61- enable_comments{ std::move ( other.enable_comments ) },
62- enable_jsonlink{ std::move ( other.enable_jsonlink ) } {
63-
64- set_links ( );
65- }
60+ daw::json::JsonLink<config_t >{ },
61+ enable_comments{ std::move ( other.enable_comments ) },
62+ enable_jsonlink{ std::move ( other.enable_jsonlink ) } {
63+
64+ set_links ( );
65+ }
6666
6767 namespace {
6868 struct val_info_t {
@@ -102,7 +102,7 @@ namespace daw {
102102
103103 void parse_json_object ( boost::string_view cur_name, daw::json::impl::object_value const & cur_item, std::unordered_map<std::string, obj_info_t > & obj_info ) {
104104 using daw::json::impl::value_t ;
105-
105+
106106 obj_info_t cur_obj;
107107 cur_obj.name = cur_name.to_string ( );
108108 if ( cur_obj.name .empty ( ) ) {
@@ -127,7 +127,7 @@ namespace daw {
127127 obj_info[cur_obj.name ].members [member.first ] = member.second ;
128128 }
129129 }
130-
130+
131131 void parse_json_array ( boost::string_view cur_name, daw::json::impl::value_t const & cur_item, std::unordered_map<std::string, obj_info_t > & obj_info ) {
132132 using daw::json::impl::value_t ;
133133 obj_info_t cur_obj;
@@ -208,18 +208,97 @@ namespace daw {
208208 std::abort ( );
209209 }
210210
211+ void generate_default_constructor ( bool definition, std::stringstream & ss, std::string const & obj_type, obj_info_t cur_obj ) {
212+ if ( definition ) {
213+ ss << obj_type << " ::" << obj_type << " ( ):\n " ;
214+ ss << " \t\t daw::json::JsonLink<" << obj_type << " >{ }" ;
215+ for ( auto const & member: cur_obj.members ) {
216+ ss << " ,\n\t\t " << member.first << " { }" ;
217+ }
218+ ss << " {\n\n\t set_links( );\n }\n\n " ;
219+ } else {
220+ ss << " \t " << obj_type << " ( );\n " ;
221+ }
222+ }
223+
224+ void generate_copy_constructor ( bool definition, std::stringstream & ss, std::string const & obj_type, obj_info_t cur_obj ) {
225+ if ( definition ) {
226+ ss << obj_type << " ::" ;
227+ ss << obj_type << " ( " << obj_type << " const & other )" ;
228+ ss << " :\n\t\t daw::json::JsonLink<" << obj_type << " >{ }" ;
229+ for ( auto const & member: cur_obj.members ) {
230+ ss << " ,\n\t\t " << member.first << " { other." << member.first << " }" ;
231+ }
232+ ss << " {\n\n\t set_links( );\n }\n\n " ;
233+ } else {
234+ ss << " \t " << obj_type << " ( " << obj_type << " const & other );\n " ;
235+ }
236+ }
237+
238+ void generate_move_constructor ( bool definition, std::stringstream & ss, std::string const & obj_type, obj_info_t cur_obj ) {
239+ if ( definition ) {
240+ ss << obj_type << " ::" << obj_type << " ( " << obj_type << " && other ):\n " ;
241+ ss << " \t\t daw::json::JsonLink<" << obj_type << " >{ }" ;
242+ for ( auto const & member: cur_obj.members ) {
243+ ss << " ,\n\t\t " << member.first << " { std::move( other." << member.first << " ) }" ;
244+ }
245+ ss << " {\n\n\t set_links( );\n }\n\n " ;
246+ } else {
247+ ss << " \t " << obj_type << " ( " << obj_type << " && other );\n " ;
248+ }
249+ }
250+
251+ void generate_destructor ( bool definition, std::stringstream & ss, std::string const & obj_type ) {
252+ if ( definition ) {
253+ ss << obj_type << " ::~" << obj_type << " ( ) { }\n\n " ;
254+ } else {
255+ ss << " \t ~" << obj_type << " ( );\n " ;
256+ }
257+ }
258+
259+ void generate_set_links ( bool definition, std::stringstream & ss, std::string const & obj_type, obj_info_t cur_obj ) {
260+ if ( definition ) {
261+ ss << " void " << obj_type << " ::set_links( ) {\n " ;
262+ for ( auto const & item: cur_obj.members ) {
263+ auto const & member = item.second ;
264+ ss << " \t link_" ;
265+ if ( cur_obj.is_array ) {
266+ ss << " array" ;
267+ } else {
268+ ss << type_to_jsonstring ( member.type );
269+ }
270+ ss << " ( \" " << member.name << " \" , " << member.name << " );\n " ;
271+ }
272+ ss << " }\n\n " ;
273+ } else {
274+ ss << " \t void set_links( );\n " ;
275+ }
276+ }
277+
278+ void generate_jsonlink ( bool definition, std::stringstream & ss, std::string const & obj_type, obj_info_t cur_obj ) {
279+ generate_default_constructor ( definition, ss, obj_type, cur_obj );
280+ generate_copy_constructor ( definition, ss, obj_type, cur_obj );
281+ generate_move_constructor ( definition, ss, obj_type, cur_obj );
282+ generate_destructor ( definition, ss, obj_type );
283+ generate_set_links ( definition, ss, obj_type, cur_obj );
284+ if ( !definition ) {
285+ ss << " \n\t " << obj_type << " & operator=( " << obj_type << " const & ) = default;\n " ;
286+ ss << " \t " << obj_type << " & operator=( " << obj_type << " && ) = default;\n " ;
287+ }
288+ }
289+
211290 std::string generate_code ( std::unordered_map<std::string, obj_info_t > obj_info, config_t const & config ) {
212291 using daw::json::impl::value_t ;
213292 std::stringstream ss;
214293 for ( auto const & obj_info_item: obj_info ) {
215294 auto const & cur_obj = obj_info_item.second ;
216295 auto const obj_type = cur_obj.name + " _t" ;
217-
296+
218297 ss << " struct " << obj_type;
219298 if ( config.enable_jsonlink ) {
220299 ss << " : public daw::json::JsonLink<" << obj_type << " >" ;
221300 }
222- ss << " {\n\n " ;
301+ ss << " {\n " ;
223302 for ( auto const & item: cur_obj.members ) {
224303 auto const & member = item.second ;
225304 auto const member_type = type_to_string ( member.name , member.type );
@@ -239,60 +318,18 @@ namespace daw {
239318 }
240319 ss << " " << member.name << " ;\n " ;
241320 }
321+ ss << " \n " ;
242322 if ( config.enable_jsonlink ) {
243- // Default Constructor
244- {
245- ss << " \n\t " << obj_type << " ( ):\n " ;
246- ss << " \t\t\t daw::json::JsonLink<" << obj_type << " >{ }" ;
247- for ( auto const & member: cur_obj.members ) {
248- ss << " ,\n\t\t\t " << member.first << " { }" ;
249- }
250- ss << " {\n\t\t set_links( );\n\t }\n " ;
251- }
252- // Copy Constructor
253- {
254- ss << " \t " << obj_type << " ( " << obj_type << " const & other ):\n " ;
255- ss << " \t\t\t daw::json::JsonLink<" << obj_type << " >{ }" ;
256- for ( auto const & member: cur_obj.members ) {
257- ss << " ,\n\t\t\t " << member.first << " { other." << member.first << " }" ;
258- }
259- ss << " {\n\t\t set_links( );\n\t }\n " ;
260- }
261- // Move Constructor
262- {
263- ss << " \t " << obj_type << " ( " << obj_type << " && other ):\n " ;
264- ss << " \t\t\t daw::json::JsonLink<" << obj_type << " >{ }" ;
265- for ( auto const & member: cur_obj.members ) {
266- ss << " ,\n\t\t\t " << member.first << " { std::move( other." << member.first << " ) }" ;
267- }
268- ss << " {\n\t\t set_links( );\n\t }\n " ;
269- }
270- // Destructor
271- ss << " \t ~" << obj_type << " ( ) { }\n " ;
272- // copy/move operators
273- ss << " \t " << obj_type << " & operator=( " << obj_type << " const & ) = default\n " ;
274- ss << " \t " << obj_type << " & operator=( " << obj_type << " && ) = default\n " ;
275- // set_links
276- ss << " \t void set_links( ) {" ;
277- for ( auto const & item: cur_obj.members ) {
278- auto const & member = item.second ;
279- ss << " \n\t\t link_" ;
280- if ( cur_obj.is_array ) {
281- ss << " array" ;
282- } else {
283- ss << type_to_jsonstring ( member.type );
284- }
285- ss << " ( \" " << member.name << " \" , " << member.name << " );" ;
286- }
287- ss << " \n\t }\n " ;
323+ generate_jsonlink ( false , ss, obj_type, cur_obj );
288324 }
289-
290325 ss << " };" ;
291326 if ( config.enable_comments ) {
292327 ss << " \t // " << obj_type;
293328 }
294329 ss << " \n\n " ;
295-
330+ if ( config.enable_jsonlink ) {
331+ generate_jsonlink ( true , ss, obj_type, cur_obj );
332+ }
296333 }
297334 return ss.str ( );
298335 }
@@ -305,7 +342,7 @@ namespace daw {
305342 std::string result = generate_code ( obj_info, config );
306343
307344 return result;
308-
345+
309346 }
310347 } // namespace json_to_cpp
311348} // namespace daw
0 commit comments