#!/usr/bin/ruby # # General purpose C++ code generation. # require 'amqpgen' require 'set' Copyright=< 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