Skip to content

Commit fb2d3fc

Browse files
committed
Renamed multiple template variables to more meaningful names. Also renamed multiple methods in BaseGenerator to match their template variable names.
1 parent 4465ae3 commit fb2d3fc

File tree

9 files changed

+96
-121
lines changed

9 files changed

+96
-121
lines changed

TODO.txt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
Rename the following:
2-
3-
getFileClassFileName to getFileObjectFileName
4-
getFileClassFilePath to getFileObjectFilePath
5-
getFileClassGetFileNameImplementation to getFileObjectGetFileNameImplementation
6-
getFileClassGetFileNameImplementation to getFileObjectGetFileNameImplementation
7-
bin2cpp_getter_function_name to bin2cpp_file_object_getter_function_name
8-
getGetterFunctionName to getFileObjectGetterFunctionName
9-
101
check implementation of the following to see if they could be simplified:
112
* getFileClassGetFileNameImplementation
123
* getFileClassGetFilePathImplementation
13-
14-
15-
Move TemplateProcessor code to its own static library ?

src/bin2cpp/ArrayGenerator.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ namespace bin2cpp
5656
return false;
5757

5858
const std::string text = ""
59-
"${bin2cpp_output_file_header}"
60-
"#include \"${bin2cpp_header_source_file_include_path}\"\n"
59+
"${bin2cpp_output_file_header_template}"
60+
"#include \"${bin2cpp_header_file_include_path}\"\n"
6161
"#include <iostream>\n"
6262
"#include <fstream> //for ofstream\n"
6363
"namespace ${bin2cpp_namespace}\n"
@@ -76,10 +76,10 @@ namespace bin2cpp
7676
" };\n"
7777
" return (const char *)buffer;\n"
7878
" }\n"
79-
"${bin2cpp_cpp_get_save_method_impl}"
79+
"${bin2cpp_cpp_save_method_template}"
8080
" };\n"
81-
" const ${bin2cpp_baseclass} & ${bin2cpp_getter_function_name}() { static ${bin2cpp_classname} _instance; return _instance; }\n"
82-
"${bin2cpp_cpp_get_file_manager_registration_impl}"
81+
" const ${bin2cpp_baseclass} & ${bin2cpp_file_object_getter_function_name}() { static ${bin2cpp_classname} _instance; return _instance; }\n"
82+
"${bin2cpp_file_manager_cpp_registration_implementation}"
8383
"}; //${bin2cpp_namespace}\n";
8484

8585
TemplateProcessor processor(&text);
@@ -111,18 +111,18 @@ namespace bin2cpp
111111
return false;
112112

113113
const std::string text = ""
114-
"${bin2cpp_output_file_header}"
114+
"${bin2cpp_output_file_header_template}"
115115
"#if defined(_WIN32) && !defined(_CRT_SECURE_NO_WARNINGS)\n"
116116
"#define _CRT_SECURE_NO_WARNINGS\n"
117117
"#endif\n"
118-
"#include \"${bin2cpp_header_source_file_include_path}\"\n"
118+
"#include \"${bin2cpp_header_file_include_path}\"\n"
119119
"#include <stdlib.h> // for malloc\n"
120120
"#include <string.h> // for memset\n"
121121
"#include <stdio.h> // for fopen\n"
122122
"static ${bin2cpp_baseclass} ${bin2cpp_function_identifier_lowercase}_file = { 0 };\n"
123123
"static bool ${bin2cpp_function_identifier_lowercase}_initialized = false;\n"
124124
"\n"
125-
"${bin2cpp_c_file_manager_registration_predeclaration}"
125+
"${bin2cpp_file_manager_c_registration_predeclaration}"
126126
"bool ${bin2cpp_function_identifier_lowercase}_load()\n"
127127
"{\n"
128128
" if ( ${bin2cpp_function_identifier_lowercase}_file.buffer )\n"
@@ -172,15 +172,15 @@ namespace bin2cpp
172172
"\n"
173173
" // load file by default on init as in c++ implementation\n"
174174
" file->load();\n"
175-
"${bin2cpp_c_file_manager_registration_post_init_impl}"
175+
"${bin2cpp_file_manager_c_registration_post_init_implementation}"
176176
"}\n"
177177
"\n"
178-
"${bin2cpp_baseclass}* ${bin2cpp_getter_function_name}(void)\n"
178+
"${bin2cpp_baseclass}* ${bin2cpp_file_object_getter_function_name}(void)\n"
179179
"{\n"
180180
" ${bin2cpp_function_identifier_lowercase}_init();\n"
181181
" return &${bin2cpp_function_identifier_lowercase}_file;\n"
182182
"}\n"
183-
"${bin2cpp_c_file_manager_registration_implementation}";
183+
"${bin2cpp_file_manager_c_registration_implementation}";
184184

185185
TemplateProcessor processor(&text);
186186
processor.setTemplateVariableLookup(this);

src/bin2cpp/BaseGenerator.cpp

Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -58,52 +58,29 @@ namespace bin2cpp
5858

5959
bool BaseGenerator::lookupStringVariable(const std::string& name, std::string& output)
6060
{
61-
if ( name == "bin2cpp_output_file_macro_guard" ) { output = getCppIncludeGuardMacroName(mContext.headerFilename); return true; }
62-
if ( name == "bin2cpp_output_file_header" ) { output = getHeaderTemplate(); return true; }
63-
if ( name == "bin2cpp_file_object_macro_guard_prefix" ) { output = getClassMacroGuardPrefix(); return true; }
61+
if ( name == "bin2cpp_baseclass" ) { output = mContext.baseClass; return true; }
62+
if ( name == "bin2cpp_classname" ) { output = getClassName(); return true; }
63+
if ( name == "bin2cpp_cpp_save_method_template" ) { output = getCppSaveMethodTemplate(); return true; }
64+
if ( name == "bin2cpp_file_manager_c_registration_implementation" && mContext.registerFiles ) { output = getCFileManagerStaticFileRegistrationImplementation(); return true; }
65+
if ( name == "bin2cpp_file_manager_c_registration_post_init_implementation" && mContext.registerFiles ) { output = getCFileManagerRegistrationPostInitImplementation(); return true; }
66+
if ( name == "bin2cpp_file_manager_c_registration_predeclaration" && mContext.registerFiles ) { output = getCFileManagerRegistrationPredeclarationImplementation(); return true; }
67+
if ( name == "bin2cpp_file_manager_cpp_registration_implementation" && mContext.registerFiles ) { output = getCppFileManagerRegistrationImplementationTemplate(); return true; }
6468
if ( name == "bin2cpp_file_manager_file_header" ) { output = getHeaderTemplate(false); return true; }
6569
if ( name == "bin2cpp_file_manager_header_file_name" ) { output = mContext.managerHeaderFilename; return true; }
6670
if ( name == "bin2cpp_file_manager_macro_guard_prefix" ) { output = getFileManagerMacroGuardPrefix(); return true; }
67-
if ( name == "bin2cpp_baseclass" ) { output = mContext.baseClass; return true; }
68-
if ( name == "bin2cpp_classname" ) { output = getClassName(); return true; }
69-
if ( name == "bin2cpp_namespace" ) { output = mContext.codeNamespace; return true; }
70-
//if ( name == "bin2cpp_baseclass_uppercase" ) { output = ra::strings::Uppercase(mContext.baseClass); return true; }
71-
//if ( name == "bin2cpp_classname_uppercase" ) { output = ra::strings::Uppercase(getClassName()); return true; }
72-
//if ( name == "bin2cpp_namespace_uppercase" ) { output = ra::strings::Lowercase(mContext.codeNamespace); return true; }
73-
//if ( name == "bin2cpp_baseclass_lowercase" ) { output = ra::strings::Lowercase(mContext.baseClass); return true; }
74-
//if ( name == "bin2cpp_classname_lowercase" ) { output = ra::strings::Lowercase(getClassName()); return true; }
75-
//if ( name == "bin2cpp_namespace_lowercase" ) { output = ra::strings::Lowercase(mContext.codeNamespace); return true; }
76-
if ( name == "bin2cpp_function_identifier" ) { output = mContext.functionIdentifier; return true; }
71+
if ( name == "bin2cpp_file_object_file_name_impl" ) { output = getFileObjectGetFileNameImplementation(); return true; }
72+
if ( name == "bin2cpp_file_object_file_name" ) { output = getFileObjectFileName(); return true; }
73+
if ( name == "bin2cpp_file_object_file_path_impl" ) { output = getFileObjectGetFilePathImplementation(); return true; }
74+
if ( name == "bin2cpp_file_object_file_path" ) { output = getFileObjectFilePath(); return true; }
75+
if ( name == "bin2cpp_file_object_getter_function_name" ) { output = getFileObjectGetterFunctionName(); return true; }
76+
if ( name == "bin2cpp_file_object_macro_guard_prefix" ) { output = getClassMacroGuardPrefix(); return true; }
7777
if ( name == "bin2cpp_function_identifier_lowercase" ) { output = ra::strings::Lowercase(mContext.functionIdentifier); return true; }
78-
if ( name == "bin2cpp_getter_function_name" ) { output = getGetterFunctionName(); return true; }
79-
if ( name == "bin2cpp_header_source_file_include_path" ) { output = getCppHeaderIncludePath(); return true; }
80-
if ( name == "bin2cpp_file_object_file_name_impl" ) { output = getFileClassGetFileNameImplementation(); return true; }
81-
if ( name == "bin2cpp_file_object_file_path_impl" ) { output = getFileClassGetFilePathImplementation(); return true; }
82-
if ( name == "bin2cpp_file_object_file_name" ) { output = getFileClassFileName(); return true; }
83-
if ( name == "bin2cpp_file_object_file_path" ) { output = getFileClassFilePath(); return true; }
84-
if ( name == "bin2cpp_cpp_get_save_method_impl" ) { output = getCppSaveMethodTemplate(); return true; }
85-
if ( name == "bin2cpp_cpp_get_file_manager_registration_impl" && mContext.registerFiles ) { output = getCppFileManagerRegistrationImplementationTemplate(); return true; }
86-
if ( name == "bin2cpp_c_file_manager_registration_predeclaration" && mContext.registerFiles ) { output = getCFileManagerRegistrationPredeclarationImplementation(); return true; }
87-
if ( name == "bin2cpp_c_file_manager_registration_implementation" && mContext.registerFiles ) { output = getCFileManagerStaticFileRegistrationImplementation(); return true; }
88-
89-
if ( name == "bin2cpp_c_file_manager_registration_post_init_impl" && mContext.registerFiles )
90-
{
91-
std::string tmp;
92-
tmp += " \n";
93-
tmp += " // register when loaded if static initialisation does not work\n";
94-
tmp += " ${bin2cpp_namespace}_filemanager_register_file(file);\n";
95-
output = tmp;
96-
return true;
97-
}
98-
99-
if ( name == "bin2cpp_input_file_size" )
100-
{
101-
//determine file properties
102-
uint32_t file_size = ra::filesystem::GetFileSize(mContext.inputFilePath.c_str());
103-
std::string file_size_str = ra::strings::ToString(file_size);
104-
output = file_size_str;
105-
return true;
106-
}
78+
if ( name == "bin2cpp_function_identifier" ) { output = mContext.functionIdentifier; return true; }
79+
if ( name == "bin2cpp_header_file_include_path" ) { output = getHeaderFileIncludePath(); return true; }
80+
if ( name == "bin2cpp_input_file_size" ) { output = ra::strings::ToString(ra::filesystem::GetFileSize(mContext.inputFilePath.c_str())); return true; }
81+
if ( name == "bin2cpp_namespace" ) { output = mContext.codeNamespace; return true; }
82+
if ( name == "bin2cpp_output_file_header_template" ) { output = getHeaderTemplate(); return true; }
83+
if ( name == "bin2cpp_output_file_macro_guard" ) { output = getIncludeGuardMacroName(mContext.headerFilename); return true; }
10784

10885
// Unknown name
10986
return false;
@@ -124,7 +101,7 @@ namespace bin2cpp
124101
//protected methods
125102
//-------------------------------
126103

127-
std::string BaseGenerator::getGetterFunctionName()
104+
std::string BaseGenerator::getFileObjectGetterFunctionName()
128105
{
129106

130107
std::string getter;
@@ -243,7 +220,7 @@ namespace bin2cpp
243220
std::string output;
244221
output << " typedef const " << mContext.baseClass << " & (*t_func)();\n";
245222
output << " extern bool RegisterFile(t_func iFunctionPointer);\n";
246-
output << " static bool k" << className << "Registered = " << mContext.codeNamespace << "::RegisterFile(&" << getGetterFunctionName() << ");\n";
223+
output << " static bool k" << className << "Registered = " << mContext.codeNamespace << "::RegisterFile(&" << getFileObjectGetterFunctionName() << ");\n";
247224
output << " \n";
248225
return output;
249226
}
@@ -284,6 +261,15 @@ namespace bin2cpp
284261
return output;
285262
}
286263

264+
std::string BaseGenerator::getCFileManagerRegistrationPostInitImplementation()
265+
{
266+
std::string output;
267+
output += " \n";
268+
output += " // register when loaded if static initialisation does not work\n";
269+
output += " ${bin2cpp_namespace}_filemanager_register_file(file);\n";
270+
return output;
271+
}
272+
287273
std::string BaseGenerator::getClassName()
288274
{
289275
std::string functionIdentifier = ra::strings::CapitalizeFirstCharacter(mContext.functionIdentifier);
@@ -307,14 +293,14 @@ namespace bin2cpp
307293
{
308294
//define macro guard a macro matching the filename
309295
std::string output;
310-
output += getCppIncludeGuardMacroName(mContext.codeNamespace.c_str()); //prefix the custom namespace for the file manager
296+
output += getIncludeGuardMacroName(mContext.codeNamespace.c_str()); //prefix the custom namespace for the file manager
311297
if ( !output.empty() )
312298
output += "_";
313-
output += getCppIncludeGuardMacroName(mContext.managerHeaderFilename);
299+
output += getIncludeGuardMacroName(mContext.managerHeaderFilename);
314300
return output;
315301
}
316302

317-
std::string BaseGenerator::getFileClassGetFileNameImplementation()
303+
std::string BaseGenerator::getFileObjectGetFileNameImplementation()
318304
{
319305
std::string output;
320306

@@ -341,7 +327,7 @@ namespace bin2cpp
341327
return output;
342328
}
343329

344-
std::string BaseGenerator::getFileClassGetFilePathImplementation()
330+
std::string BaseGenerator::getFileObjectGetFilePathImplementation()
345331
{
346332
std::string output;
347333

@@ -378,7 +364,7 @@ namespace bin2cpp
378364
return output;
379365
}
380366

381-
std::string BaseGenerator::getFileClassFileName()
367+
std::string BaseGenerator::getFileObjectFileName()
382368
{
383369
std::string output;
384370

@@ -389,7 +375,7 @@ namespace bin2cpp
389375
return output;
390376
}
391377

392-
std::string BaseGenerator::getFileClassFilePath()
378+
std::string BaseGenerator::getFileObjectFilePath()
393379
{
394380
std::string output;
395381

@@ -413,7 +399,7 @@ namespace bin2cpp
413399
{
414400
//if reported path is not specified ?
415401
//report the same as getFileName()
416-
output = getFileClassFileName();
402+
output = getFileObjectFileName();
417403
return output;
418404
}
419405

@@ -422,7 +408,7 @@ namespace bin2cpp
422408
return output;
423409
}
424410

425-
std::string BaseGenerator::getCppHeaderIncludePath()
411+
std::string BaseGenerator::getHeaderFileIncludePath()
426412
{
427413
return mContext.headerFilename;
428414
}
@@ -473,7 +459,7 @@ namespace bin2cpp
473459
bool BaseGenerator::createCppHeaderFile(const char * header_file_path)
474460
{
475461
const std::string text = ""
476-
"${bin2cpp_output_file_header}"
462+
"${bin2cpp_output_file_header_template}"
477463
"#ifndef ${bin2cpp_output_file_macro_guard}\n"
478464
"#define ${bin2cpp_output_file_macro_guard}\n"
479465
"\n"
@@ -494,7 +480,7 @@ namespace bin2cpp
494480
" virtual bool save(const char * filename) const = 0;\n"
495481
" };\n"
496482
" #endif //${bin2cpp_file_object_macro_guard_prefix}_FILE_OBJECT_CLASS\n"
497-
" const ${bin2cpp_baseclass} & ${bin2cpp_getter_function_name}();\n"
483+
" const ${bin2cpp_baseclass} & ${bin2cpp_file_object_getter_function_name}();\n"
498484
"}; //${bin2cpp_namespace}\n"
499485
"\n"
500486
"#endif //${bin2cpp_output_file_macro_guard}\n"
@@ -562,7 +548,7 @@ namespace bin2cpp
562548
bool BaseGenerator::createCHeaderFile(const char* file_path)
563549
{
564550
const std::string text = ""
565-
"${bin2cpp_output_file_header}"
551+
"${bin2cpp_output_file_header_template}"
566552
"#ifndef ${bin2cpp_output_file_macro_guard}\n"
567553
"#define ${bin2cpp_output_file_macro_guard}\n"
568554
"\n"
@@ -587,7 +573,7 @@ namespace bin2cpp
587573
"} ${bin2cpp_baseclass};\n"
588574
"typedef ${bin2cpp_baseclass}* ${bin2cpp_baseclass}Ptr;\n"
589575
"#endif //${bin2cpp_file_object_macro_guard_prefix}_FILE_OBJECT_STRUCT\n"
590-
"${bin2cpp_baseclass}* ${bin2cpp_getter_function_name}(void);\n"
576+
"${bin2cpp_baseclass}* ${bin2cpp_file_object_getter_function_name}(void);\n"
591577
"\n"
592578
"#endif //${bin2cpp_output_file_macro_guard}\n"
593579
;

src/bin2cpp/BaseGenerator.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace bin2cpp
5656

5757
protected:
5858

59-
virtual std::string getGetterFunctionName();
59+
virtual std::string getFileObjectGetterFunctionName();
6060
virtual std::string getHeaderFilePath(const char * cpp_file_path);
6161
virtual std::string getCppFilePath(const char * header_file_path);
6262
virtual std::string getHeaderTemplate();
@@ -65,14 +65,15 @@ namespace bin2cpp
6565
virtual std::string getCppFileManagerRegistrationImplementationTemplate();
6666
virtual std::string getCFileManagerRegistrationPredeclarationImplementation();
6767
virtual std::string getCFileManagerStaticFileRegistrationImplementation();
68+
virtual std::string getCFileManagerRegistrationPostInitImplementation();
6869
virtual std::string getClassName();
6970
virtual std::string getClassMacroGuardPrefix();
7071
virtual std::string getFileManagerMacroGuardPrefix();
71-
virtual std::string getFileClassGetFileNameImplementation();
72-
virtual std::string getFileClassGetFilePathImplementation();
73-
virtual std::string getFileClassFileName();
74-
virtual std::string getFileClassFilePath();
75-
virtual std::string getCppHeaderIncludePath();
72+
virtual std::string getFileObjectGetFileNameImplementation();
73+
virtual std::string getFileObjectGetFilePathImplementation();
74+
virtual std::string getFileObjectFileName();
75+
virtual std::string getFileObjectFilePath();
76+
virtual std::string getHeaderFileIncludePath();
7677
virtual void writeInputFileDataAsCode(std::ostream& output);
7778
virtual void writeInputFileChunkAsCode(const unsigned char * buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk, std::ostream& output);
7879

src/bin2cpp/SegmentGenerator.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ namespace bin2cpp
5555
return false;
5656

5757
const std::string text = ""
58-
"${bin2cpp_output_file_header}"
58+
"${bin2cpp_output_file_header_template}"
5959
"#if defined(_WIN32) && !defined(_CRT_SECURE_NO_WARNINGS)\n"
6060
"#define _CRT_SECURE_NO_WARNINGS\n"
6161
"#endif\n"
62-
"#include \"${bin2cpp_header_source_file_include_path}\"\n"
62+
"#include \"${bin2cpp_header_file_include_path}\"\n"
6363
"#include <string> //for std::string\n"
6464
"#include <iostream>\n"
6565
"#include <fstream> //for ofstream\n"
@@ -79,12 +79,12 @@ namespace bin2cpp
7979
" mBuffer.clear();\n"
8080
" mBuffer.reserve(getSize()); //allocate all required memory at once to prevent reallocations\n${bin2cpp_insert_input_file_as_code}" // INPUT FILE AS CODE HERE
8181
" }\n"
82-
"${bin2cpp_cpp_get_save_method_impl}"
82+
"${bin2cpp_cpp_save_method_template}"
8383
" private:\n"
8484
" std::string mBuffer;\n"
8585
" };\n"
86-
" const ${bin2cpp_baseclass} & ${bin2cpp_getter_function_name}() { static ${bin2cpp_classname} _instance; return _instance; }\n"
87-
"${bin2cpp_cpp_get_file_manager_registration_impl}"
86+
" const ${bin2cpp_baseclass} & ${bin2cpp_file_object_getter_function_name}() { static ${bin2cpp_classname} _instance; return _instance; }\n"
87+
"${bin2cpp_file_manager_cpp_registration_implementation}"
8888
"}; //${bin2cpp_namespace}\n";
8989

9090
TemplateProcessor processor(&text);
@@ -101,18 +101,18 @@ namespace bin2cpp
101101
return false;
102102

103103
const std::string text = ""
104-
"${bin2cpp_output_file_header}"
104+
"${bin2cpp_output_file_header_template}"
105105
"#if defined(_WIN32) && !defined(_CRT_SECURE_NO_WARNINGS)\n"
106106
"#define _CRT_SECURE_NO_WARNINGS\n"
107107
"#endif\n"
108-
"#include \"${bin2cpp_header_source_file_include_path}\"\n"
108+
"#include \"${bin2cpp_header_file_include_path}\"\n"
109109
"#include <stdlib.h> // for malloc\n"
110110
"#include <string.h> // for memset\n"
111111
"#include <stdio.h> // for fopen\n"
112112
"static ${bin2cpp_baseclass} ${bin2cpp_function_identifier_lowercase}_file = { 0 };\n"
113113
"static bool ${bin2cpp_function_identifier_lowercase}_initialized = false;\n"
114114
"\n"
115-
"${bin2cpp_c_file_manager_registration_predeclaration}"
115+
"${bin2cpp_file_manager_c_registration_predeclaration}"
116116
"bool ${bin2cpp_function_identifier_lowercase}_load()\n"
117117
"{\n"
118118
" if ( ${bin2cpp_function_identifier_lowercase}_file.buffer )\n"
@@ -168,15 +168,15 @@ namespace bin2cpp
168168
"\n"
169169
" // load file by default on init as in c++ implementation\n"
170170
" file->load();\n"
171-
"${bin2cpp_c_file_manager_registration_post_init_impl}"
171+
"${bin2cpp_file_manager_c_registration_post_init_implementation}"
172172
"}\n"
173173
"\n"
174-
"${bin2cpp_baseclass}* ${bin2cpp_getter_function_name}(void)\n"
174+
"${bin2cpp_baseclass}* ${bin2cpp_file_object_getter_function_name}(void)\n"
175175
"{\n"
176176
" ${bin2cpp_function_identifier_lowercase}_init();\n"
177177
" return &${bin2cpp_function_identifier_lowercase}_file;\n"
178178
"}\n"
179-
"${bin2cpp_c_file_manager_registration_implementation}";
179+
"${bin2cpp_file_manager_c_registration_implementation}";
180180

181181
TemplateProcessor processor(&text);
182182
processor.setTemplateVariableLookup(this);

0 commit comments

Comments
 (0)