summaryrefslogtreecommitdiff
path: root/cpp/rubygen/cppgen.rb
diff options
context:
space:
mode:
authorAlan Conway <aconway@apache.org>2007-07-31 20:00:10 +0000
committerAlan Conway <aconway@apache.org>2007-07-31 20:00:10 +0000
commitc11f9a79eec63da7aa6e6dac248a689a9d461beb (patch)
tree7c89f9804904dd3b99f3b3e564d14278ee3a7044 /cpp/rubygen/cppgen.rb
parent82d128b33fac55b8618d593c89283356ee4e192b (diff)
downloadqpid-python-c11f9a79eec63da7aa6e6dac248a689a9d461beb.tar.gz
Ruby code generator for C++.
Not yet in active use yet but two sample templates are provided. Note: same dependency story as java codegen: distribution has pre-generated code so ruby not required to build a distro. Only required for svn working copy builds. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@561468 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/rubygen/cppgen.rb')
-rwxr-xr-xcpp/rubygen/cppgen.rb126
1 files changed, 126 insertions, 0 deletions
diff --git a/cpp/rubygen/cppgen.rb b/cpp/rubygen/cppgen.rb
new file mode 100755
index 0000000000..3e3800c4cd
--- /dev/null
+++ b/cpp/rubygen/cppgen.rb
@@ -0,0 +1,126 @@
+#!/usr/bin/ruby
+#
+# General purpose C++ code generation.
+#
+require 'amqpgen'
+require 'set'
+
+Copyright=<<EOS
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+EOS
+
+CppKeywords = Set.new(["and", "and_eq", "asm", "auto", "bitand",
+ "bitor", "bool", "break", "case", "catch", "char",
+ "class", "compl", "const", "const_cast", "continue",
+ "default", "delete", "do", "DomainInfo", "double",
+ "dynamic_cast", "else", "enum", "explicit", "extern",
+ "false", "float", "for", "friend", "goto", "if",
+ "inline", "int", "long", "mutable", "namespace", "new",
+ "not", "not_eq", "operator", "or", "or_eq", "private",
+ "protected", "public", "register", "reinterpret_cast",
+ "return", "short", "signed", "sizeof", "static",
+ "static_cast", "struct", "switch", "template", "this",
+ "throw", "true", "try", "typedef", "typeid",
+ "typename", "union", "unsigned", "using", "virtual",
+ "void", "volatile", "wchar_t", "while", "xor",
+ "xor_eq"])
+# Names that need a trailing "_" to avoid clashes.
+CppMangle = CppKeywords+Set.new(["string"])
+
+class String
+ def cppsafe()
+ CppMangle.include?(self) ? self+"_" : self
+ end
+end
+
+# Additional methods for AmqpField.
+class AmqpField
+ def cppname() @cache_cppname ||= name.lcaps.cppsafe; end
+ def cpptype() @cache_cpptype ||= amqp_root.param_type(field_type); end
+ def type_name () @type_name ||= cpptype+" "+cppname; end
+end
+
+# Additional methods for AmqpMethod
+class AmqpMethod
+ def cppname() @cache_cppname ||= name.lcaps.cppsafe; end
+ def param_names() @cache_param_names ||= fields.collect { |f| f.cppname }; end
+ def signature() @cache_signature ||= fields.collect { |f| f.cpptype+" "+f.cppname }; end
+ def body_name() @cache_body_name ||= amqp_parent.name.caps+name.caps+"Body"; end
+end
+
+# Additional methos for AmqpRoot
+class AmqpRoot
+ # FIXME aconway 2007-06-20: fix u_int types, should be uint
+ CppTypeMap={
+ "bit"=> ["bool"],
+ "octet"=>["u_int8_t"],
+ "short"=>["u_int16_t"],
+ "long"=>["u_int32_t"],
+ "longlong"=>["u_int64_t"],
+ "timestamp"=>["u_int64_t"],
+ "longstr"=>["string", "const string&"],
+ "shortstr"=>["string", "const string&"],
+ "table"=>["FieldTable", "const FieldTable&", "const FieldTable&"],
+ "content"=>["Content", "const Content&", "const Content&"]
+ }
+
+ def lookup(amqptype)
+ CppTypeMap[amqptype] or raise "No cpp type for #{amqptype}";
+ end
+
+ def member_type(amqptype) lookup(amqptype)[0]; end
+ def param_type(amqptype) t=lookup(amqptype); t[1] or t[0]; end
+ def return_type(amqptype) t=lookup(amqptype); t[2] or t[0]; end
+end
+
+# Additional methods for AmqpClass
+class AmqpClass
+ def cppname() @cache_cppname ||= name.caps; end
+end
+
+class CppGen < Generator
+ def initialize(outdir, *specs)
+ super(outdir,*specs)
+ end
+
+ # Write a header file.
+ def h_file(path)
+ guard=path.upcase.tr('./-','_')
+ file(path) {
+ gen "#ifndef #{guard}\n"
+ gen "#define #{guard}\n"
+ gen Copyright
+ yield
+ gen "#endif /*!#{guard}*/\n"
+ }
+ end
+
+ # Write a .cpp file.
+ def cpp_file(path)
+ file (path) do
+ gen Copyright
+ yield
+ end
+ end
+end
+