summaryrefslogtreecommitdiff
path: root/cpp/rubygen/cppgen.rb
blob: 0063231e8444e53088d6015d97b484e02c6d0d6e (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/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.
 *
 */

///
/// This file was automatically generated from the AMQP specification.
/// Do not edit.
///


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

# Hold information about a C++ type.
class CppType
  def initialize(name) @name=@param=@ret=name; end
  attr_reader :name, :param, :ret, :code
  
  def retref() @ret="#{name}&"; self; end
  def retcref() @ret="const #{name}&"; self; end
  def passcref() @param="const #{name}&"; self; end
  def code(str) @code=str; self; end

  def encode(value, buffer)
    @code ? "#{buffer}.put#{@code}(#{value});" : "#{value}.encode(#{buffer});"
  end

  def decode(value,buffer)
    if @code
      if /&$/===param then
        "#{buffer}.get#{@code}(#{value});"
      else
        "#{value} = #{buffer}.get#{@code}();"
      end
    else
      "#{value}.decode(#{buffer});"
    end
  end

  def to_s() name; end;
end

class AmqpField
  def cppname() name.lcaps.cppsafe; end
  def cpptype() domain.cpptype;  end
  def bit?() domain.type_ == "bit"; end
end

class AmqpMethod
  def cppname() name.lcaps.cppsafe; end
  def param_names() fields.map { |f| f.cppname }; end
  def signature() fields.map { |f| f.cpptype.param+" "+f.cppname }; end
  def body_name() parent.name.caps+name.caps+"Body"; end
end

class AmqpClass
  def cppname() name.caps; end
end

class AmqpDomain
  @@typemap = {
    "bit"=> CppType.new("bool").code("Octet"),
    "octet"=>CppType.new("uint8_t").code("Octet"), 
    "short"=>CppType.new("uint16_t").code("Short"),
    "long"=>CppType.new("uint32_t").code("Long"),
    "longlong"=>CppType.new("uint64_t").code("LongLong"),
    "timestamp"=>CppType.new("uint64_t").code("LongLong"),
    "longstr"=>CppType.new("string").passcref.retcref.code("LongString"),
    "shortstr"=>CppType.new("string").passcref.retcref.code("ShortString"),
    "table"=>CppType.new("FieldTable").passcref.retcref.code("FieldTable"),
    "content"=>CppType.new("Content").passcref.retcref.code("Content"),
    "rfc1982-long-set"=>CppType.new("SequenceNumberSet").passcref.retcref,
    "long-struct"=>CppType.new("string").passcref.retcref.code("LongString"),
    # FIXME aconway 2007-08-25: Use Uuid class.
    # "uuid"=>CppType.new("Uuid").passcref.retcref.code,
    "uuid"=>CppType.new("string").passcref.retcref.code("ShortString") 
  }

  def cppname() name.caps; end

  def cpptype()
    d=unalias
    @cpptype ||= @@typemap[d.type_] or
      CppType.new(d.cppname).passcref.retcref or
      raise "Invalid type #{self}"
  end
end

class AmqpResult
  def cpptype()
    @cpptype=CppType.new(parent.parent.name.caps+parent.name.caps+"Result").passcref
  end
end

class AmqpStruct
  def cpptype() parent.cpptype; end
  def cppname() cpptype.name;  end
end

class CppGen < Generator
  def initialize(outdir, *specs)
    super(outdir,*specs)
  end

  # Write a header file. 
  def h_file(path)
    path = (/\.h$/ === path ? path : path+".h")
    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)
    path = (/\.cpp$/ === path ? path : path+".cpp")
    file(path) do
      gen Copyright
      yield
    end
  end

  def include(header)
    header+=".h" unless /(\.h|[">])$/===header
    header="\"#{header}\"" unless /(^<.*>$)|(^".*"$)/===header
    genl "#include #{header}"
  end

  def scope(open="{",close="}", &block) 
    genl open; indent(&block); genl close
  end

  def namespace(name, &block) 
    genl
    names = name.split("::")
    names.each { |n| genl "namespace #{n} {" }
    genl
    yield
    genl
    genl('}'*names.size+" // "+name)
    genl
  end

  def struct_class(type, name, bases, &block)
    genl
    gen "#{type} #{name}"
    if (!bases.empty?)
      genl ":"
      indent { gen "#{bases.join(",\n")}" }
    end
    genl
    scope("{","};") { yield }
  end

  def struct(name, *bases, &block)
    struct_class("struct", name, bases, &block);
  end
  def cpp_class(name, *bases, &block)
    struct_class("class", name, bases, &block);
  end

  def typedef(type, name) genl "typedef #{type} #{name};\n"; end

  def variant(types) "boost::variant<#{types.join(", ")}>"; end
  def variantl(types) "boost::variant<#{types.join(", \n")}>"; end
  def blank_variant(types) variant(["boost::blank"]+types); end
  def tuple(types) "boost::tuple<#{types.join(', ')}>"; end

  def public() outdent { genl "public:" } end
  def private() outdent { genl "private:" } end
  def protected() outdent { genl "protected:" } end

  # Returns [namespace, classname, filename]
  def parse_classname(full_cname)
    names=full_cname.split("::")
    return names[0..-2].join('::'), names[-1], names.join("/") 
  end
end