summaryrefslogtreecommitdiff
path: root/cpp/rubygen
diff options
context:
space:
mode:
authorAlan Conway <aconway@apache.org>2008-02-27 21:49:04 +0000
committerAlan Conway <aconway@apache.org>2008-02-27 21:49:04 +0000
commit6bf200b4a953e3e7ae521c1e269e420af7e52cba (patch)
treef9cbe87cb8c85f6fc902d1d2874139257c4ef6f9 /cpp/rubygen
parent12ea725d2b2cd95e9a8ef5f4b4511e841054b628 (diff)
downloadqpid-python-6bf200b4a953e3e7ae521c1e269e420af7e52cba.tar.gz
Generating domains, structs, commands and controls for 0-10 final spec.
Not yet generating: holders, visitors. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@631740 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/rubygen')
-rwxr-xr-xcpp/rubygen/0-10/amqp_0_10.rb175
-rwxr-xr-xcpp/rubygen/0-10/specification.rb177
-rwxr-xr-xcpp/rubygen/amqpgen.rb48
-rwxr-xr-xcpp/rubygen/cppgen.rb30
-rwxr-xr-xcpp/rubygen/generate2
5 files changed, 236 insertions, 196 deletions
diff --git a/cpp/rubygen/0-10/amqp_0_10.rb b/cpp/rubygen/0-10/amqp_0_10.rb
deleted file mode 100755
index 985d275668..0000000000
--- a/cpp/rubygen/0-10/amqp_0_10.rb
+++ /dev/null
@@ -1,175 +0,0 @@
-#!/usr/bin/env ruby
-$: << ".." # Include .. in load path
-require 'cppgen'
-
-class Amqp_0_10 < CppGen
- ArrayTypes={
- "str16-array" => "Str16",
- "amqp-host-array" => "connection::AmqpHostUrl",
- "command-fragments" => "session::CommandFragment",
- "in-doubt" => "dtx::Xid"
- }
-
- def initialize(outdir, amqp)
- super(outdir, amqp)
- @ns="qpid::amqp_0_10"
- @dir="qpid/amqp_0_10"
- end
-
- # domains
-
- def domain_h(d)
- typename=d.name.typename
- if d.enum
- scope("enum #{typename} {", "};") {
- genl d.enum.choices.map { |c| "#{c.name.constname} = #{c.value}" }.join(",\n")
- }
- elsif d.type_ == "array"
- @array_domains << "typedef Array<#{ArrayTypes[d.name]}> #{typename};\n"
- else
- genl "typedef #{d.type_.amqp2cpp} #{typename};"
- end
- end
-
- # structs
-
- # field members and MemberInfo
- def member_h(m)
- genl "static const MemberInfo INFO;"
- m.fields.each { |f| genl "#{f.type_.amqp2cpp} #{f.cppname};" }
- end
-
- # MemberInfo constant definition.
- def member_cpp(m)
- infotype=m.is_a?(AmqpStruct) ? "Struct1Info" : "MemberInfo"
- scope("{infotype} #{m.classname}::INFO = {","};") {
- inits=[]
- inits << (m.parent.is_a?(AmqpClass) ? "&CLASS_INFO" : "0")
- inits << m.name << (m.code or "0");
- if m.is_a?(AmqpStruct)
- inits << (m.size or 0) << (m.pack or 0)
- end
- genl inits.join(", ")
- }
- end
-
- def struct_h(s) struct(s.classname, "public Struct") { member_h s }; end
- def struct_cpp(s) member_cpp s; end
- def gen_structs()
- file="#{@dir}/structs"
- h_file(file) {
- include "#{@dir}/built_in_types.h"
- include "#{@dir}/helpers.h"
- include "<boost/call_traits.hpp>"
- genl "using boost::call_traits;"
- namespace(@ns) {
- @amqp.structs.each { |s| struct_h s }
- each_class_ns { |c| c.structs.each { |s| struct_h s }}
- }
- }
- cpp_file(file) {
- include file
- namespace(@ns) {
- @amqp.structs.each { |s| struct_h s }
- each_class_ns { |c| c.structs.each { |s| struct_cpp s }}
- }
- }
- end
-
- # command and control
-
- def action_h(a)
- name=a.name.typename
- struct(name, "public #{a.base}") {
- genl "#{name}() {}"
- scope("#{name}(",");") { genl a.parameters } unless a.fields.empty?
- scope("template <class T> void invoke(T& target) {","}") {
- scope("target.#{a.funcname}(", ");") { genl a.values }
- }
- scope("template <class S> void serialize(S& s) {","}") {
- gen "s"
- a.fields.each { |f| gen "(#{f.cppname})"}
- genl ";"
- } unless a.fields.empty?
- member_h a
- }
- end
-
- def action_cpp(a) # command or control
- # ctor
- scope("#{a.classname}::#{a.classname}(",") :") { genl a.parameters }
- indent() { genl a.initializers }
- genl "{}"
- # member constants
- member_cpp a
- end
-
- def class_h(c)
- genl "extern const ClassInfo CLASS_INFO;"
- @array_domains=""
- c.domains.each { |d| domain_h d }
- c.structs.each { |s| struct_h s }
- gen @array_domains
- end
-
- def class_cpp(c)
- genl "const ClassInfo CLASS_INFO = { #{c.code}, \"#{c.name}\" };"
- c.structs.each { |s| struct_cpp s }
- end
-
- def gen_specification()
- h_file("#{@dir}/specification") {
- include "#{@dir}/built_in_types"
- include "#{@dir}/helpers"
- include "<boost/call_traits.hpp>"
- genl "using boost::call_traits;"
- namespace(@ns) {
- # We don't generate top-level domains, as they have clashing values.
- each_class_ns { |c| class_h c }
- each_class_ns { |c| c.actions.each { |a| action_h a}
- }
- }
- }
- cpp_file("#{@dir}/specification") {
- include "#{@dir}/specification"
- namespace(@ns) {
- each_class_ns { |c| class_cpp c }
- each_class_ns { |c| c.actions.each { |a| action_cpp a}
- }
- }
- }
- end
-
- def gen_proxy()
- h_file("#{@dir}/Proxy.h") {
- include "#{@dir}/specification"
- namespace(@ns) {
- genl "template <class F, class R=F::result_type>"
- cpp_class("ProxyTemplate") {
- public
- genl "ProxyTemplate(F f) : functor(f) {}"
- @amqp.classes.each { |c|
- c.actions.each { |a|
- scope("R #{a.funcname}(", ")") { genl a.parameters }
- scope() {
- var=a.name.funcname
- scope("#{a.classname} #{var}(",");") { genl a.arguments }
- genl "return functor(#{var});"
- }
- }
- }
- private
- genl "F functor;"
- }
- }
- }
- end
-
- def generate
- gen_specification
- gen_proxy
- end
-end
-
-Amqp_0_10.new($outdir, $amqp).generate();
-
diff --git a/cpp/rubygen/0-10/specification.rb b/cpp/rubygen/0-10/specification.rb
new file mode 100755
index 0000000000..026b49e1a9
--- /dev/null
+++ b/cpp/rubygen/0-10/specification.rb
@@ -0,0 +1,177 @@
+#!/usr/bin/env ruby
+$: << ".." # Include .. in load path
+require 'cppgen'
+
+class Specification < CppGen
+ def initialize(outdir, amqp)
+ super(outdir, amqp)
+ @ns="qpid::amqp_#{@amqp.version.bars}"
+ @dir="qpid/amqp_#{@amqp.version.bars}"
+ end
+
+ # domains
+
+ def domain_h(d)
+ genl
+ typename=d.name.typename
+ if d.enum
+ scope("enum #{typename} {", "};") {
+ genl d.enum.choices.map { |c|
+ "#{c.name.constname} = #{c.value}" }.join(",\n")
+ }
+ elsif (d.type_ == "array")
+ genl "typedef Array<#{ArrayTypes[d.name].amqp2cpp}> #{typename};"
+ else
+ genl "typedef #{d.type_.amqp2cpp} #{typename};"
+ end
+ end
+
+ # class constants
+
+ def class_h(c)
+ genl "const uint8_t CODE=#{c.code};"
+ genl "extern const char* NAME;"
+ end
+
+ def class_cpp(c)
+ genl "const char* NAME=\"#{c.fqname}\";"
+ end
+
+ # Used by structs, commands and controls.
+ def action_struct_h(x, base, consts, &block)
+ genl
+ struct(x.classname, "public #{base}") {
+ x.fields.each { |f| genl "#{f.type_.amqp2cpp} #{f.cppname};" }
+ genl
+ genl "static const char* NAME;"
+ consts.each { |c| genl "static const uint8_t #{c.upcase}=#{x.send c or 0};"}
+ genl "static const uint8_t CLASS_CODE=#{x.containing_class.nsname}::CODE;"
+ genl
+ genl "#{x.classname}();"
+ scope("#{x.classname}(",");") { genl x.parameters } unless x.fields.empty?
+ genl
+ genl "void accept(Visitor&) const;"
+ genl
+ yield if block
+ }
+ end
+
+ def action_struct_cpp(x)
+ genl
+ genl "const char* #{x.classname}::NAME=\"#{x.fqname}\";"
+ genl
+ genl "#{x.classname}::#{x.classname}() {}";
+ genl
+ if not x.fields.empty?
+ scope("#{x.classname}::#{x.classname}(",") :") { genl x.parameters }
+ indent() { genl x.initializers }
+ genl "{}"
+ genl
+ end
+ scope("void #{x.classname}::accept(Visitor&) const {","}") {
+ genl "// FIXME aconway 2008-02-27: todo"
+ }
+ end
+
+ # structs
+
+ def struct_h(s) action_struct_h(s, "Struct", ["size","pack","code"]); end
+ def struct_cpp(s) action_struct_cpp(s) end
+
+ # command and control
+
+ def action_h(a)
+ action_struct_h(a, a.base, ["code"]) {
+ scope("template <class T> void invoke(T& target) {","}") {
+ scope("target.#{a.funcname}(", ");") { genl a.values }
+ }
+ genl
+ scope("template <class S> void serialize(S& s) {","}") {
+ gen "s"
+ a.fields.each { |f| gen "(#{f.cppname})"}
+ genl ";"
+ } unless a.fields.empty?
+ }
+ end
+
+ def action_cpp(a) action_struct_cpp(a); end
+
+ # Types that must be generated early because they are used by other types.
+ def pregenerate?(x) not @amqp.used_by[x.fqname].empty?; end
+
+ # Generate the log
+ def gen_specification()
+ h_file("#{@dir}/specification") {
+ include "#{@dir}/built_in_types"
+ include "#{@dir}/helpers"
+ include "<boost/call_traits.hpp>"
+ genl "using boost::call_traits;"
+ namespace(@ns) {
+ # Top level
+ @amqp.domains.each { |d|
+ # segment-type and track are are built in
+ domain_h d unless ["track","segment-type"].include?(d.name)
+ }
+ puts @amqp.used_by.inspect
+
+ # Domains and structs that must be generated early because
+ # they are used by other definitions:
+ each_class_ns { |c|
+ class_h c
+ c.domains.each { |d| domain_h d if pregenerate? d }
+ c.structs.each { |s| struct_h s if pregenerate? s }
+ }
+ # Now dependent domains/structs and actions
+ each_class_ns { |c|
+ c.domains.each { |d| domain_h d if not pregenerate? d }
+ c.structs.each { |s| struct_h s if not pregenerate? s }
+ c.actions.each { |a| action_h a }
+ }
+ }
+ }
+
+ cpp_file("#{@dir}/specification") {
+ include "#{@dir}/specification"
+ namespace(@ns) {
+ each_class_ns { |c|
+ class_cpp c
+ c.actions.each { |a| action_cpp a}
+ c.structs.each { |s| struct_cpp s }
+ }
+ }
+ }
+ end
+
+ def gen_proxy()
+ h_file("#{@dir}/Proxy.h") {
+ include "#{@dir}/specification"
+ namespace(@ns) {
+ genl "template <class F, class R=F::result_type>"
+ cpp_class("ProxyTemplate") {
+ public
+ genl "ProxyTemplate(F f) : functor(f) {}"
+ @amqp.classes.each { |c|
+ c.actions.each { |a|
+ scope("R #{a.funcname}(", ")") { genl a.parameters }
+ scope() {
+ var=a.name.funcname
+ scope("#{a.classname} #{var}(",");") { genl a.arguments }
+ genl "return functor(#{var});"
+ }
+ }
+ }
+ private
+ genl "F functor;"
+ }
+ }
+ }
+ end
+
+ def generate
+ gen_specification
+ gen_proxy
+ end
+end
+
+Specification.new($outdir, $amqp).generate();
+
diff --git a/cpp/rubygen/amqpgen.rb b/cpp/rubygen/amqpgen.rb
index fbbdc8dd2e..b1e635a27b 100755
--- a/cpp/rubygen/amqpgen.rb
+++ b/cpp/rubygen/amqpgen.rb
@@ -152,10 +152,21 @@ class AmqpElement
# Text of doc child if there is one.
def doc() d=xml.elements["doc"]; d and d.text; end
+ def fqname()
+ throw "fqname: #{self} #{parent.fqname} has no name" unless name
+ p=parent && parent.fqname
+ p ? p+"."+name : name;
+ end
+
+ def containing_class()
+ return self if is_a? AmqpClass
+ return parent && parent.containing_class
+ end
+
end
class AmqpResponse < AmqpElement
- def initialize(xml, parent) super; end
+ def initialize(xml, parent) super; end
end
class AmqpDoc < AmqpElement
@@ -167,18 +178,32 @@ class AmqpChoice < AmqpElement
def initialize(xml,parent) super; end
amqp_attr_reader :name, :value
end
-
+
class AmqpEnum < AmqpElement
def initialize(xml,parent) super; end
amqp_child_reader :choice
end
+# 0-10 array domains are missing element type information, add it here.
+ArrayTypes={
+ "str16-array" => "str-16",
+ "amqp-host-array" => "connection.amqp-host-url",
+ "command-fragments" => "session.command-fragment",
+ "in-doubt" => "dtx.xid"
+}
+
class AmqpDomain < AmqpElement
- def initialize(xml, parent) super; end
+ def initialize(xml, parent)
+ super
+ root.used_by[uses].push(fqname) if uses and uses.index('.')
+ end
+
amqp_attr_reader :type
amqp_single_child_reader :struct # preview
amqp_single_child_reader :enum
+ def uses() type_=="array" ? ArrayTypes[name] : type_; end
+
def unalias()
d=self
while (d.type_ != d.name and root.domain(d.type_))
@@ -194,7 +219,11 @@ class AmqpException < AmqpElement
end
class AmqpField < AmqpElement
- def initialize(xml, amqp) super; end;
+ def initialize(xml, amqp)
+ super;
+ root.used_by[type_].push(parent.fqname) if type_ and type_.index('.')
+ end
+
def domain() root.domain(xml.attributes["domain"]); end
amqp_single_child_reader :struct # preview
amqp_child_reader :exception
@@ -215,6 +244,7 @@ class AmqpResult < AmqpElement
def initialize(xml, parent) super; end
amqp_single_child_reader :struct # preview
amqp_attr_reader :type
+ def name() "result"; end
end
class AmqpEntry < AmqpElement
@@ -333,16 +363,16 @@ class AmqpRoot < AmqpElement
raise "No XML spec files." if specs.empty?
xml=parse(specs.shift)
specs.each { |s| xml_merge(xml, parse(s)) }
+ @used_by=Hash.new{ |h,k| h[k]=[] }
super(xml, nil)
end
+ attr_reader :used_by
+
def merge(root) xml_merge(xml, root.xml); end
def version() major + "-" + minor; end
- # Find the element corresponding to an amqp dotted name.
- def lookup(dotted_name) xml.elements[dotted_name.gsub(/\./,"/")]; end
-
# preview - only struct child reader remains for new mapping
def domain_structs() domains.map{ |d| d.struct }.compact; end
def result_structs()
@@ -358,6 +388,8 @@ class AmqpRoot < AmqpElement
@methods_on[chassis] ||= classes.map { |c| c.methods_on(chassis) }.flatten
end
+ def fqname() nil; end
+
# TODO aconway 2008-02-21: methods by role.
private
@@ -390,7 +422,7 @@ class Generator
@prefix=[''] # For indentation or comments.
@indentstr=' ' # One indent level.
@outdent=2
- Pathname.new(@outdir).mkpath unless @outdir=="-" or File.directory?(@outdir)
+ Pathname.new(@outdir).mkpath unless @outdir=="-"
end
# Create a new file, set @out.
diff --git a/cpp/rubygen/cppgen.rb b/cpp/rubygen/cppgen.rb
index e3462d60d7..edee72e0bd 100755
--- a/cpp/rubygen/cppgen.rb
+++ b/cpp/rubygen/cppgen.rb
@@ -61,7 +61,7 @@ class String
name=path.pop
return name.typename if path.empty?
path.map! { |n| n.nsname }
- return "amqp_0_10::"+(path << name.caps).join("::")
+ return (path << name.caps).join("::")
end
alias :typename :caps
@@ -137,12 +137,8 @@ class AmqpMethod
def body_name() parent.name.caps+name.caps+"Body"; end
end
-class AmqpAction
- def nsname() name.namespace; end
- def classname() name.typename; end
- def funcname() parent.name.funcname + name.caps; end
-
- def parameters()
+module AmqpHasFields
+ def parameters()
fields.map { |f| "#{f.paramtype} #{f.cppname}_"}.join(",\n")
end
@@ -155,21 +151,26 @@ class AmqpAction
end
def initializers()
- fields.map { |f| "#{f.cppname}(#{f.cppname}_)}"}.join(",\n")
+ fields.map { |f| "#{f.cppname}(#{f.cppname}_)"}.join(",\n")
end
+end
+class AmqpAction
+ def classname() name.typename; end
+ def funcname() parent.name.funcname + name.caps; end
+ include AmqpHasFields
end
-class AmqpCommand
+class AmqpCommand < AmqpAction
def base() "Command"; end
end
-class AmqpControl
+class AmqpControl < AmqpAction
def base() "Control"; end
end
class AmqpClass
- def cppname() name.caps; end
+ def cppname() name.caps; end # preview
def nsname() name.nsname; end
end
@@ -212,9 +213,14 @@ class AmqpResult
end
class AmqpStruct
- def cpp_pack_type() AmqpDomain.lookup_type(pack()) or CppType.new("uint16_t"); end
+ include AmqpHasFields
+
+ def cpp_pack_type() # preview
+ AmqpDomain.lookup_type(pack()) or CppType.new("uint16_t");
+ end
def cpptype() parent.cpptype; end # preview
def cppname() cpptype.name; end # preview
+
def classname() name.typename; end
end
diff --git a/cpp/rubygen/generate b/cpp/rubygen/generate
index 4ea8aef425..8a226031e4 100755
--- a/cpp/rubygen/generate
+++ b/cpp/rubygen/generate
@@ -74,7 +74,7 @@ rgen_generator=#{make_continue rgen_generator}
rgen_client_cpp=#{make_continue(rgen_srcs.grep(%r|/qpid/client/.+\.cpp$|))}
-rgen_common_cpp=#{make_continue(rgen_srcs.grep(%r|qpid/framing/.+\.cpp$|))}
+rgen_common_cpp=#{make_continue(rgen_srcs.grep(%r{qpid/(framing|amqp_.+)/.+\.cpp$}))}
rgen_srcs=#{make_continue rgen_srcs}