summaryrefslogtreecommitdiff
path: root/qpid/cpp/rubygen/templates/Session.rb
blob: c61a21768258d01eb83953c2fe1a74c7b87c1572 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env ruby
# Usage: output_directory xml_spec_file [xml_spec_file...]
# 
$: << '..'
require 'cppgen'

class CppGen
  def session_methods
    excludes = ["channel", "connection", "session", "execution"]
    gen_methods=@amqp.methods_on(@chassis).reject { |m|
      excludes.include? m.parent.name
    }
  end
end

class ContentField               # For extra content parameters
  def cppname() "content"  end
  def signature() "const MethodContent& content" end
  def unpack() "p[arg::content|DefaultContent(std::string())]"; end
end

class AmqpField
  def unpack() "p[arg::#{cppname}|#{cpptype.default_value}]"; end
end

class AmqpMethod
  def fields_c() content ? fields+[ContentField.new] : fields end
  def param_names_c() fields_c.map { |f| f.cppname} end
  def signature_c()  fields_c.map { |f| f.signature }; end
  def argpack_name() "#{parent.cppname}#{name.caps}Parameters"; end
  def argpack_type()
    "boost::parameter::parameters<" +
      fields_c.map { |f| "arg::keyword_tags::"+f.cppname }.join(',') +
      ">"
  end
  def return_type()
    return "TypedResult<qpid::framing::#{result.cpptype.ret}>" if (result)
    return "Response" if (not responses().empty?)
    return "Completion"
  end
  def session_function() "#{parent.name.lcaps}#{name.caps}"; end
end

class SessionNoKeywordGen < CppGen

  def initialize(outdir, amqp)
    super(outdir, amqp)
    @chassis="server"
    @namespace,@classname,@file=
      parse_classname "qpid::client::no_keyword::Session_#{@amqp.version.bars}"
  end

  def generate()
    h_file(@file) {
      include "qpid/framing/amqp_framing.h"
      include "qpid/framing/Uuid.h"
      include "qpid/framing/amqp_structs.h"
      include "qpid/framing/ProtocolVersion.h"
      include "qpid/framing/MethodContent.h"
      include "qpid/framing/TransferContent.h"
      include "qpid/client/Completion.h"
      include "qpid/client/ConnectionImpl.h"
      include "qpid/client/Response.h"
      include "qpid/client/SessionCore.h"
      include "qpid/client/TypedResult.h"
      include "qpid/shared_ptr.h"
      include "<string>"
      namespace("qpid::client") { 
        genl "using std::string;"
        genl "using framing::Content;"
        genl "using framing::FieldTable;"
        genl "using framing::MethodContent;"
        genl "using framing::SequenceNumberSet;"
        genl "using framing::Uuid;"
        genl
        namespace("no_keyword") { 
          cpp_class(@classname) {
            public
            gen <<EOS
#{@classname}();
framing::FrameSet::shared_ptr get() { return impl->get(); }
Uuid getId() const { return impl->getId(); }
void setSynchronous(bool sync) { impl->setSync(sync); } 
void suspend();
void close();
Execution& execution() { return impl->getExecution(); }

typedef framing::TransferContent DefaultContent;
EOS
          session_methods.each { |m|
            genl
            args=m.signature_c.join(", ") 
            genl "#{m.return_type} #{m.session_function}(#{args});" 
            }
            genl
            protected
            gen <<EOS
shared_ptr<SessionCore> impl;
framing::ProtocolVersion version;
friend class Connection;
#{@classname}(shared_ptr<SessionCore>);
EOS
          }}}}

    cpp_file(@file) { 
        include @classname
        include "qpid/framing/all_method_bodies.h"
        namespace(@namespace) {
        gen <<EOS
using namespace framing;
#{@classname}::#{@classname}() {}
#{@classname}::#{@classname}(shared_ptr<SessionCore> core) : impl(core) {}
void #{@classname}::suspend() { impl->suspend(); }
void #{@classname}::close() { impl->close(); }
EOS
        session_methods.each { |m|
          genl
          sig=m.signature_c.join(", ")
          func="#{@classname}::#{m.session_function}"
          scope("#{m.return_type} #{func}(#{sig}) {") {
            args=(["ProtocolVersion()"]+m.param_names).join(", ")
            body="#{m.body_name}(#{args})"
            sendargs=body
            sendargs << ", content" if m.content
            genl "return #{m.return_type}(impl->send(#{sendargs}), impl);"
          }}}}
  end
end

class SessionGen < CppGen

  def initialize(outdir, amqp)
    super(outdir, amqp)
    @chassis="server"
    session="Session_#{@amqp.version.bars}"
    @base="no_keyword::#{session}"
    @fqclass=FqClass.new "qpid::client::#{session}"
    @classname=@fqclass.name
    @fqbase=FqClass.new("qpid::client::#{@base}")
  end

  def gen_keyword_decl(m, prefix)
    return if m.fields_c.empty?     # Inherited function will do.
    scope("BOOST_PARAMETER_MEMFUN(#{m.return_type}, #{m.session_function}, 0, #{m.fields_c.size}, #{m.argpack_name}) {") {
      scope("return #{prefix}#{m.session_function}(",");") {
        gen m.fields_c.map { |f| f.unpack() }.join(",\n")
      }
    }
    genl
  end

  def generate()
    keyword_methods=session_methods.reject { |m| m.fields_c.empty? }
    max_arity = keyword_methods.map{ |m| m.fields_c.size }.max
    
    h_file(@fqclass.file) {
      include @fqbase.file
      genl
      genl "#define BOOST_PARAMETER_MAX_ARITY #{max_arity}"
      include "<boost/parameter.hpp>"
      genl
      namespace("qpid::client") {
        # Generate keyword tag declarations.
        namespace("arg") { 
          keyword_methods.map{ |m| m.param_names_c }.flatten.uniq.each { |k|
            genl "BOOST_PARAMETER_KEYWORD(keyword_tags, #{k})"
          }}
        genl
        cpp_class(@classname,"public #{@base}") {
          private
          genl "#{@classname}(shared_ptr<SessionCore> core) : #{ @base}(core) {}"
          keyword_methods.each { |m| typedef m.argpack_type, m.argpack_name }
          genl "friend class Connection;"
          public
          genl "#{@classname}() {}"
          keyword_methods.each { |m| gen_keyword_decl(m,@base+"::") }
       }}}
  end
end

SessionNoKeywordGen.new(ARGV[0], Amqp).generate()
SessionGen.new(ARGV[0], Amqp).generate()