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 <fstream>
#include <sstream>
using namespace Sivelkiria::sdkBuilder;
void cppGenerator::construct(
std::string cppGenerator::generateTemplate(
const std::vector<std::string> &descr_templ,
const std::vector<templateHelper> &templ, const bool &has_template,
const std::string type_obj, const std::string name,
const std::string &body ) {
const std::string type_obj ) {
std::ostringstream _templ;
std::ostringstream description_template;
if ( has_template ) {
......@@ -29,21 +27,19 @@ void cppGenerator::construct(
_templ << ' '; //вставляем вместо последней запятой
_templ << '>' << std::endl << description_template.str();
}
construct_hpp( _templ.str(), type_obj, body, name );
return _templ.str();
}
void cppGenerator::construct_hpp(
const std::string _template, const std::string type_obj,
const std::string &body, const std::string &name ) {
const std::string filename= build_dir + name + ".hpp";
std::ofstream hpp_file( filename );
hpp_file << "namespace SivDK{" << std::endl << "namespace ";
if ( type_obj == "class" ) hpp_file << "Interfaces";
else if ( type_obj == "struct" )
hpp_file << "Structures";
else
hpp_file << "Error";
hpp_file << "{" << std::endl << _template;
hpp_file << body << "};" << std::endl;
hpp_file << "};" << std::endl << "};";
std::vector<std::string>
cppGenerator::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
......@@ -21,23 +21,10 @@ namespace Sivelkiria { namespace sdkBuilder {
*\param [in] body body of object
*\return void
*/
void construct(
std::string generateTemplate(
const std::vector<std::string> &descr_templ,
const std::vector<templateHelper> &templ, const bool &has_template,
const std::string type_obj, const std::string name,
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 );
const std::string type_obj );
/*\brief virtual abstract method for write hpp file???!!!
*\date 05.2020
*\param [in] filename
......@@ -46,7 +33,13 @@ namespace Sivelkiria { namespace sdkBuilder {
*/
virtual void
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
\ No newline at end of file
#include "cppInterfaceGenerator.hpp"
#include <sstream>
#include <fstream>
using namespace Sivelkiria::sdkBuilder;
std::vector<std::string> split(const std::string& str, const std::string& delim);
std::string cppInterfaceGenerator::generate_body(
const interfaceParser &interface, const std::string normal_name,
int minor ) {
......@@ -14,7 +13,7 @@ std::string cppInterfaceGenerator::generate_body(
else {
code << name;
if ( minor > 1 )
code << "_" << ( minor - 1 ); //наcледуемся от предыдущей версии
code << '_' << ( minor - 1 ); //наcледуемся от предыдущей версии
}
//тут еще описания методов добавить
if ( minor != 0 ) name= name + "_" + std::to_string( minor );
......@@ -29,12 +28,12 @@ std::string cppInterfaceGenerator::generate_body(
if ( std::stoi( it->minor_since ) == minor ) {
code << "/*" << std::endl << gt->descr_main << 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 методы?
code << it->name << "("; //вывели имя метода
code << it->name << '('; //вывели имя метода
write_args( code, it );
code << ") = 0;" << std::endl;
}
......@@ -63,42 +62,26 @@ void cppInterfaceGenerator::build(
const std::string &filename, const std::string out_dir ) {
build_dir= out_dir;
interfaceParser interface( filename );
std::vector<std::string> v = split (interface.name, ".");
std::string name= v.back() + "_" + interface.major;
std::vector<std::string> v= split( interface.name, "." );
std::string name= v.back() + '_' + interface.major;
v.pop_back();
std::ostringstream body;
for (auto i : v)
body << "namespace " << i << " {" << std::endl;
const std::string hppfile= build_dir + name + ".hpp";
std::ofstream hpp( hppfile );
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 ) {
body << "class " << name;
if ( i != 0 ) body << "_" << i;
body << " : public virtual "
<< generate_body(
interface, name,
i ); //нужно генерить несколько классов для разных минорных
//версий
hpp << templ << "class " << name;
if ( i != 0 ) hpp << "_" << i;
hpp << " : public virtual "
<< generate_body(
interface, name,
i ); //нужно генерить несколько классов для разных минорных
//версий
}
for(auto i : v)
body << "}" << std::endl;
construct(
interface.description_template_names, interface.templates,
interface.has_template, "class", name, body.str() );
for ( auto i: v ) hpp << '}' << std::endl;
hpp << '}' << std::endl << '}' << 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
#include "cppStructGenerator.hpp"
#include <sstream>
#include <fstream>
using namespace Sivelkiria::sdkBuilder;
std::vector<std::string>
split( const std::string &str, const std::string &delim );
std::string cppStructGenerator::generate_body(
const structParser &structure, const std::string name, const int minor ) {
std::ostringstream code;
if ( minor != 0 ) {
code << " : public " << name; //наследуем от 0-й мажорной версии
if ( minor > 1 )
code << '_' << ( minor - 1 ); //наcледуемся от предыдущей версии
}
code << " {" << std::endl;
auto gt= structure.fields_descriptions.begin();
......@@ -23,12 +28,32 @@ void cppStructGenerator::build(
const std::string &filename, const std::string out_dir ) {
build_dir= out_dir;
structParser structure( filename );
std::string name= structure.name.substr( structure.name.find( "." ) + 1 );
name += "_" + structure.major;
std::ostringstream body;
body << "struct " << name;
body << generate_body( structure, name, 1 );
construct(
std::vector<std::string> v= split( structure.name, "." );
std::string name= v.back() + '_' + structure.major;
v.pop_back();
const std::string hppfile= build_dir + name + ".hpp";
std::ofstream hpp( hppfile );
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.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() {
// vec.push_back( "../interfaces/Interface/ExampleCalculator.0.xml" );
// vec.push_back( "../interfaces/Structure/ExampleTemperature.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, "./" );
}
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