#!/usr/bin/ruby # # General purpose C++ code generation. # require 'amqpgen' require 'set' Copyright=< or " end end 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&"], "rfc1982-long-set"=>["SequenceNumberSet", "const SequenceNumberSet&", "const SequenceNumberSet&"], "long-struct"=>["string", "const string&"], "uuid"=>["string", "const string&"] # FIXME should be: ["Uuid", "const Uuid&", "const Uuid&"] } 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 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 end