summaryrefslogtreecommitdiff
path: root/cpp/rubygen/templates/Session.rb
blob: 4cea9f966507c27e3a6946d3339ee9b75031b3bd (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
#!/usr/bin/env ruby
# Usage: output_directory xml_spec_file [xml_spec_file...]
# 
$: << '..'
require 'cppgen'

class SessionGen < CppGen

  def initialize(outdir, amqp)
    super(outdir, amqp)
    @chassis="server"
    @classname="Session"
  end
  
  def return_type(m)
    if (m.result)
      return "TypedResult<qpid::framing::#{m.result.cpptype.ret}>"
    elsif (not m.responses().empty?)
      return "Response"
    else
      return "Completion"
    end 
  end

  def declare_method (m)
    gen "#{return_type(m)} #{m.parent.name.lcaps}#{m.name.caps}(" 
    if (m.content())
      params=m.signature + ["const MethodContent& content"]
    else
      params=m.signature
    end
    indent { gen params.join(",\n") }
    gen ");\n\n"
  end

  def declare_class(c)
    c.methods_on(@chassis).each { |m| declare_method(m) }
  end

  def define_method (m)
    gen "#{return_type(m)} Session::#{m.parent.name.lcaps}#{m.name.caps}(" 
    if (m.content())
      params=m.signature + ["const MethodContent& content"]
    else
      params=m.signature
    end
    indent { gen params.join(",\n") }
    gen "){\n\n"
    indent (2) { 
      gen "return #{return_type(m)}(impl()->send(#{m.body_name}(" 
      params = ["version"] + m.param_names
      gen params.join(", ")
      other_params=[]
      if (m.content())
        gen "), content), impl());\n"
      else
        gen ")), impl());\n"
      end
    }
    gen "}\n\n"
  end

  def define_class(c)
    c.methods_on(@chassis).each { |m| define_method(m) }
  end

  def generate()
    excludes = ["channel", "connection", "session", "execution"]

    h_file("qpid/client/#{@classname}.h") { 
      gen <<EOS
#include <sstream> 
#include "qpid/framing/amqp_framing.h"
#include "qpid/framing/amqp_structs.h"
#include "qpid/framing/ProtocolVersion.h"
#include "qpid/framing/MethodContent.h"
#include "qpid/client/ConnectionImpl.h"
#include "qpid/client/Response.h"
#include "qpid/client/ScopedAssociation.h"
#include "qpid/client/TypedResult.h"

namespace qpid {
namespace client {

using std::string;
using framing::Content;
using framing::FieldTable;
using framing::MethodContent;
using framing::SequenceNumberSet;

class #{@classname} {
  ScopedAssociation::shared_ptr assoc;
  framing::ProtocolVersion version;
  
  SessionCore::shared_ptr impl();

public:
    #{@classname}();
    #{@classname}(ScopedAssociation::shared_ptr);

    framing::FrameSet::shared_ptr get() { return impl()->get(); }
    void setSynchronous(bool sync) { impl()->setSync(sync); } 
    void close();
    Execution& execution() { return impl()->getExecution(); }

EOS
  indent { @amqp.classes.each { |c| declare_class(c) if !excludes.include?(c.name) } }
  gen <<EOS
}; /* class #{@classname} */
}
}
EOS
}

  # .cpp file
  cpp_file("qpid/client/#{@classname}.cpp") { 
    gen <<EOS
#include "#{@classname}.h"
#include "qpid/framing/all_method_bodies.h"

using std::string;
using namespace qpid::framing;

namespace qpid {
namespace client {

#{@classname}::#{@classname}() {}
#{@classname}::#{@classname}(ScopedAssociation::shared_ptr _assoc) : assoc(_assoc) {}

SessionCore::shared_ptr #{@classname}::impl()
{
    if (!assoc) throw Exception("Uninitialised session");
    return assoc->session;
}

void #{@classname}::close()
{
    impl()->close(); 
}

EOS

  @amqp.classes.each { |c| define_class(c) if !excludes.include?(c.name)  }
  
  gen <<EOS
}} // namespace qpid::client
EOS
  }

  end
end

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