示例#1
0
func goFunction(f *parser.Function) string {
	if f.Meta == "signal" || strings.Contains(f.Virtual, "impure") && f.Output == "void" {
		var tmp string
		for _, signalMode := range []string{"Connect", "Disconnect", "callback"} {
			f.SignalMode = signalMode
			if signalMode == "callback" {
				tmp += fmt.Sprintf("//export callback%v%v", f.Class(), strings.Replace(strings.Title(f.Name), "~", "Destroy", -1))
				if f.Overload {
					tmp += f.OverloadNumber
				}
				tmp += "\n"
			}
			tmp += fmt.Sprintf("%v{\n%v\n}\n\n", goFunctionHeader(f), goFunctionBody(f))
		}
		f.SignalMode = ""

		var isPrivate bool
		if f.Meta == "signal" {
			isPrivate = converter.IsPrivateSignal(f)
		}

		var tmpMeta = f.Meta
		f.Meta = "plain"
		if !isPrivate {
			tmp += fmt.Sprintf("%v{\n%v\n}\n\n", goFunctionHeader(f), goFunctionBody(f))
			if tmpMeta != "signal" {
				var tmpTmp = strings.Replace(fmt.Sprintf("%v{\n%v\n}\n\n", goFunctionHeader(f), goFunctionBody(f)), "_"+strings.Title(f.Name)+"(", "_"+strings.Title(f.Name)+"Default(", -1)
				tmp += strings.Replace(tmpTmp, ")"+strings.Title(f.Name)+"(", ")"+strings.Title(f.Name)+"Default(", -1)
			}
		}
		f.Meta = tmpMeta

		return tmp
	}

	if isGeneric(f) {
		var tmp string
		for _, m := range jniGenericModes(f) {
			f.TemplateMode = m
			tmp += fmt.Sprintf("%v{\n%v\n}\n", goFunctionHeader(f), goFunctionBody(f))
		}
		f.TemplateMode = ""
		return tmp
	}

	return fmt.Sprintf("%v{\n%v\n}", goFunctionHeader(f), goFunctionBody(f))
}
示例#2
0
func HTemplate(m string) (o string) {

	o += "#pragma once\n\n"

	o += fmt.Sprintf("#ifndef GO_%v_H\n", strings.ToUpper(m))
	o += fmt.Sprintf("#define GO_%v_H\n\n", strings.ToUpper(m))

	o += "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"

	var tmpArray = make([]string, 0)
	for _, c := range parser.ClassMap {
		if c.Module == m {
			tmpArray = append(tmpArray, c.Name)
		}
	}
	sort.Stable(sort.StringSlice(tmpArray))

	for _, cName := range tmpArray {
		var c = parser.ClassMap[cName]
		var virtuals = make(map[string]bool)

		for _, e := range c.Enums {
			if isSupportedEnum(e) {
				for _, v := range e.Values {
					if needsCppValueGlue(v) {
						o += fmt.Sprintf("%v;\n", cppEnumHeader(e, v))
					}
				}
			}
		}

		if isSupportedClass(c) {
			for _, f := range c.Functions {

				switch {
				case f.Meta == "signal":
					{
						for _, signalMode := range []string{"Connect", "Disconnect"} {
							f.SignalMode = signalMode
							if i := cppFunctionHeader(f); isSupportedFunction(c, f) {
								virtuals[f.Name+f.OverloadNumber] = true
								o += fmt.Sprintf("%v;\n", i)
							}
						}
						f.SignalMode = ""
					}

					var tmpMeta = f.Meta
					f.Meta = "plain"
					if i := cppFunctionHeader(f); isSupportedFunction(c, f) && !converter.IsPrivateSignal(f) {
						o += fmt.Sprintf("%v;\n", i)
					}
					f.Meta = tmpMeta

				case strings.Contains(f.Virtual, "impure") && !strings.Contains(f.Meta, "structor"):
					{
						if _, exists := virtuals[f.Name+f.OverloadNumber]; !exists {

							var tmpMeta = f.Meta
							f.Meta = "plain"
							if i := cppFunctionHeader(f); isSupportedFunction(c, f) {
								virtuals[f.Name+f.OverloadNumber] = true
								o += fmt.Sprintf("%v;\n", i)
								o += strings.Replace(fmt.Sprintf("%v;\n", i), "_"+strings.Title(f.Name), "_"+strings.Title(f.Name)+"Default", -1)
							}
							f.Meta = tmpMeta
						}
					}

				case isGeneric(f):
					{
						for _, m := range jniGenericModes(f) {
							f.TemplateMode = m
							if i := cppFunctionHeader(f); isSupportedFunction(c, f) {
								o += fmt.Sprintf("%v;\n", i)
							}
							f.TemplateMode = ""
						}
					}

				default:
					{
						if i := cppFunctionHeader(f); isSupportedFunction(c, f) {
							virtuals[f.Name+f.OverloadNumber] = true
							o += fmt.Sprintf("%v;\n", i)
						}
					}
				}
			}
		}

		for _, bcName := range c.GetAllBases([]string{}) {
			if bc, exists := parser.ClassMap[bcName]; exists {
				for _, f := range bc.Functions {

					if strings.Contains(f.Virtual, "impure") && f.Output == "void" {

						var tmpMeta = f.Meta
						f.Meta = "plain"
						if i := cppFunctionHeader(f); isSupportedFunction(bc, f) && isSupportedClass(bc) {
							if _, exists := virtuals[f.Name+f.OverloadNumber]; !exists {
								virtuals[f.Name+f.OverloadNumber] = true
								if !isBlockedVirtual(f.Name, c.Name) {
									o += strings.Replace(fmt.Sprintf("%v;\n", i), bc.Name, c.Name, -1)
									o += strings.Replace(strings.Replace(fmt.Sprintf("%v;\n", i), bc.Name, c.Name, -1), "_"+strings.Title(f.Name), "_"+strings.Title(f.Name)+"Default", -1)
								}
							}
						}
						f.Meta = tmpMeta
					}

				}
			}
		}

	}

	o += "\n#ifdef __cplusplus\n}\n#endif\n\n#endif"

	return
}
示例#3
0
func CppTemplate(module string) (o string) {

	var tmpArray = make([]string, 0)
	for _, c := range parser.ClassMap {
		if c.Module == module {
			tmpArray = append(tmpArray, c.Name)
		}
	}
	sort.Stable(sort.StringSlice(tmpArray))

	for _, cName := range tmpArray {
		var c = parser.ClassMap[cName]
		var virtuals = make(map[string]bool)

		if isSupportedClass(c) && (hasVirtualFunction(c) || hasSignalFunction(c)) {
			if !strings.Contains(c.Name, "tomic") {
				o += fmt.Sprintf("class My%v: public %v {\n", c.Name, c.Name)

				o += "public:\n"
				if !c.IsQObjectSubClass() {
					if c.Name != "QMetaType" {
						if hasVirtualFunction(c) {
							o += "\tQString _objectName;\n"
							o += "\tQString objectNameAbs() const { return this->_objectName; };\n"
							o += "\tvoid setObjectNameAbs(const QString &name) { this->_objectName = name; };\n"
						}
					}
				}

				if hasVirtualFunction(c) {
					for _, f := range c.Functions {
						if f.Meta == "constructor" && isSupportedFunction(c, f) {

							var originalInput string
							for _, p := range f.Parameters {
								if p.Name == "" {
									originalInput += "v, "
								} else {
									originalInput += fmt.Sprintf("%v, ", p.Name)
								}
							}
							originalInput = strings.TrimSuffix(originalInput, ", ")

							o += fmt.Sprintf("\tMy%v(%v) : %v(%v) {};\n", f.Class(), strings.Split(strings.Split(f.Signature, "(")[1], ")")[0], f.Class(), originalInput)
						}
					}
				}

				for _, f := range c.Functions {
					if (f.Meta == "signal" || strings.Contains(f.Virtual, "impure")) && f.Output == "void" {
						if i := cppFunctionSignal(f); isSupportedFunction(c, f) {
							if strings.Contains(f.Virtual, "impure") {
								if _, exists := virtuals[f.Name+f.OverloadNumber]; !exists {
									virtuals[f.Name+f.OverloadNumber] = true
									if !isBlockedVirtual(f.Name, c.Name) {
										o += fmt.Sprintf("\t%v;\n", i)
									}
								}
							} else {
								o += fmt.Sprintf("\t%v;\n", i)
							}
						}
					} else {
						virtuals[f.Name+f.OverloadNumber] = true
					}
				}

				for _, bcName := range c.GetAllBases([]string{}) {
					if bc, exists := parser.ClassMap[bcName]; exists {
						for _, f := range bc.Functions {
							if strings.Contains(f.Virtual, "impure") && f.Output == "void" {
								if i := cppFunctionSignal(f); isSupportedFunction(bc, f) && isSupportedClass(bc) {
									if _, exists := virtuals[f.Name+f.OverloadNumber]; !exists {
										virtuals[f.Name+f.OverloadNumber] = true
										if !isBlockedVirtual(f.Name, c.Name) {
											o += strings.Replace(fmt.Sprintf("\t%v;\n", i), bc.Name, c.Name, -1)
										}
									}
								}
							}
						}
					}
				}

				o += "};\n\n"
			}
		}

		for _, e := range c.Enums {
			if isSupportedEnum(e) {
				for _, v := range e.Values {
					if needsCppValueGlue(v) {
						o += fmt.Sprintf("%v\n\n", cppEnum(e, v))
					}
				}
			}
		}

		virtuals = make(map[string]bool)
		if isSupportedClass(c) {
			for _, f := range c.Functions {

				switch {
				case f.Meta == "signal":
					{
						for _, signalMode := range []string{"Connect", "Disconnect"} {
							f.SignalMode = signalMode
							if i := cppFunction(f); isSupportedFunction(c, f) {
								virtuals[f.Name+f.OverloadNumber] = true
								o += fmt.Sprintf("%v\n\n", i)
							}
						}
						f.SignalMode = ""

						var tmpMeta = f.Meta
						f.Meta = "plain"
						if i := cppFunction(f); isSupportedFunction(c, f) && !converter.IsPrivateSignal(f) {
							o += fmt.Sprintf("%v\n\n", i)
						}
						f.Meta = tmpMeta
					}

				default:
					{
						if i := cppFunction(f); isSupportedFunction(c, f) {
							if _, exists := virtuals[f.Name+f.OverloadNumber]; !exists {
								virtuals[f.Name+f.OverloadNumber] = true

								var normal = fmt.Sprintf("%v\n\n", i)

								if strings.Contains(f.Virtual, "impure") && f.Output == "void" && (hasVirtualFunction(c) || hasSignalFunction(c)) {
									normal = strings.Replace(normal, "static_cast<"+c.Name+"*>(ptr)", "static_cast<My"+c.Name+"*>(ptr)", -1)
								}

								o += normal

								if strings.Contains(f.Virtual, "impure") && f.Output == "void" {
									var tmp = strings.Replace(fmt.Sprintf("%v\n\n", i), "_"+strings.Title(f.Name), "_"+strings.Title(f.Name)+"Default", -1)
									tmp = strings.Replace(tmp, "->"+f.Name, "->"+c.Name+"::"+f.Name, -1)

									o += tmp
								}
							}
						}
					}
				}
			}

			for _, bcName := range c.GetAllBases([]string{}) {
				if bc, exists := parser.ClassMap[bcName]; exists {
					for _, f := range bc.Functions {

						if strings.Contains(f.Virtual, "impure") && f.Output == "void" {

							if i := cppFunction(f); isSupportedFunction(bc, f) && isSupportedClass(bc) {
								if _, exists := virtuals[f.Name+f.OverloadNumber]; !exists {
									virtuals[f.Name+f.OverloadNumber] = true
									if !isBlockedVirtual(f.Name, c.Name) {

										var normal = strings.Replace(fmt.Sprintf("%v\n\n", i), bc.Name, c.Name, -1)
										if strings.Contains(f.Virtual, "impure") && f.Output == "void" && (hasVirtualFunction(c) || hasSignalFunction(c)) {
											normal = strings.Replace(normal, "static_cast<"+c.Name+"*>(ptr)", "static_cast<My"+c.Name+"*>(ptr)", -1)
										}

										o += normal

										var tmp = strings.Replace(strings.Replace(fmt.Sprintf("%v\n\n", i), bc.Name, c.Name, -1), "_"+strings.Title(f.Name), "_"+strings.Title(f.Name)+"Default", -1)
										tmp = strings.Replace(tmp, "->"+f.Name, "->"+c.Name+"::"+f.Name, -1)

										o += tmp
									}
								}
							}
						}
					}
				}
			}
		}
	}

	return managedImportsCpp(module, o)
}
示例#4
0
func cppFunctionBody(f *parser.Function) (o string) {

	/*
		for _, p := range f.Parameters {
			if strings.Contains(p.Value, "**") && p.Name == "argv" {
				o += "QList<QByteArray> aList = QByteArray(argv).split('|');\n"
				o += "\tQVarLengthArray<const char*> argvs(argc);\n"
				o += "\tstatic int argcs = argc;\n"
				o += "\tfor (int i = 0; i < argc; i++)\n"
				o += "\t\targvs[i] = static_cast<const char*>(aList[i].constData());\n\n\t"
			}
		}
	*/

	for _, p := range f.Parameters {
		if strings.Contains(p.Value, "**") && p.Name == "argv" {
			o += "QList<QByteArray> aList = QByteArray(argv).split('|');\n"
			o += "\tchar *argvs[argc];\n"
			o += "\tstatic int argcs = argc;\n"
			o += "\tfor (int i = 0; i < argc; i++)\n"
			o += "\t\targvs[i] = aList[i].data();\n\n\t"
		}
	}

	if f.Name == "objectNameAbs" || f.Name == "setObjectNameAbs" {
		o += fmt.Sprintf("if (dynamic_cast<My%v*>(static_cast<%v*>(ptr))) {\n\t\t", f.Class(), f.Class())
	}

	if converter.CppHeaderOutput(f) != "void" {
		o += "return "
	}

	var tmpMeta string
	if strings.Contains(f.Virtual, "impure") {
		if isDerivedFromSlot(f) {
			tmpMeta = f.Meta
			f.Meta = "slot"
		}
	}

	switch f.Meta {
	case "constructor":
		{
			if hasVirtualFunction(parser.ClassMap[f.Class()]) {
				o += fmt.Sprintf("new My%v(%v)", f.Class(), converter.CppBodyInput(f))
			} else {
				o += fmt.Sprintf("new %v(%v)", f.Class(), converter.CppBodyInput(f))
			}
		}
	case "slot":
		{
			if f.Static {
				o += fmt.Sprintf("QMetaObject::invokeMethod(%v::instance(), \"%v\"%v)", f.Class(), f.Name, converter.CppBodyInput(f))
			} else {
				o += fmt.Sprintf("QMetaObject::invokeMethod(static_cast<%v*>(ptr), \"%v\"%v)", f.Class(), f.Name, converter.CppBodyInput(f))
			}
		}
	case "plain", "destructor":
		{
			if f.Static {
				o += converter.CppBodyOutput(f, fmt.Sprintf("%v::%v%v(%v)", f.Class(), f.Name, converter.DeduceGeneric(f), converter.CppBodyInput(f)))
			} else {
				if f.Output == "T" && f.Class() == "QObject" {
					o += converter.CppBodyOutput(f, fmt.Sprintf("static_cast<%v*>(ptr)->%v<QObject*>(%v)", f.Class(), f.Name, converter.CppBodyInput(f)))
				} else if f.Output == "T" && f.Class() == "QMediaService" {
					o += converter.CppBodyOutput(f, fmt.Sprintf("static_cast<%v*>(ptr)->%v<QMediaControl*>(%v)", f.Class(), f.Name, converter.CppBodyInput(f)))
				} else {
					if parser.ClassMap[f.Class()].IsQObjectSubClass() {
						o += converter.CppBodyOutput(f, fmt.Sprintf("static_cast<%v*>(ptr)->%v%v(%v)", f.Class(), f.Name, converter.DeduceGeneric(f), converter.CppBodyInput(f)))
					} else {
						if f.Name == "objectNameAbs" || f.Name == "setObjectNameAbs" {
							o += converter.CppBodyOutput(f, fmt.Sprintf("static_cast<My%v*>(ptr)->%v%v(%v)", f.Class(), f.Name, converter.DeduceGeneric(f), converter.CppBodyInput(f)))
						} else {
							o += converter.CppBodyOutput(f, fmt.Sprintf("static_cast<%v*>(ptr)->%v%v(%v)", f.Class(), f.Name, converter.DeduceGeneric(f), converter.CppBodyInput(f)))
						}
					}
				}
			}
		}
	case "signal":
		{
			if converter.IsPrivateSignal(f) {
				o += fmt.Sprintf("QObject::%v(%v, &%v::%v, static_cast<My%v*>(ptr), static_cast<%v (My%v::*)(%v)>(&My%v::Signal_%v%v));", strings.ToLower(f.SignalMode), fmt.Sprintf("static_cast<%v*>(%v)", f.Class(), "ptr"), f.Class(), f.Name, f.Class(), f.Output, f.Class(), converter.CppBodyInput(f), f.Class(), strings.Title(f.Name), cppFunctionSignalOverload(f))
			} else {
				o += fmt.Sprintf("QObject::%v(%v, static_cast<void (%v::*)(%v)>(&%v::%v), static_cast<My%v*>(ptr), static_cast<%v (My%v::*)(%v)>(&My%v::Signal_%v%v));", strings.ToLower(f.SignalMode), fmt.Sprintf("static_cast<%v*>(%v)", f.Class(), "ptr"), f.Class(), converter.CppBodyInput(f), f.Class(), f.Name, f.Class(), f.Output, f.Class(), converter.CppBodyInput(f), f.Class(), strings.Title(f.Name), cppFunctionSignalOverload(f))
			}
		}
	default:
		{
			f.Access = "unsupported_CppFunctionBody"
			return f.Access
		}
	}

	if tmpMeta != "" {
		f.Meta = tmpMeta
	}

	if f.Name == "objectNameAbs" {
		o += fmt.Sprintf(";\n\t}\n\treturn QString(\"%v_BASE\").toUtf8().data()", f.Class())
	} else if f.Name == "setObjectNameAbs" {
		return o + ";\n\t}"
	}

	return o + ";"
}