PhoenixGenerator  3.4.0
Generate code from template files and projects in C++, Rust, Python and wrapped C++ to Python
Loading...
Searching...
No Matches
CppTraitDataStream.cpp
Go to the documentation of this file.
1
2/***************************************
3 Auteur : Pierre Aubert
4 Mail : pierre.aubert@lapp.in2p3.fr
5 Licence : LGPL-3.0-only
6****************************************/
7
8#include "type_utils.h"
10
15
18
20
23void CppTraitDataStream::headerExtraInclude(std::ofstream & fs, const GeneratorMode & mode) const{
24 if(!mode.enableDataStream){return;}
25 fs << "#include <array>" << std::endl;
26 fs << "#include <algorithm>" << std::endl;
27 fs << "#include \"phoenix_data_stream.h\"" << std::endl;
28}
29
31
35void CppTraitDataStream::publicMethodDeclaration(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
36 if(!mode.enableDataStream || classConfig.getIsEnum()){return;}
37 PString name(classConfig.getName());
38 fs << std::endl;
39 fs << "\t\t///Load the current "<<name<<" with a stream" << std::endl;
40 fs << "\t\t/** @param[out] ds : stream to be used" << std::endl;
41 fs << "\t\t * @return true on success, false otherwise" << std::endl;
42 fs << "\t\t*/" << std::endl;
43 fs << "\t\ttemplate<typename Stream, DataStreamMode::DataStreamMode Mode>" << std::endl;
44 fs << "\t\tbool readWriteStream(Stream & ds){" << std::endl;
45
46 const PVecClassAttribute & vecAttr(classConfig.getListAttribute());
47 if(vecAttr.size() == 0lu){
48 fs << "\t\t\treturn true;" << std::endl;
49 }else{
50 //Neither "b = f() && b" (each && counts toward SonarQube's cognitive complexity) nor "b &= f()" (SonarQube flags bitwise ops on bool) scale to many attributes.
51 //Collecting every (always-evaluated, no short-circuit) result in an array and reducing it with a single std::all_of keeps cognitive complexity constant regardless of attribute count.
52 fs << "\t\t\tconst std::array<bool, " << vecAttr.size() << "> vecIsOk{" << std::endl;
53 PString separator("\t\t\t\t");
54 for(PVecClassAttribute::const_iterator it(vecAttr.begin()); it != vecAttr.end(); ++it){
55 fs << separator << "streamAttribute<Stream, Mode>(ds, p_" << it->getName() << ")";
56 separator = ",\n\t\t\t\t";
57 }
58 fs << std::endl << "\t\t\t};" << std::endl;
59 fs << "\t\t\treturn std::all_of(vecIsOk.begin(), vecIsOk.end(), [](bool isOk){return isOk;});" << std::endl;
60 }
61 fs << "\t\t}" << std::endl << std::endl;
62}
63
65
69void CppTraitDataStream::publicMethodImplementation(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
70 if(!mode.enableDataStream){return;}
71
72}
73
75
79void CppTraitDataStream::protectedMethodDeclaration(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
80 if(!mode.enableDataStream || classConfig.getIsEnum()){return;}
81 fs << std::endl;
82 fs << "\t\t///Streams a single attribute" << std::endl;
83 fs << "\t\t/** @param[out] ds : stream to be used" << std::endl;
84 fs << "\t\t * @param data : attribute to be streamed" << std::endl;
85 fs << "\t\t * @return true on success, false otherwise" << std::endl;
86 fs << "\t\t*/" << std::endl;
87 fs << "\t\ttemplate<typename Stream, DataStreamMode::DataStreamMode Mode, typename T>" << std::endl;
88 fs << "\t\tstatic bool streamAttribute(Stream & ds, T & data){" << std::endl;
89 fs << "\t\t\treturn DataStream<Stream, Mode, T>::data_stream(ds, data);" << std::endl;
90 fs << "\t\t}" << std::endl << std::endl;
91}
92
94
98void CppTraitDataStream::protectedMethodImplementation(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
99
100}
101
103
107void CppTraitDataStream::privateMethodDeclaration(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
108
109}
110
112
116void CppTraitDataStream::privateMethodImplementation(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
117
118}
119
121
125void CppTraitDataStream::classExtraFunctionDeclaration(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
126 if(!mode.enableDataStream){return;}
127 PString name(classConfig.getName());
128 PString fullName(classConfig.getFullName());
129 if(classConfig.getIsEnum()){
130 fs << "PHOENIX_DATA_STREAM_ENUM("<<name<<"::"<<name<<")" << std::endl << std::endl;
131 return;
132 }
133 fs << "///@brief Generic "<<name<<" serialisation/deserialisation, load/save and size function for PhoenixDataStream" << std::endl;
134 fs << "template<typename Stream, DataStreamMode::DataStreamMode Mode>" << std::endl;
135 fs << "struct DataStream<Stream, Mode, "<<fullName<<">{" << std::endl;
136 fs << "\t///Generic function to load/save/serialise/deserialise "<<name << std::endl;
137 fs << "\t/**\t@param[out] ds : stream to be used" << std::endl;
138 fs << "\t * \t@param data : "<<name<<" to be used" << std::endl;
139 fs << "\t * \t@return true on success, false otherwise" << std::endl;
140 fs << "\t*/" << std::endl;
141 fs << "\tstatic bool data_stream(Stream & ds, "<<fullName<<" & data){" << std::endl;
142 fs << "\t\treturn data.readWriteStream<Stream, Mode>(ds);" << std::endl;
143 fs << "\t}" << std::endl;
144 fs << "};" << std::endl << std::endl;
145}
146
148
152void CppTraitDataStream::classExtraFunctionImplementation(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
153 if(!mode.enableDataStream){return;}
154
155}
156
157
158
159
std::vector< PClassAttribute > PVecClassAttribute
Definition PDataConfig.h:13
virtual void protectedMethodImplementation(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of protected methods.
virtual void publicMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Declaration of public methods.
virtual void protectedMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Declaration of protected methods.
virtual void headerExtraInclude(std::ofstream &fs, const GeneratorMode &mode) const
Add extra include on the header.
virtual void classExtraFunctionImplementation(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of extra functions.
CppTraitDataStream()
Consctructor of CppTraitDataStream.
virtual void classExtraFunctionDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Declaration of extra functions.
virtual ~CppTraitDataStream()
Desctructor of CppTraitDataStream.
virtual void privateMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Declaration of private methods.
virtual void privateMethodImplementation(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of private methods.
virtual void publicMethodImplementation(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of public methods.
Class to describe a basic class.
PString getFullName() const
Returns the full name of the class including the namespace.
const std::vector< PClassAttribute > & getListAttribute() const
Returns the list of attributes of the class.
const PString & getName() const
Returns the class name.
bool getIsEnum() const
Say if the current PClassConfig is an enum.
All the genertor modes.
bool enableDataStream
True to enable data stream interface generator.