Sivelkiria Operating System repository

Commit 5938cda4 authored by Alexey's avatar Alexey
Browse files

изменено: libs/programming_languages/cpp/cppGenerator.cpp

	изменено:      libs/programming_languages/cpp/cppGenerator.hpp
	изменено:      libs/programming_languages/cpp/cppInterfaceGenerator.cpp
	изменено:      libs/programming_languages/cpp/cppStructGenerator.cpp
	изменено:      main.cpp
parent a57e073f
#include "cppGenerator.hpp" #include "cppGenerator.hpp"
#include <fstream>
#include <sstream> #include <sstream>
using namespace Sivelkiria::sdkBuilder; using namespace Sivelkiria::sdkBuilder;
void cppGenerator::construct( std::string cppGenerator::generateTemplate(
const std::vector<std::string> &descr_templ, const std::vector<std::string> &descr_templ,
const std::vector<templateHelper> &templ, const bool &has_template, const std::vector<templateHelper> &templ, const bool &has_template,
const std::string type_obj, const std::string name, const std::string type_obj ) {
const std::string &body ) {
std::ostringstream _templ; std::ostringstream _templ;
std::ostringstream description_template; std::ostringstream description_template;
if ( has_template ) { if ( has_template ) {
...@@ -29,21 +27,19 @@ void cppGenerator::construct( ...@@ -29,21 +27,19 @@ void cppGenerator::construct(
_templ << ' '; //вставляем вместо последней запятой _templ << ' '; //вставляем вместо последней запятой
_templ << '>' << std::endl << description_template.str(); _templ << '>' << std::endl << description_template.str();
} }
construct_hpp( _templ.str(), type_obj, body, name ); return _templ.str();
} }
void cppGenerator::construct_hpp( std::vector<std::string>
const std::string _template, const std::string type_obj, cppGenerator::split( const std::string &str, const std::string &delim ) {
const std::string &body, const std::string &name ) { std::vector<std::string> tokens;
const std::string filename= build_dir + name + ".hpp"; size_t prev= 0, pos= 0;
std::ofstream hpp_file( filename ); do {
hpp_file << "namespace SivDK{" << std::endl << "namespace "; pos= str.find( delim, prev );
if ( type_obj == "class" ) hpp_file << "Interfaces"; if ( pos == std::string::npos ) pos= str.length();
else if ( type_obj == "struct" ) std::string token= str.substr( prev, pos - prev );
hpp_file << "Structures"; if ( !token.empty() ) tokens.push_back( token );
else prev= pos + delim.length();
hpp_file << "Error"; } while ( pos < str.length() && prev < str.length() );
hpp_file << "{" << std::endl << _template; return tokens;
hpp_file << body << "};" << std::endl;
hpp_file << "};" << std::endl << "};";
} }
\ No newline at end of file
...@@ -21,23 +21,10 @@ namespace Sivelkiria { namespace sdkBuilder { ...@@ -21,23 +21,10 @@ namespace Sivelkiria { namespace sdkBuilder {
*\param [in] body body of object *\param [in] body body of object
*\return void *\return void
*/ */
void construct( std::string generateTemplate(
const std::vector<std::string> &descr_templ, const std::vector<std::string> &descr_templ,
const std::vector<templateHelper> &templ, const bool &has_template, const std::vector<templateHelper> &templ, const bool &has_template,
const std::string type_obj, const std::string name, const std::string type_obj );
const std::string &body );
/*\brief write hpp file???!!!
*\details write to build_dir/objname.hpp
*\date 05.2020
*\param [in] _template template code
*\param [in] type_obj struct or class
*\param [in] body body of object
*\param [in] name name of object
*\return void
*/
void construct_hpp(
const std::string _template, const std::string type_obj,
const std::string &body, const std::string &name );
/*\brief virtual abstract method for write hpp file???!!! /*\brief virtual abstract method for write hpp file???!!!
*\date 05.2020 *\date 05.2020
*\param [in] filename *\param [in] filename
...@@ -46,7 +33,13 @@ namespace Sivelkiria { namespace sdkBuilder { ...@@ -46,7 +33,13 @@ namespace Sivelkiria { namespace sdkBuilder {
*/ */
virtual void virtual void
build( const std::string &filename, const std::string out_dir )= 0; build( const std::string &filename, const std::string out_dir )= 0;
/* /*\brief split str by delim to array
*\date 05.2020
*\param [in] str - input string
*\param [in] delim - delimiter (example: ' ', '.')
*\return std::vector<std::string>
*/ */
std::vector<std::string>
split( const std::string &str, const std::string &delim );
}; };
}} // namespace Sivelkiria::sdkBuilder }} // namespace Sivelkiria::sdkBuilder
\ No newline at end of file
#include "cppInterfaceGenerator.hpp" #include "cppInterfaceGenerator.hpp"
#include <sstream> #include <sstream>
#include <fstream>
using namespace Sivelkiria::sdkBuilder; using namespace Sivelkiria::sdkBuilder;
std::vector<std::string> split(const std::string& str, const std::string& delim);
std::string cppInterfaceGenerator::generate_body( std::string cppInterfaceGenerator::generate_body(
const interfaceParser &interface, const std::string normal_name, const interfaceParser &interface, const std::string normal_name,
int minor ) { int minor ) {
...@@ -14,7 +13,7 @@ std::string cppInterfaceGenerator::generate_body( ...@@ -14,7 +13,7 @@ std::string cppInterfaceGenerator::generate_body(
else { else {
code << name; code << name;
if ( minor > 1 ) if ( minor > 1 )
code << "_" << ( minor - 1 ); //наcледуемся от предыдущей версии code << '_' << ( minor - 1 ); //наcледуемся от предыдущей версии
} }
//тут еще описания методов добавить //тут еще описания методов добавить
if ( minor != 0 ) name= name + "_" + std::to_string( minor ); if ( minor != 0 ) name= name + "_" + std::to_string( minor );
...@@ -29,12 +28,12 @@ std::string cppInterfaceGenerator::generate_body( ...@@ -29,12 +28,12 @@ std::string cppInterfaceGenerator::generate_body(
if ( std::stoi( it->minor_since ) == minor ) { if ( std::stoi( it->minor_since ) == minor ) {
code << "/*" << std::endl << gt->descr_main << std::endl; code << "/*" << std::endl << gt->descr_main << std::endl;
code << "return_type:" << gt->descr_ret << std::endl << "*/"; code << "return_type:" << gt->descr_ret << std::endl << "*/";
code << std::endl << "\t"; code << std::endl << '\t';
//где-то тут вывести описания аргументов //где-то тут вывести описания аргументов
code << "virtual " << it->ret.first << " "; code << "virtual " << it->ret.first << ' ';
//каким-то образом запретить обращаться по правилам //каким-то образом запретить обращаться по правилам
//для вложенных allow friend методы? //для вложенных allow friend методы?
code << it->name << "("; //вывели имя метода code << it->name << '('; //вывели имя метода
write_args( code, it ); write_args( code, it );
code << ") = 0;" << std::endl; code << ") = 0;" << std::endl;
} }
...@@ -63,42 +62,26 @@ void cppInterfaceGenerator::build( ...@@ -63,42 +62,26 @@ void cppInterfaceGenerator::build(
const std::string &filename, const std::string out_dir ) { const std::string &filename, const std::string out_dir ) {
build_dir= out_dir; build_dir= out_dir;
interfaceParser interface( filename ); interfaceParser interface( filename );
std::vector<std::string> v = split (interface.name, "."); std::vector<std::string> v= split( interface.name, "." );
std::string name= v.back() + "_" + interface.major; std::string name= v.back() + '_' + interface.major;
v.pop_back(); v.pop_back();
std::ostringstream body; const std::string hppfile= build_dir + name + ".hpp";
for (auto i : v) std::ofstream hpp( hppfile );
body << "namespace " << i << " {" << std::endl; hpp << "namespace SivDK {" << std::endl;
for ( auto i: v ) hpp << "namespace " << i << " {" << std::endl;
std::string templ= generateTemplate(
interface.description_template_names, interface.templates,
interface.has_template, "class" );
for ( int i= 0; i <= stoi( interface.minor ); ++i ) { for ( int i= 0; i <= stoi( interface.minor ); ++i ) {
body << "class " << name; hpp << templ << "class " << name;
if ( i != 0 ) body << "_" << i; if ( i != 0 ) hpp << "_" << i;
body << " : public virtual " hpp << " : public virtual "
<< generate_body( << generate_body(
interface, name, interface, name,
i ); //нужно генерить несколько классов для разных минорных i ); //нужно генерить несколько классов для разных минорных
//версий //версий
} }
for(auto i : v) for ( auto i: v ) hpp << '}' << std::endl;
body << "}" << std::endl; hpp << '}' << std::endl << '}' << std::endl << '}' << std::endl;
construct(
interface.description_template_names, interface.templates,
interface.has_template, "class", name, body.str() );
} }
std::vector<std::string> split(const std::string& str, const std::string& delim)
{
std::vector<std::string> tokens;
size_t prev = 0, pos = 0;
do
{
pos = str.find(delim, prev);
if (pos == std::string::npos) pos = str.length();
std::string token = str.substr(prev, pos-prev);
if (!token.empty()) tokens.push_back(token);
prev = pos + delim.length();
}
while (pos < str.length() && prev < str.length());
return tokens;
}
\ No newline at end of file
#include "cppStructGenerator.hpp" #include "cppStructGenerator.hpp"
#include <sstream> #include <sstream>
#include <fstream>
using namespace Sivelkiria::sdkBuilder; using namespace Sivelkiria::sdkBuilder;
std::vector<std::string>
split( const std::string &str, const std::string &delim );
std::string cppStructGenerator::generate_body( std::string cppStructGenerator::generate_body(
const structParser &structure, const std::string name, const int minor ) { const structParser &structure, const std::string name, const int minor ) {
std::ostringstream code; std::ostringstream code;
if ( minor != 0 ) { if ( minor != 0 ) {
code << " : public " << name; //наследуем от 0-й мажорной версии code << " : public " << name; //наследуем от 0-й мажорной версии
if ( minor > 1 )
code << '_' << ( minor - 1 ); //наcледуемся от предыдущей версии
} }
code << " {" << std::endl; code << " {" << std::endl;
auto gt= structure.fields_descriptions.begin(); auto gt= structure.fields_descriptions.begin();
...@@ -23,12 +28,32 @@ void cppStructGenerator::build( ...@@ -23,12 +28,32 @@ void cppStructGenerator::build(
const std::string &filename, const std::string out_dir ) { const std::string &filename, const std::string out_dir ) {
build_dir= out_dir; build_dir= out_dir;
structParser structure( filename ); structParser structure( filename );
std::string name= structure.name.substr( structure.name.find( "." ) + 1 ); std::vector<std::string> v= split( structure.name, "." );
name += "_" + structure.major; std::string name= v.back() + '_' + structure.major;
std::ostringstream body; v.pop_back();
body << "struct " << name; const std::string hppfile= build_dir + name + ".hpp";
body << generate_body( structure, name, 1 ); std::ofstream hpp( hppfile );
construct( hpp << "namespace SivDK {" << std::endl;
for ( auto i: v ) hpp << "namespace " << i << " {" << std::endl;
std::string templ= generateTemplate(
structure.description_template_names, structure.templates, structure.description_template_names, structure.templates,
structure.has_template, "struct", name, body.str() ); structure.has_template, "struct" );
hpp << "struct " << name;
hpp << generate_body( structure, name, 0 );
for ( auto i: v ) hpp << '}' << std::endl;
hpp << '}' << std::endl << '}' << std::endl;
}
std::vector<std::string>
split( const std::string &str, const std::string &delim ) {
std::vector<std::string> tokens;
size_t prev= 0, pos= 0;
do {
pos= str.find( delim, prev );
if ( pos == std::string::npos ) pos= str.length();
std::string token= str.substr( prev, pos - prev );
if ( !token.empty() ) tokens.push_back( token );
prev= pos + delim.length();
} while ( pos < str.length() && prev < str.length() );
return tokens;
} }
\ No newline at end of file
...@@ -4,6 +4,6 @@ int main() { ...@@ -4,6 +4,6 @@ int main() {
// vec.push_back( "../interfaces/Interface/ExampleCalculator.0.xml" ); // vec.push_back( "../interfaces/Interface/ExampleCalculator.0.xml" );
// vec.push_back( "../interfaces/Structure/ExampleTemperature.0.xml" ); // vec.push_back( "../interfaces/Structure/ExampleTemperature.0.xml" );
std::string filename= "../interfaces/Interface/ExampleCalculator.0.xml"; std::string filename= "../interfaces/Interface/ExampleCalculator.0.xml";
// std::string filename= "../interfaces/Structure/ExampleTemperature.0.xml"; // std::string filename= "../interfaces/Structure/ExampleTemperature.0.xml";
Sivelkiria::sdkBuilder::build( filename, "./" ); Sivelkiria::sdkBuilder::build( filename, "./" );
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment