added test using own class as argument
[libt2n] / codegen / main.cpp
index a023eaf..f0c6ad4 100644 (file)
@@ -152,8 +152,14 @@ type_info get_type(const xmlpp::Element* root, const std::string &id)
 
      assert(element->get_attribute("name"));
      type_info ret;
-     if (element->get_attribute("context"))
-         ret.name=get_namespace(root, element->get_attribute("context")->get_value())+"::";
+     if (element->get_attribute("context")) {
+         ret.name=get_namespace(root, element->get_attribute("context")->get_value());
+         if (ret.name!="::")
+              ret.name+="::";
+         else
+              // do not explicitely add ::
+              ret.name="";
+     }
      ret.name+=element->get_attribute("name")->get_value();
      return ret;
 }
@@ -295,14 +301,13 @@ used_groups(const std::list<t2n_procedure> &funcs) {
 void output_common_hpp(std::ostream &o, const std::list<t2n_procedure> &procs) {
      std::set<std::string> groups(used_groups(procs));
 
-     o << "#include \"codegen-stubhead.hxx\"\n";
      for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
          o << "class cmd_group_" << *it << " : public libt2n::command\n"
            << "{\n"
            << "private:\n"
            << "        friend class boost::serialization::access;\n"
            << "        template<class Archive>\n"
-           << "        void serialize(Archive & ar, const unsigned int version)\n"
+           << "        void serialize(Archive & ar, const unsigned int /* version */)\n"
            << "        {ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);}\n"
            << "};\n";
      }
@@ -314,7 +319,7 @@ void output_common_hpp(std::ostream &o, const std::list<t2n_procedure> &procs) {
            << "        " << it->ret_type << " res;\n"
            << "        friend class boost::serialization::access;\n"
            << "        template<class Archive>\n"
-           << "        void serialize(Archive & ar, const unsigned int version)\n"
+           << "        void serialize(Archive & ar, const unsigned int /* version */)\n"
            << "        {\n"
            << "                ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);\n"
            << "                ar & BOOST_SERIALIZATION_NVP(res);\n"
@@ -334,7 +339,7 @@ void output_common_hpp(std::ostream &o, const std::list<t2n_procedure> &procs) {
          }
          o << "        friend class boost::serialization::access;\n"
            << "        template<class Archive>\n"
-           << "        void serialize(Archive & ar, const unsigned int version)\n"
+           << "        void serialize(Archive & ar, const unsigned int /* version */)\n"
            << "        {\n"
            << "                ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_" << it->group << ");\n";
          for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
@@ -476,17 +481,57 @@ struct cpp_file : public std::ofstream
      }
 };
 
+std::list<std::string>
+get_includes(const std::string &fname)
+{
+     // grep "#include" fname
+     std::ifstream in(fname.c_str());
+     std::string line;
+     std::list<std::string> ret;
+     while (std::getline(in,line)) {
+         if (line.find("#include")!=std::string::npos)
+              ret.push_back(line);
+     }
+     return ret;
+}
+
+void
+paste_includes(std::ostream &o, std::list<std::string> &i)
+{
+     o << std::endl
+       << "// copied includes begin" << std::endl;
+     for (std::list<std::string>::const_iterator it=i.begin(); it!=i.end(); ++it)
+         o << *it << std::endl;
+     o << "// copied includes end" << std::endl
+       << std::endl;
+}
+
+struct RemoveGenerated
+{
+     RemoveGenerated(const std::string &_prefix) : prefix(_prefix) {}
+     bool operator()(const std::string &s) const {
+         return (s.find(prefix+"common.hxx")!=std::string::npos);
+     }
+     const std::string &prefix;
+};
+
 int
 main(int argc, char* argv[])
 {
-     if (argc != 3) {
-         std::cerr << "Usage: " << argv[0] << " gccxml-file outputprefix" << std::endl;
+     if (argc != 4) {
+         std::cerr << "Usage: " << argv[0] << " header-file gccxml-file outputprefix" << std::endl;
          return 1;
      }
-     std::string filepath(argv[1]);
-     std::string prefix(argv[2]);
+     std::string headerfile(argv[1]);
+     std::string xmlfile(argv[2]);
+     std::string prefix(argv[3]);
+     std::list<std::string> includes(get_includes(headerfile));
+     remove_if(includes.begin(), includes.end(), RemoveGenerated(prefix));
+     includes.erase(remove_if(includes.begin(), includes.end(), RemoveGenerated(prefix)), includes.end());
+
+     paste_includes(std::cerr, includes);
   
-     Parser parser(filepath);
+     Parser parser(xmlfile);
      std::list<t2n_procedure> procedures(parser.get_procedures());
 
      std::cerr << "Procedures:" << std::endl;
@@ -505,12 +550,18 @@ main(int argc, char* argv[])
      std::string server_cpp_fname(prefix+"server.cpp");
 
      header_file common_hpp(common_hpp_fname.c_str());
+     common_hpp << "#include \"codegen-stubhead.hxx\"\n";
+     paste_includes(common_hpp, includes);
+
      output_common_hpp(common_hpp, procedures);
 
      cpp_file common_cpp(common_cpp_fname.c_str());
      output_common_cpp(common_cpp, procedures, common_hpp_fname);
 
      header_file client_hpp(client_hpp_fname.c_str());
+     client_hpp << "#include \"codegen-stubhead.hxx\"\n";
+     // we can't paste the includes before codegen-stubhead.hxx was included
+     paste_includes(client_hpp, includes);
      output_client_hpp(client_hpp, procedures);
 
      cpp_file client_cpp(client_cpp_fname.c_str());