Exemple #1
0
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 << "struct " << DLLMACRO << LocalName () << nl;
      os << tab << "{" << nl;
      source.Indent ();

      // declare nested types

      be_CodeGenerator::Generate(source);

      // 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() << ";" << nl;
      }

      source.Outdent();

      source.Stream() << tab << "};" << nl << nl;

      g_cppScopeStack.Pop();

      GenerateAuxTypes (source);

      be_root::GenerateDependants(source, SequenceMemberTypeName(),
                                  EnclosingScope());
   }
}
void be_exception::Generate (be_ClientHeader & source)
{
   if (!Generated ())
   {
      ostream & os = source.Stream ();
      be_Tab tab (source);
      DDS_StdString lname = LocalName ();

      Generated (pbtrue);

      be_root::AddGlobalDeclarations (this);
      be_root::AddAnyOps (*this);
      be_root::AddStreamOps (*this);
      be_root::AddTypedef (*this);
      be_root::AddTypecode (*this);

      GenerateOpenClassDefinition (source);
      GenerateClassDeclarations (source);
      SetAccess (source, CA_PUBLIC);

      // now define nested types

      be_CodeGenerator::Generate (source);

      // generate _downcast

      os << tab << "static " << lname << "* _downcast ("
         << "DDS::Exception *);" << nl;
      os << tab << "static const " << lname << "* _downcast ("
         << "const DDS::Exception *);" << nl;

      // generate factory and builder

      os << tab << "static DDS::Exception * factory ();" << nl;
      os << tab << "static DDS::ExceptionInitializer m_initializer;" << nl << nl;

      // generate inline default constructor

      os << tab << lname << " () {};" << nl;

      // generate convenience constructor

      if (nmembers())
      {
         GenerateConvenienceConstructor (source);
      }

      // generate copy constructor

      os << tab << lname << " (const " << lname << " &);" << nl;

      // generate assignment operator

      os << tab << lname << "& operator = " << "(const " << lname << " &);" << nl;

      // generate duplicate

      os << tab << "virtual DDS::Exception * _clone () const;" << nl;

      // generate raise

      os << tab << "virtual void _raise (" << XBE_Ev::arg (XBE_ENV_ARG1)
         << ") const;" << nl;

      // generate name

      os << tab << "virtual const char * _name () const { return m_name; };" << nl;

      // generate repository id

      os << tab << "virtual const char * _rep_id () const { return m_id; };" << nl;

      // generate virtual destructor

      os << tab << "virtual ~" << lname << " () {}" << nl;

      GenerateMembers (source);
      SetAccess (source, CA_PRIVATE);

      os << tab << "static const char * m_name;" << nl;
      os << tab << "static const char * m_id;" << nl;

      GenerateCloseClassDefinition (source);

      be_root::GenerateDependants
      (
         source,
         SequenceMemberTypeName (),
         EnclosingScope ()
      );
   }
}
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());
   }
}