Switch time() calls to monotonic clock calls (#7597)
[libt2n] / codegen / main.cpp
index ac4b9e9..7271b22 100644 (file)
@@ -1,20 +1,24 @@
 /*
-    Copyright (C) 2006-2008
+    Copyright (C) 2006-2008 by Intra2net AG
     intra2net.com
 
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
+    The software in this package is distributed under the GNU General
+    Public License version 2 (with a special exception described below).
 
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
+    A copy of GNU General Public License (GPL) is included in this distribution,
+    in the file COPYING.GPL.
 
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+    As a special exception, if other files instantiate templates or use macros
+    or inline functions from this file, or you compile this file and link it
+    with other works to produce a work based on this file, this file
+    does not by itself cause the resulting work to be covered
+    by the GNU General Public License.
+
+    However the source code for this file must still be made available
+    in accordance with section (3) of the GNU General Public License.
+
+    This exception does not invalidate any other reasons why a work based
+    on this file might be covered by the GNU General Public License.
 */
 
 #include <libxml++/libxml++.h>
 #include <fstream>
 #include <list>
 #include <stdexcept>
+#include <string>
 #include <boost/lexical_cast.hpp>
-#ifdef HAVE_CONFIG_H
+#include <boost/serialization/extended_type_info.hpp>
+
 #include "config.h"
-#endif
+
 
 
 //! map group to class name
@@ -137,6 +143,15 @@ struct parse_error : public std::runtime_error
     {}
 };
 
+struct error_name_too_long : public std::runtime_error
+{
+    error_name_too_long(const std::string &name)
+            : std::runtime_error("symbol name '" + name + "' too long for serialization ("
+                                 + boost::lexical_cast<std::string>(name.length()) + ">"
+                                 + boost::lexical_cast<std::string>(BOOST_SERIALIZATION_MAX_KEY_SIZE-1) + ")")
+    {}
+};
+
 //! get type by id
 /*!
   \return type name or empty string on error
@@ -201,9 +216,16 @@ type_info get_type(const xmlpp::Element* root, const std::string &id)
     return ret;
 }
 
+struct Arg
+{
+    std::string name;
+    type_info type;
+    std::string defaultArg;
+};
+
 struct t2n_procedure
 {
-    typedef std::list<std::pair<std::string, type_info> > Args;
+    typedef std::list<Arg> Args;
 
     type_info ret_type;
     std::string name;
@@ -212,11 +234,17 @@ struct t2n_procedure
 
     std::string ret_classname() const
     {
-        return name+mangled+"_res";
+        std::string result = name+mangled+"_res";
+        if (result.length() >= BOOST_SERIALIZATION_MAX_KEY_SIZE)
+            throw error_name_too_long(result);
+        return result;
     }
     std::string cmd_classname() const
     {
-        return name+mangled+"_cmd";
+        std::string result = name+mangled+"_cmd";
+        if (result.length() >= BOOST_SERIALIZATION_MAX_KEY_SIZE)
+            throw error_name_too_long(result);
+        return result;
     }
     bool hasReturn() const {return !ret_type.isVoid();}
 };
@@ -226,7 +254,10 @@ std::ostream &operator<<(std::ostream &o, const t2n_procedure::Args &args)
     for (t2n_procedure::Args::const_iterator it=args.begin();it!=args.end();++it)
     {
         if (it!=args.begin()) o << ", ";
-        o << it->second << " " << it->first;
+        o << it->type << " " << it->name;
+
+        if (!it->defaultArg.empty())
+            o << "=" << it->defaultArg;
     }
     return o;
 }
@@ -303,20 +334,45 @@ protected:
             const xmlpp::Element* arg = dynamic_cast<const xmlpp::Element*>(*iter);
             if ( arg )
             {
+                struct Arg a;
+
                 assert(arg->get_name() == "Argument");
+
                 assert(arg->get_attribute("name"));
+                a.name=arg->get_attribute("name")->get_value();
+
                 assert(arg->get_attribute("type"));
-                f.args.push_back(std::pair<std::string, type_info>(arg->get_attribute("name")->get_value(), get_type(root, arg->get_attribute("type")->get_value())));
+                a.type=get_type(root, arg->get_attribute("type")->get_value());
+
+                // this would be the nice way of solving the problem of default arguments
+                // but currently the output of gccxml is unreliable (e.g. namespace only
+                // sometimes available)
+                // if(arg->get_attribute("default"))
+                //    a.defaultArg=arg->get_attribute("default")->get_value();
+
+                // so we need to get the def. arg. via attribute
+                // which is previously set by the macro LIBT2N_DEFAULT_ARG(type,value)
+                if(arg->get_attribute("attributes"))
+                {
+                    std::string attr=arg->get_attribute("attributes")->get_value();
+                    const std::string look_for = "gccxml(libt2n-default-arg,";
+
+                    if (attr.compare(0,look_for.size(),look_for) == 0)
+                        a.defaultArg=attr.substr(look_for.size(),attr.size()-look_for.size()-1);
+                }
+
                 // todo: ugly - could be any other error
-                if (f.args.back().second.name.empty())
+                if (a.type.name.empty())
                 {
                     assert(element->get_attribute("file"));
                     assert(element->get_attribute("line"));
                     std::pair<std::string, unsigned> file_and_line(get_file_and_line(root, element));
                     throw parse_error(file_and_line.first,
                                       file_and_line.second,
-                                      std::string("type of parameter `")+f.args.back().first+"' not (yet?) supported");
+                                      std::string("type of parameter '")+a.name+"' not (yet?) supported");
                 }
+
+                f.args.push_back(a);
             }
         }
         std::cerr << get_file_and_line_as_string(root, element) << ":\texport procedure: " << f << std::endl;
@@ -394,7 +450,7 @@ void output_common_hpp(std::ostream &o, const std::string &group, const std::lis
         << "private:\n";
         for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait)
         {
-            o << " " << ait->second.noref() << " " << ait->first << ";\n";
+            o << " " << ait->type.noref() << " " << ait->name << ";\n";
         }
         o << " friend class boost::serialization::access;\n"
         << " template<class Archive>\n"
@@ -403,7 +459,7 @@ void output_common_hpp(std::ostream &o, const std::string &group, const std::lis
         << "  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(" << groupClass(group) << ");\n";
         for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait)
         {
-            o << "  ar & BOOST_SERIALIZATION_NVP(" << ait->first << ");\n";
+            o << "  ar & BOOST_SERIALIZATION_NVP(" << ait->name << ");\n";
         }
 
         // default constructor
@@ -419,14 +475,14 @@ void output_common_hpp(std::ostream &o, const std::string &group, const std::lis
             for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait)
             {
                 if (ait!=it->args.begin()) o << ", ";
-                o << ait->second << " _" << ait->first;
+                o << ait->type << " _" << ait->name;
             }
             o << ") : ";
             for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait)
             {
                 if (ait!=it->args.begin()) o << ", ";
                 // pointers are const pointers and must be dereferenced
-                o << ait->first << "(" << ((ait->second.name.find_first_of('*')!=std::string::npos) ? "*" : "" ) << "_" << ait->first << ")";
+                o << ait->name << "(" << ((ait->type.name.find_first_of('*')!=std::string::npos) ? "*" : "" ) << "_" << ait->name << ")";
             }
             o << " {}\n";
         }
@@ -456,7 +512,7 @@ void output_client_hpp(std::ostream &o, const std::string &group, const std::lis
     o << "class " << groupClass(group) << "_client : public libt2n::command_client\n"
     << "{\n"
     << "public:\n"
-    << groupClass(group) << "_client(libt2n::client_connection &_c,\n"
+    << groupClass(group) << "_client(libt2n::client_connection *_c,\n"
     << " long long _command_timeout_usec=command_timeout_usec_default,\n"
     << " long long _hello_timeout_usec=hello_timeout_usec_default)\n"
     << " : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec)\n"
@@ -480,14 +536,24 @@ void output_client_cpp(std::ostream &o, const std::string &group, const std::lis
 
     for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit)
     {
-        o << pit->ret_type << " " << groupClass(group) << "_client::" << pit->name << "(" << pit->args << ")\n"
+        o << pit->ret_type << " " << groupClass(group) << "_client::" << pit->name << "(";
+
+        // we need to do this by hand here because we don't want default arguments within the cpp
+        for (t2n_procedure::Args::const_iterator xit=pit->args.begin();xit!=pit->args.end();++xit)
+        {
+            if (xit!=pit->args.begin())
+                o << ", ";
+            o << xit->type << " " << xit->name;
+        }
+
+        o << ")\n"
         << "{\n"
         << " libt2n::result_container rc;\n"
         << " send_command(new " << pit->cmd_classname() << "(";
         for (t2n_procedure::Args::const_iterator ait=pit->args.begin();ait!=pit->args.end();++ait)
         {
             if (ait!=pit->args.begin()) o << ", ";
-            o << ait->first;
+            o << ait->name;
         }
         o << "), rc);\n"
         << " " << pit->ret_classname() << "* res=dynamic_cast<" << pit->ret_classname() << "*>(rc.get_result());\n"
@@ -530,9 +596,9 @@ void output_server_cpp(std::ostream &o, const std::string &group, const std::lis
         {
             if (ait!=it->args.begin()) o << ", ";
             // get pointer
-            if (ait->second.name.find_first_of('*')!=std::string::npos)
+            if (ait->type.name.find_first_of('*')!=std::string::npos)
                 o << '&';
-            o << ait->first;
+            o << ait->name;
         }
 
         if (it->hasReturn())