void be_root::Generate (be_ClientHeader & source) { be_CppFwdDecl::GenerateAllWithinScope (source, be_CppEnclosingScope()); ostream& os = source.Stream (); TList<be_Type *>::iterator bit; TList<be_exception *>::iterator git; for (bit = fwdDecls.begin(); bit != fwdDecls.end(); bit++) { (*bit)->GenerateFwdDecls(source); } os << nl; GenerateGlobalTypes (source); GenerateGlobalDecls (source); g_generatorList.GenerateGlobal (source); if (BE_Globals::gen_externalization) { os << nl; for (bit = streamOps.begin(); bit != streamOps.end(); bit++) { (*bit)->GenerateStreamOps(source); } } os << nl; for (git = globalDecls.begin(); git != globalDecls.end(); git++) { (*git)->GenerateGlobalDecls (source); } os << nl; for (bit = typedefs.begin(); bit != typedefs.end(); bit++) { (*bit)->GenerateGlobalTypedef (source); } os << nl; if(BE_Globals::isocpp_test_methods) { //Generate file to stop missing file error DDS_StdString BaseFilename; BaseFilename = StripExtension(source.Filename()); BaseFilename += "_testmethod.h"; be_Source testsource; if(!testsource.Open(BaseFilename)) cerr << "Cannot open: " << BaseFilename << endl; testsource.Close(); //os << nl << "#ifndef " << Ifndefize(BE_Globals::ClientHeaderFilename + "DCPS"); os << nl << "#ifndef " << Ifndefize(StripExtension(FindFilename(source.Filename())) + "DCPS.h"); os << nl << "#ifndef " << Ifndefize(StripExtension(FindFilename(source.Filename())) + "_testmethod.h"); os << nl << "#define " << Ifndefize(StripExtension(FindFilename(source.Filename())) + "_testmethod.h"); os << nl << "#include \"" << StripExtension(FindFilename(source.Filename())) + "_testmethod.h" << "\""; os << nl << "#endif " << nl << "#endif" << nl; } if (BE_Globals::lite) { DDS_StdString BaseFilename; BaseFilename = StripExtension(source.Filename()); os << "\n#include \"" << BaseFilename << "-lite.h" << "\"" << nl; } os << nl << "#endif" << nl; source.Close(); }
void be_structure::Generate (be_ClientHeader& source) { if (BE_Globals::ignore_interfaces && IsInterfaceDependant ()) { return; } if (!Generated()) { be_root::AddAnyOps(*this); be_root::AddPutGetOps(*this); be_root::AddStreamOps(*this); be_root::AddTypedef(*this); be_root::AddTypecode(*this); ostream & os = source.Stream(); DDS_StdString scopedName = ScopedName(); DDS_StdString relativeName; be_Tab tab(source); TList<be_field *>::iterator mit; be_field * field; Generated (pbtrue); be_root::GenerateDependants(source, SequenceMemberTypeName(), EnclosingScope()); g_cppScopeStack.Push(m_cppType); // struct definition os << nl; os << tab << (BE_Globals::isocpp_new_types ? "class " : "struct ") << DLLMACRO << LocalName () << (BE_Globals::isocpp_new_types ? " OSPL_DDS_FINAL" : "") << nl; os << tab << "{" << nl; if (BE_Globals::isocpp_new_types) os << tab << "public:" << nl; source.Indent(); // declare nested types source.Indent(); be_CodeGenerator::Generate(source); // member accessor functions for isocpp new types if (BE_Globals::isocpp_new_types) { /** @internal * @todo OSPL-3369 Repetition; the sort of code people go to hell * for (rightfully); is_sequency is already evideantally a stupid name; &c... */ // Constructors if (m_fields.size() > 0) { os << tab << LocalName() << "() {}" << nl; os << tab << "explicit " << LocalName() << "(" << nl; for (mit = m_fields.begin(); mit != m_fields.end(); mit++) { field = *mit; bool is_sequency = !(field->get_be_type()->IsPrimitiveType() || field->get_be_type()->IsEnumeratedType()); relativeName = BE_Globals::RelativeScope (scopedName, field->StructMemberTypeName ()); TList<be_field *>::iterator final_field = m_fields.end(); --final_field; os << tab << tab << (is_sequency ? "const " : "") << relativeName << (is_sequency ? "& " : " ") << field->get_local_name(); if (mit == final_field) os << ")"; else os <<"," << nl; } os << tab << ":" << nl; for (mit = m_fields.begin(); mit != m_fields.end(); mit++) { field = *mit; TList<be_field *>::iterator final_field = m_fields.end(); --final_field; os << tab << tab << tab << field->get_local_name() << "_(" << field->get_local_name() << ")" << (mit == final_field ? " {}" : ",") << nl; } } // C++ 11 move constructor, copy consructor, and assignement ops source.Outdent(); os << "#ifdef OSPL_DDS_CXX11" << nl; os << "# ifdef OSPL_CXX11_NO_FUNCTION_DEFAULTS" << nl; source.Indent(); os << tab << LocalName() << "(const " << LocalName() << "& _other)" << nl; if (m_fields.size() > 0) { os << tab << ":" << nl; for (mit = m_fields.begin(); mit != m_fields.end(); mit++) { field = *mit; TList<be_field *>::iterator final_field = m_fields.end(); --final_field; os << tab << tab << tab << field->get_local_name() << "_(_other." << field->get_local_name() << "_)" << (mit == final_field ? "" : ",") << nl; } } os << tab << "{}" << nl; os << tab << LocalName() << "(" << LocalName() << "&& _other)" << nl; if (m_fields.size() > 0) { os << tab << ":" << nl; for (mit = m_fields.begin(); mit != m_fields.end(); mit++) { field = *mit; TList<be_field *>::iterator final_field = m_fields.end(); --final_field; os << tab << tab << tab << field->get_local_name() << "_(::std::move(_other." << field->get_local_name() << "_))" << (mit == final_field ? "" : ",") << nl; } } os << tab << "{}" << nl; os << tab << LocalName() << "& operator=(" << LocalName() << "&& _other)" << nl; os << tab << "{" << nl ; if (m_fields.size() > 0) { os << tab << tab << "if (this != &_other)" << nl; os << tab << tab << "{" << nl; for (mit = m_fields.begin(); mit != m_fields.end(); mit++) { field = *mit; TList<be_field *>::iterator final_field = m_fields.end(); --final_field; os << tab << tab << tab << field->get_local_name() << "_ = ::std::move(_other." << field->get_local_name() << "_);" << nl; } os << tab << tab << "}" << nl; } os << tab << tab << "return *this;" << nl; os << tab << "}" << nl; os << tab << LocalName() << "& operator=(const " << LocalName() << "& _other)" << nl; os << tab << "{" << nl ; if (m_fields.size() > 0) { os << tab << tab << "if (this != &_other)" << nl; os << tab << tab << "{" << nl; for (mit = m_fields.begin(); mit != m_fields.end(); mit++) { field = *mit; TList<be_field *>::iterator final_field = m_fields.end(); --final_field; os << tab << tab << tab << field->get_local_name() << "_ = _other." << field->get_local_name() << "_;" << nl; } os << tab << tab << "}" << nl; } os << tab << tab << "return *this;" << nl; os << tab << "}" << nl; source.Outdent(); os << "# else" << nl; source.Indent(); os << tab << LocalName() << "(const " << LocalName() << "& _other) = default;" << nl; os << tab << LocalName() << "(" << LocalName() << "&& _other) = default;" << nl; os << tab << LocalName() << "& operator=(" << LocalName() << "&& _other) = default;" << nl; os << tab << LocalName() << "& operator=(const " << LocalName() << "& _other) = default;" << nl; source.Outdent(); os << "# endif" << nl; os << "#endif" << nl; source.Indent(); for (mit = m_fields.begin(); mit != m_fields.end(); mit++) { field = *mit; bool is_sequency = !(field->get_be_type()->IsPrimitiveType() || field->get_be_type()->IsEnumeratedType()); relativeName = BE_Globals::RelativeScope (scopedName, field->StructMemberTypeName ()); // const get accessor os << tab << (is_sequency ? "const " : "") << relativeName << (is_sequency ? "& " : " ") << field->get_local_name() << "() const { return this->" << field->get_local_name() << "_; }" << nl; // reference get accessor os << tab << relativeName << "& " << field->get_local_name() << "() { return this->" << field->get_local_name() << "_; }" << nl; // const set accessor os << tab << "void " << field->get_local_name() << (is_sequency ? "(const " : "(") << relativeName << (is_sequency ? "&" : "") << " _val_) { this->" << field->get_local_name() << "_ = _val_; }" << nl; source.Outdent(); if (!(field->get_be_type()->IsPrimitiveType() && !field->get_be_type()->IsEnumeratedType())) { os << "#ifdef OSPL_DDS_CXX11" << nl; source.Indent(); // C++ 11 move assignement op os << tab << "void " << field->get_local_name() << "(" << relativeName << "&& _val_) { this->" << field->get_local_name() << "_ = _val_; }" << nl; source.Outdent(); os << "#endif" << nl; } source.Indent(); } } if (BE_Globals::gen_equality) { os << tab << "bool operator==(const " << LocalName() << "& _other) const" << nl << tab << "{" << nl << tab << tab << "return "; DDS_StdString relName; for(mit = m_fields.begin(); mit != m_fields.end(); mit++) { field = *mit; relName = BE_Globals::RelativeScope (scopedName, field->get_local_name()); TList<be_field *>::iterator final_field = m_fields.end(); --final_field; if(mit != m_fields.begin()) os << tab << tab; os << relName << (BE_Globals::isocpp_new_types ? "_" : "") << " == _other." << relName << (BE_Globals::isocpp_new_types ? "_" : "") << (mit != final_field ? " &&" : ";") << nl; } os << tab << "}" << nl; os << tab << "bool operator!=(const " << LocalName() << "& other) const" << nl << tab << "{" << nl << tab << tab << "return !(*this == other);" << nl << tab << "}" << nl; } // member declarations for (mit = m_fields.begin(); mit != m_fields.end(); mit++) { field = *mit; relativeName = BE_Globals::RelativeScope (scopedName, field->StructMemberTypeName ()); os << tab << relativeName << " " << field->get_local_name() << (BE_Globals::isocpp_new_types ? "_" : "") << ";" << nl; } source.Outdent(); source.Outdent(); source.Stream() << tab << "};" << nl << nl; g_cppScopeStack.Pop(); if (BE_Globals::isocpp_test_methods) { //Get base filename and append _testmethod.h DDS_StdString BaseFilename; BaseFilename = StripExtension(source.Filename()); BaseFilename += "_testmethod.h"; //Open or append to file be_Source testsource; ostream & ts = testsource.Stream(); if(!testsource.Open(BaseFilename)) cerr << "Cannot open: " << BaseFilename << endl; testsource.Indent(); ts << "namespace {" << nl << "template <>" << nl << "::std::vector< ::" << ScopedName() << " > generate_test_values< ::" << ScopedName() << " >()" << nl << "{" << nl; ts << tab << "::std::vector< ::" << ScopedName() << " > values;" << nl << tab << "::" << ScopedName() << " next;" << nl << tab << "::std::size_t biggest = 0;" << nl; for (mit = m_fields.begin(); mit != m_fields.end(); mit++) { field = *mit; relativeName = field->StructMemberTypeName (); ts << tab <<"::std::vector< " << field->StructMemberTypeName() << " > " << field->get_local_name() << "_ = generate_test_values< " << field->StructMemberTypeName() << " >();" << nl; ts << tab << "if(" << field->get_local_name() << "_.size() > biggest)" << nl << tab << tab << "biggest = " << field->get_local_name() << "_.size();" << nl; } ts << tab << "for(::std::size_t i = 0; i < biggest; ++i)" << nl << tab << "{" << nl; for (mit = m_fields.begin(); mit != m_fields.end(); mit++) { field = *mit; relativeName = BE_Globals::RelativeScope(scopedName, field->StructMemberTypeName ()); ts << tab << tab << "next." << field->get_local_name() << "_ = " << field->get_local_name() << "_[i < " << field->get_local_name() << "_.size()? i : " << field->get_local_name() << "_.size() -1];" << nl; } ts << tab << tab << "values.push_back(next);" << nl << tab << "}" << nl << tab << "return values;" << nl; ts << "} }" << nl << nl; testsource.Outdent(); testsource.Close(); } GenerateAuxTypes (source); be_root::GenerateDependants(source, SequenceMemberTypeName(), EnclosingScope()); } }