Skip to content

Commit 5da6065

Browse files
author
Andres Sevillano
committed
Get ready for cpp files
1 parent 26cc6fb commit 5da6065

File tree

5 files changed

+130
-15
lines changed

5 files changed

+130
-15
lines changed

include/boilerplateCodeDoc.h

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ namespace boilerplateCodeDoc {
6363
/// @remark Although needed by another class, it's very handy to grab that piece of info here
6464
std::string namespace_id {};
6565

66-
/// @brief Expected C++ header file name to save automatically generated code
66+
/// @brief Expected C++ h/cpp file name to save automatically generated code
6767
/// @remark Although needed by another class, it's very handy to grab that piece of info here
68-
std::string header_filename {};
68+
std::string cpp_filename {};
6969
};
7070

7171

@@ -178,9 +178,9 @@ namespace boilerplateCodeDoc {
178178
std::string css_class {};
179179
};
180180

181-
/// @brief Specific handler to generate HTML tables.
182-
/// @remark Style will be provided by CSS if needed.
183-
struct JsonSchema2CPP final: public JsonSchemaFilter {
181+
/// @brief Specific handler to generate cpp header.
182+
/// @remark Working in sync with JsonSchema2CPP.
183+
struct JsonSchema2H final: public JsonSchemaFilter {
184184

185185
///@brief namespace to isolate autogenerated code
186186
/// @remark to be used as extra parameter
@@ -204,13 +204,43 @@ namespace boilerplateCodeDoc {
204204
// TODO
205205
static constexpr const char* const FOOTER {"\n\n// END: automatically generated code\n"};
206206

207+
///@brief simplest constructor
208+
JsonSchema2H(std::string extra_ = NAMESPACE, std::string header_ = HEADER, std::string footer_ = FOOTER) :
209+
JsonSchemaFilter(extra_, header_, footer_) {}
210+
211+
///@brief filter to apply
212+
bool operator()(const JsonSchema& jsonSchema) override;
213+
};
214+
215+
/// @brief Specific handler to generate cpp file.
216+
/// @remark Working in sync with JsonSchema2H.
217+
struct JsonSchema2CPP final: public JsonSchemaFilter {
218+
219+
///@brief namespace to isolate autogenerated code
220+
/// @remark to be used as extra parameter
221+
static constexpr const char* const NAMESPACE {"boilerplateExample"};
222+
223+
// Expected to be added the name of the file
224+
// /** @file XXXXXXX.cpp
225+
static constexpr const char* const HEADER {R"(
226+
* @brief Example of automatically generated code.
227+
*
228+
* @bug Abuse of JSON Schema validation leads to crushes if an invalid json is queried (missing redundant checks to speed up).
229+
*
230+
*/
231+
)"};
232+
233+
// TODO
234+
static constexpr const char* const FOOTER {"\n\n// END: automatically generated code\n"};
235+
207236
///@brief simplest constructor
208237
JsonSchema2CPP(std::string extra_ = NAMESPACE, std::string header_ = HEADER, std::string footer_ = FOOTER) :
209238
JsonSchemaFilter(extra_, header_, footer_) {}
210239

211240
///@brief filter to apply
212241
bool operator()(const JsonSchema& jsonSchema) override;
213242
};
243+
214244
} // namespace
215245

216246
#endif // header

json/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "version 0.0.1",
55
"type": "object",
66
"namespace": "boilerplate::Example",
7-
"headerFileName": "boilerplateExample.h",
7+
"cppFileName": "boilerplateExample",
88
"cssClass": "boilerplateExample",
99
"metatype": "struct Data",
1010
"properties": {

src/boilerplateCodeDoc.cpp

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ boilerplateCodeDoc::JsonSchema::JsonSchema(std::string filename)
115115
if( document.HasMember("cssClass") ) {
116116
css_class = document["cssClass"].GetString();
117117
}
118-
if( document.HasMember("headerFileName") ) {
119-
header_filename = document["headerFileName"].GetString();
118+
if( document.HasMember("cppFileName") ) {
119+
cpp_filename = document["cppFileName"].GetString();
120120
}
121121

122122
error = boilerplateCodeDoc::ParseErrorCode::OK;
@@ -445,9 +445,9 @@ return boilerplateOperator(jsonSchema, *this, jsonSchema.css_class, [](const Pro
445445
/****************************************************************************************/
446446
/****************************************************************************************/
447447

448-
bool boilerplateCodeDoc::JsonSchema2CPP::operator()(const boilerplateCodeDoc::JsonSchema& jsonSchema)
448+
bool boilerplateCodeDoc::JsonSchema2H::operator()(const boilerplateCodeDoc::JsonSchema& jsonSchema)
449449
{
450-
if( not jsonSchema.header_filename.empty() && not header.empty() ) { header = "/** @file " + jsonSchema.header_filename + header; }
450+
if( not jsonSchema.cpp_filename.empty() && not header.empty() ) { header = "/** @file " + jsonSchema.cpp_filename + ".h" + header; }
451451
return boilerplateOperator(jsonSchema, *this, jsonSchema.namespace_id, [](const Properties& properties, std::string& filtered, std::string& namespace_id) {
452452

453453
if(properties.size() > 0) {
@@ -492,3 +492,60 @@ return boilerplateOperator(jsonSchema, *this, jsonSchema.namespace_id, [](const
492492

493493
}); // return boilerplateOperator
494494
} // operator()
495+
496+
/****************************************************************************************/
497+
/****************************************************************************************/
498+
/****************************************************************************************/
499+
/*********************** ACTUAL LOGIC CODE **********************************************/
500+
/****************************************************************************************/
501+
/****************************************************************************************/
502+
/****************************************************************************************/
503+
504+
bool boilerplateCodeDoc::JsonSchema2CPP::operator()(const boilerplateCodeDoc::JsonSchema& jsonSchema)
505+
{
506+
if( not jsonSchema.cpp_filename.empty() && not header.empty() ) { header = "/** @file " + jsonSchema.cpp_filename + ".cpp" + header; }
507+
return boilerplateOperator(jsonSchema, *this, jsonSchema.namespace_id, [](const Properties& properties, std::string& filtered, std::string& namespace_id) {
508+
509+
/*
510+
if(properties.size() > 0) {
511+
512+
// supposed metatype is a must
513+
std::string parentMetatype {};
514+
parentMetatype = properties.begin()->second.parentMetatype;
515+
if( parentMetatype.empty() ) { return; } // required
516+
517+
// if nothing is implemented, do nothing
518+
bool nothing_implemented {true};
519+
for(const auto& p : properties) {
520+
if( implemented(p.second.metainfo) ) { nothing_implemented = false; break; }
521+
}
522+
if( nothing_implemented ) {
523+
filtered = "\n// " + parentMetatype + ": all their properties are not implemented\n\n" + filtered;
524+
return;
525+
}
526+
527+
std::string addition {};
528+
if( not namespace_id.empty() ) { addition += "namespace " + namespace_id + " {\n"; }
529+
addition += "\n" + parentMetatype + " {\n\n";
530+
531+
for(const auto& p : properties) {
532+
533+
if( not implemented(p.second.metainfo) ) { addition += "// " + p.second.name + ": " + p.second.metainfo + "\n"; continue; }
534+
535+
std::string metatype {p.second.metatype};
536+
if( metatype.empty() ) { continue; } // required
537+
std::string name {p.second.name};
538+
if( name.empty() ) { continue; } // required
539+
540+
if( not p.second.description.empty() ) { addition += "///@ brief " + p.second.description + "\n"; }
541+
addition += metatype + " " + name + " {};\n";
542+
}
543+
544+
addition += "\n}; // " + parentMetatype + "\n";
545+
if( not namespace_id.empty() ) { addition += "\n} // namespace " + namespace_id + "\n\n"; }
546+
547+
filtered = addition + filtered;
548+
}
549+
*/
550+
}); // return boilerplateOperator
551+
} // operator()

src/tool.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ int main(int argc, char** argv)
4545

4646
std::string schema{argv[1]};
4747
std::string html{argv[2]};
48-
std::string cpp{argv[3]};
48+
std::string h{argv[3]};
4949

5050
JsonSchema jsonSchema{schema};
5151
if(jsonSchema.error != ParseErrorCode::OK) { std::cout << jsonSchema.message << std::endl; return 1; }
5252

53-
JsonSchema2CPP cppFilter{};
54-
bool resultCPP = processFilter(jsonSchema, cpp, cppFilter);
53+
JsonSchema2H hFilter{};
54+
bool resultH = processFilter(jsonSchema, h, hFilter);
5555

5656
JsonSchema2HTML htmlFilter{};
5757
bool resultHTML = processFilter(jsonSchema, html, htmlFilter);
5858

59-
return (resultCPP && resultHTML) ? 0 : 1;
59+
return (resultH && resultHTML) ? 0 : 1;
6060
}

test/main.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,34 @@ BOOST_AUTO_TEST_CASE( test002 ) {
155155
}
156156

157157
BOOST_AUTO_TEST_CASE( test003 ) {
158-
BOOST_TEST_MESSAGE( "\ntest003: Transform external Json Schema into C++ structure");
158+
BOOST_TEST_MESSAGE( "\ntest003: Transform external Json Schema into C++ structure header");
159+
160+
// taken for granted that CMake copied default json schema file in the very directory where this test binary is generated
161+
std::string filename{"schema.json"};
162+
std::string binary{boost::unit_test::framework::master_test_suite().argv[0]};
163+
size_t found = binary.find_last_of("/\\");
164+
if( found > 0 ) {
165+
BOOST_TEST_MESSAGE( "found=" << found);
166+
filename = binary.substr(0,found+1) + filename;
167+
}
168+
169+
int argc =boost::unit_test::framework::master_test_suite().argc;
170+
if( argc > 1) {
171+
filename = std::string(boost::unit_test::framework::master_test_suite().argv[1]);
172+
}
173+
BOOST_TEST_MESSAGE( "current binary=" << binary );
174+
BOOST_TEST_MESSAGE( "schema.json=" << filename );
175+
176+
boiler::JsonSchema jsonSchema{filename};
177+
BOOST_TEST_MESSAGE( "Json Schema: " << jsonSchema.message);
178+
boiler::JsonSchema2H handler {};
179+
bool result = handler(jsonSchema);
180+
BOOST_TEST_MESSAGE( "JsonSchema2H: " << handler.message << "\n\n" << handler.filtered);
181+
BOOST_CHECK( result );
182+
}
183+
184+
BOOST_AUTO_TEST_CASE( test004 ) {
185+
BOOST_TEST_MESSAGE( "\ntest004: Transform external Json Schema into C++ structure file");
159186

160187
// taken for granted that CMake copied default json schema file in the very directory where this test binary is generated
161188
std::string filename{"schema.json"};
@@ -180,3 +207,4 @@ BOOST_AUTO_TEST_CASE( test003 ) {
180207
BOOST_TEST_MESSAGE( "JsonSchema2CPP: " << handler.message << "\n\n" << handler.filtered);
181208
BOOST_CHECK( result );
182209
}
210+

0 commit comments

Comments
 (0)