templates.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>, Tatyana Borisova <tanusshhka@mail.ru>
  5. *
  6. * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "templates.h"
  26. using namespace QtProtobuf::generator;
  27. const char *Templates::ProtoSufix = "_proto";
  28. const std::vector<std::string> Templates::ListOfQmlExeptions{"id", "property", "import"};
  29. const char *Templates::DefaultProtobufIncludesTemplate = "#include <QMetaType>\n"
  30. "#include <QList>\n"
  31. "#include <QtProtobuf/qprotobufobject.h>\n"
  32. "#include <QtProtobuf/qprotobuflazymessagepointer.h>\n"
  33. "#include <QSharedPointer>\n"
  34. "\n"
  35. "#include <memory>\n"
  36. "#include <unordered_map>\n"
  37. "\n";
  38. const char *Templates::QmlProtobufIncludesTemplate = "#include <QtQml/QQmlListProperty>\n"
  39. "#include <QtProtobuf/qtprotobufqmllistpropertyconstructor.h>\n\n";
  40. const char *Templates::GlobalEnumClassNameTemplate = "GlobalEnums";
  41. const char *Templates::DisclaimerTemplate = "/* This file is autogenerated. DO NOT CHANGE. All changes will be lost */\n\n";
  42. const char *Templates::PreambleTemplate = "#pragma once\n\n"
  43. "#include <QObject>\n";
  44. const char *Templates::InternalIncludeTemplate = "#include \"$include$.h\"\n";
  45. const char *Templates::ExternalIncludeTemplate = "#include <$include$>\n";
  46. const char *Templates::GlobalEnumIncludeTemplate = "#include <globalenums.h>\n";
  47. const char *Templates::UsingQtProtobufNamespaceTemplate = "\n";
  48. const char *Templates::ManualRegistrationDeclaration = "static void registerTypes();\n";
  49. const char *Templates::ManualRegistrationComplexTypeDefinition = "void $type$::registerTypes()\n{\n"
  50. " qRegisterMetaType<$type$>(\"$full_type$\");\n"
  51. " qRegisterMetaType<$type$*>(\"$full_type$*\");\n" //Somehow for aliastypes qRegisterMetaType logic doesn't work for pointer type registration
  52. " qRegisterMetaType<$list_type$>(\"$full_list_type$\");\n"
  53. "";
  54. const char *Templates::ManualRegistrationGlobalEnumDefinition = "void $enum_gadget$::registerTypes()\n{\n"
  55. "";
  56. const char *Templates::ComplexGlobalEnumFieldRegistrationTemplate = "qRegisterMetaType<$type$>(\"$full_type$\");\n";
  57. const char *Templates::ComplexListTypeUsingTemplate = "using $classname$Repeated = QList<QSharedPointer<$classname$>>;\n";
  58. const char *Templates::MapTypeUsingTemplate = "using $type$ = QMap<$key_type$, $value_type$>;\n";
  59. const char *Templates::MessageMapTypeUsingTemplate = "using $type$ = QMap<$key_type$, QSharedPointer<$value_type$>>;\n";
  60. const char *Templates::NestedMessageUsingTemplate = "using $type$ = $scope_namespaces$::$type$;\n"
  61. "using $list_type$ = $scope_namespaces$::$list_type$;\n";
  62. const char *Templates::EnumTypeRepeatedTemplate = "using $list_type$ = QList<$type$>;\n";
  63. const char *Templates::NamespaceTemplate = "namespace $namespace$ {\n";
  64. const char *Templates::UsingNamespaceTemplate = "using namespace $namespace$;\n";
  65. const char *Templates::ClassDeclarationTemplate = "\nclass $classname$ : public QObject\n"
  66. "{\n"
  67. " Q_OBJECT\n";
  68. const char *Templates::ProtoClassForwardDeclarationTemplate = "class $classname$;\n";
  69. const char *Templates::ProtoClassDeclarationBeginTemplate = "\nclass $classname$ : public QObject\n"
  70. "{\n"
  71. " Q_OBJECT\n"
  72. " Q_PROTOBUF_OBJECT\n"
  73. " Q_DECLARE_PROTOBUF_SERIALIZERS($classname$)\n";
  74. const char *Templates::PropertyTemplate = "Q_PROPERTY($property_type$ $property_name$ READ $property_name$ WRITE set$property_name_cap$ NOTIFY $property_name$Changed SCRIPTABLE $scriptable$)\n";
  75. const char *Templates::RepeatedPropertyTemplate = "Q_PROPERTY($property_list_type$ $property_name$ READ $property_name$ WRITE set$property_name_cap$ NOTIFY $property_name$Changed SCRIPTABLE $scriptable$)\n";
  76. const char *Templates::RepeatedMessagePropertyTemplate = "Q_PROPERTY($property_list_type$ $property_name$Data READ $property_name$ WRITE set$property_name_cap$ NOTIFY $property_name$Changed SCRIPTABLE $scriptable$)\n";
  77. const char *Templates::NonScriptablePropertyTemplate = "Q_PROPERTY($property_type$ $property_name$_p READ $property_name$ WRITE set$property_name_cap$ NOTIFY $property_name$Changed SCRIPTABLE false)\n";
  78. const char *Templates::NonScriptableAliasPropertyTemplate = "Q_PROPERTY($qml_alias_type$ $property_name$ READ $property_name$_p WRITE set$property_name_cap$_p NOTIFY $property_name$Changed SCRIPTABLE true)\n";
  79. const char *Templates::MessagePropertyTemplate = "Q_PROPERTY($property_type$ *$property_name$ READ $property_name$_p WRITE set$property_name_cap$_p NOTIFY $property_name$Changed)\n";
  80. const char *Templates::QmlListPropertyTemplate = "Q_PROPERTY(QQmlListProperty<$property_type$> $property_name$ READ $property_name$_l NOTIFY $property_name$Changed)\n";
  81. const char *Templates::ConstructorParameterTemplate = "$scope_type$ $property_name$,";
  82. const char *Templates::ConstructorMessageParameterTemplate = "const $scope_type$ &$property_name$,";
  83. const char *Templates::ConstructorRepeatedParameterTemplate = "const $scope_list_type$ &$property_name$,";
  84. const char *Templates::ProtoConstructorBeginTemplate = "$classname$(";
  85. const char *Templates::ProtoConstructorEndTemplate = "QObject *parent = nullptr);\n";
  86. const char *Templates::MemberTemplate = "$scope_type$ m_$property_name$;\n";
  87. const char *Templates::ListMemberTemplate = "$scope_list_type$ m_$property_name$;\n";
  88. const char *Templates::ComplexMemberTemplate = "QProtobufLazyMessagePointer<$scope_type$> m_$property_name$;\n";
  89. const char *Templates::PublicBlockTemplate = "\npublic:\n";
  90. const char *Templates::PrivateBlockTemplate = "\nprivate:\n";
  91. const char *Templates::EnumDefinitionTemplate = "enum $type$ {\n";
  92. const char *Templates::EnumFieldTemplate = "$enumvalue$ = $value$,\n";
  93. const char *Templates::ProtoConstructorDefinitionBeginTemplate = "$type$::$type$(";
  94. const char *Templates::ProtoConstructorDefinitionEndTemplate = "QObject *parent) : QObject(parent)";
  95. const char *Templates::ConstructorTemplate = "$classname$();\n";
  96. const char *Templates::QObjectConstructorTemplate = "explicit $classname$(QObject *parent = nullptr);\n";
  97. const char *Templates::ConstructorHeaderTemplate = "$classname$() {}\n";
  98. const char *Templates::CopyConstructorDeclarationTemplate = "$classname$(const $classname$ &other);\n";
  99. const char *Templates::MoveConstructorDeclarationTemplate = "$classname$($classname$ &&other);\n";
  100. const char *Templates::CopyConstructorDefinitionTemplate = "$classname$::$classname$(const $classname$ &other) : QObject()";
  101. const char *Templates::MoveConstructorDefinitionTemplate = "$classname$::$classname$($classname$ &&other) : QObject()";
  102. const char *Templates::EmptyCopyConstructorDefinitionTemplate = "$classname$::$classname$(const $classname$ &) : QObject()";
  103. const char *Templates::EmptyMoveConstructorDefinitionTemplate = "$classname$::$classname$($classname$ &&) : QObject()";
  104. const char *Templates::DeletedCopyConstructorTemplate = "$classname$(const $classname$ &) = delete;\n";
  105. const char *Templates::DeletedMoveConstructorTemplate = "$classname$($classname$ &&) = delete;\n";
  106. const char *Templates::CopyFieldTemplate = "set$property_name_cap$(other.m_$property_name$);\n";
  107. const char *Templates::CopyComplexFieldTemplate = "if (m_$property_name$ != other.m_$property_name$) {\n"
  108. " *m_$property_name$ = *other.m_$property_name$;\n"
  109. "}\n";
  110. const char *Templates::AssignComplexFieldTemplate = "if (m_$property_name$ != other.m_$property_name$) {\n"
  111. " *m_$property_name$ = *other.m_$property_name$;\n"
  112. " $property_name$Changed();\n"
  113. "}\n";
  114. const char *Templates::MoveMessageFieldTemplate = "if (m_$property_name$ != other.m_$property_name$) {\n"
  115. " *m_$property_name$ = std::move(*other.m_$property_name$);\n"
  116. "}\n";
  117. const char *Templates::MoveAssignMessageFieldTemplate = "if (m_$property_name$ != other.m_$property_name$) {\n"
  118. " *m_$property_name$ = std::move(*other.m_$property_name$);\n"
  119. " $property_name$Changed();\n"
  120. " other.$property_name$Changed();\n"
  121. "}\n";
  122. const char *Templates::MoveComplexFieldTemplate = "if (m_$property_name$ != other.m_$property_name$) {\n"
  123. " m_$property_name$ = std::move(other.m_$property_name$);\n"
  124. " $property_name$Changed();\n"
  125. " other.$property_name$Changed();\n"
  126. "}";
  127. const char *Templates::MoveComplexFieldConstructorTemplate = "m_$property_name$ = std::move(other.m_$property_name$);\n"
  128. "other.$property_name$Changed();\n";
  129. const char *Templates::MoveFieldTemplate = "set$property_name_cap$(std::exchange(other.m_$property_name$, 0));\n"
  130. "other.$property_name$Changed();\n";
  131. const char *Templates::EnumMoveFieldTemplate = "m_$property_name$ = other.m_$property_name$;\n";
  132. const char *Templates::AssignmentOperatorDeclarationTemplate = "$classname$ &operator =(const $classname$ &other);\n";
  133. const char *Templates::AssignmentOperatorDefinitionTemplate = "$classname$ &$classname$::operator =(const $classname$ &other)\n{\n";
  134. const char *Templates::EmptyAssignmentOperatorDefinitionTemplate = "$classname$ &$classname$::operator =(const $classname$ &)\n{\n";
  135. const char *Templates::AssignmentOperatorReturnTemplate = "return *this;\n";
  136. const char *Templates::MoveAssignmentOperatorDeclarationTemplate = "$classname$ &operator =($classname$ &&other);\n";
  137. const char *Templates::MoveAssignmentOperatorDefinitionTemplate = "$classname$ &$classname$::operator =($classname$ &&other)\n{\n";
  138. const char *Templates::EmptyMoveAssignmentOperatorDefinitionTemplate = "$classname$ &$classname$::operator =($classname$ &&)\n{\n";
  139. const char *Templates::EqualOperatorDeclarationTemplate = "bool operator ==(const $classname$ &other) const;\n";
  140. const char *Templates::EqualOperatorDefinitionTemplate = "bool $classname$::operator ==(const $classname$ &other) const\n{\n"
  141. " return ";
  142. const char *Templates::EmptyEqualOperatorDefinitionTemplate = "bool $classname$::operator ==(const $classname$ &) const\n{\n"
  143. " return true;\n"
  144. "}\n\n";
  145. const char *Templates::EqualOperatorPropertyTemplate = "m_$property_name$ == other.m_$property_name$";
  146. const char *Templates::EqualOperatorMessagePropertyTemplate = "(m_$property_name$ == other.m_$property_name$\n"
  147. " || *m_$property_name$ == *other.m_$property_name$)\n";
  148. const char *Templates::EqualOperatorRepeatedPropertyTemplate = "QtProtobuf::repeatedValueCompare(m_$property_name$, other.m_$property_name$)";
  149. const char *Templates::NotEqualOperatorDeclarationTemplate = "bool operator !=(const $classname$ &other) const;\n";
  150. const char *Templates::NotEqualOperatorDefinitionTemplate = "bool $classname$::operator !=(const $classname$ &other) const\n{\n"
  151. " return !this->operator ==(other);\n"
  152. "}\n\n";
  153. const char *Templates::GetterPrivateMessageDeclarationTemplate = "$getter_type$ *$property_name$_p() const;\n";
  154. const char *Templates::GetterPrivateMessageDefinitionTemplate = "$getter_type$ *$classname$::$property_name$_p() const\n{\n"
  155. " return m_$property_name$.get();\n"
  156. "}\n\n";
  157. const char *Templates::GetterMessageDeclarationTemplate = "$getter_type$ &$property_name$() const;\n";
  158. const char *Templates::GetterMessageDefinitionTemplate = "$getter_type$ &$classname$::$property_name$() const\n{\n"
  159. " return *m_$property_name$;\n"
  160. "}\n\n";
  161. const char *Templates::GetterTemplate = "$getter_type$ $property_name$() const {\n"
  162. " return m_$property_name$;\n"
  163. "}\n\n";
  164. const char *Templates::NonScriptableGetterTemplate = "$qml_alias_type$ $property_name$_p() const {\n"
  165. " return m_$property_name$;\n"
  166. "}\n\n";
  167. const char *Templates::GetterContainerExtraTemplate = "$getter_type$ &$property_name$() {\n"
  168. " return m_$property_name$;\n"
  169. "}\n\n";
  170. const char *Templates::GetterQmlListDeclarationTemplate = "QQmlListProperty<$scope_type$> $property_name$_l();\n";
  171. const char *Templates::GetterQmlListDefinitionTemplate = "QQmlListProperty<$full_type$> $classname$::$property_name$_l()\n{\n"
  172. " return qProtobufConstructQmlListProperty<$scope_type$>(this, &m_$property_name$);\n"
  173. "}\n\n";
  174. const char *Templates::SetterPrivateTemplateDeclarationMessageType = "void set$property_name_cap$_p($setter_type$ *$property_name$);\n";
  175. const char *Templates::SetterPrivateTemplateDefinitionMessageType = "void $classname$::set$property_name_cap$_p($setter_type$ *$property_name$)\n{\n"
  176. " if (m_$property_name$.get() != $property_name$) {\n"
  177. " m_$property_name$.reset($property_name$);\n"
  178. " $property_name$Changed();\n"
  179. " }\n"
  180. "}\n\n";
  181. const char *Templates::SetterTemplateDeclarationMessageType = "void set$property_name_cap$(const $setter_type$ &$property_name$);\n";
  182. const char *Templates::SetterTemplateDefinitionMessageType = "void $classname$::set$property_name_cap$(const $setter_type$ &$property_name$)\n{\n"
  183. " if (*m_$property_name$ != $property_name$) {\n"
  184. " *m_$property_name$ = $property_name$;\n"
  185. " $property_name$Changed();\n"
  186. " }\n"
  187. "}\n\n";
  188. const char *Templates::SetterTemplateDeclarationComplexType = "void set$property_name_cap$(const $setter_type$ &$property_name$);\n";
  189. const char *Templates::SetterTemplateDefinitionComplexType = "void $classname$::set$property_name_cap$(const $setter_type$ &$property_name$)\n{\n"
  190. " if (m_$property_name$ != $property_name$) {\n"
  191. " m_$property_name$ = $property_name$;\n"
  192. " $property_name$Changed();\n"
  193. " }\n"
  194. "}\n\n";
  195. const char *Templates::SetterTemplate = "void set$property_name_cap$(const $setter_type$ &$property_name$) {\n"
  196. " if (m_$property_name$ != $property_name$) {\n"
  197. " m_$property_name$ = $property_name$;\n"
  198. " $property_name$Changed();\n"
  199. " }\n"
  200. "}\n\n";
  201. const char *Templates::NonScriptableSetterTemplate = "void set$property_name_cap$_p(const $qml_alias_type$ &$property_name$) {\n"
  202. " if (m_$property_name$ != $property_name$) {\n"
  203. " m_$property_name$ = $property_name$;\n"
  204. " $property_name$Changed();\n"
  205. " }\n"
  206. "}\n\n";
  207. const char *Templates::SignalsBlockTemplate = "\nsignals:\n";
  208. const char *Templates::SignalTemplate = "void $property_name$Changed();\n";
  209. const char *Templates::FieldsOrderingContainerTemplate = "const QProtobufMetaObject $type$::protobufMetaObject = QProtobufMetaObject($type$::staticMetaObject, $type$::propertyOrdering);\n"
  210. "const QProtobufPropertyOrdering $type$::propertyOrdering = {";
  211. const char *Templates::FieldOrderTemplate = "{$field_number$, {$property_number$, \"$json_name$\"}}";
  212. const char *Templates::EnumTemplate = "$type$";
  213. const char *Templates::SimpleBlockEnclosureTemplate = "}\n";
  214. const char *Templates::SemicolonBlockEnclosureTemplate = "};\n";
  215. const char *Templates::EmptyBlockTemplate = "{}\n\n";
  216. const char *Templates::PropertyInitializerTemplate = "\n , m_$property_name$($property_name$)";
  217. const char *Templates::PropertyDefaultInitializerTemplate = "\n , m_$property_name$($initializer$)";
  218. const char *Templates::MessagePropertyInitializerTemplate = "\n , m_$property_name$(new $scope_type$($property_name$))";
  219. const char *Templates::MessagePropertyDefaultInitializerTemplate = "\n , m_$property_name$(nullptr)";
  220. const char *Templates::ConstructorContentTemplate = "\n{\n}\n";
  221. const char *Templates::DeclareMetaTypeTemplate = "Q_DECLARE_METATYPE($full_type$)\n";
  222. const char *Templates::DeclareMetaTypeListTemplate = "Q_DECLARE_METATYPE($full_list_type$)\n";
  223. const char *Templates::DeclareMessageMetaTypeTemplate = "Q_DECLARE_METATYPE($full_type$)\n"
  224. "Q_DECLARE_OPAQUE_POINTER($full_type$)\n";
  225. const char *Templates::DeclareComplexListTypeTemplate = "Q_DECLARE_METATYPE($full_list_type$)\n";
  226. const char *Templates::DeclareComplexQmlListTypeTemplate = "Q_DECLARE_METATYPE(QQmlListProperty<$full_type$>)\n";
  227. const char *Templates::DeclareMetaTypeMapTemplate = "#ifndef Q_PROTOBUF_MAP_$key_type_underscore$_$value_type_underscore$\n"
  228. "#define Q_PROTOBUF_MAP_$key_type_underscore$_$value_type_underscore$\n"
  229. "Q_DECLARE_METATYPE($full_type$)\n"
  230. "#endif\n";
  231. const char *Templates::RegisterLocalEnumTemplate = "qRegisterProtobufEnumType<$scope_type$>();\n"
  232. "qRegisterMetaType<$scope_type$>(\"$type$\");\n"
  233. "qRegisterMetaType<$scope_type$>(\"$full_type$\");\n"
  234. "qRegisterMetaType<$scope_list_type$>(\"$full_list_type$\");\n";
  235. const char *Templates::RegisterMapTemplate = "qRegisterMetaType<$scope_type$>(\"$full_type$\");\n"
  236. "qRegisterMetaType<$scope_type$>(\"$full_list_type$\");\n"
  237. "qRegisterProtobufMapType<$key_type$, $value_type$>();\n";
  238. const char *Templates::RegisterMetaTypeTemplateNoNamespace = "qRegisterMetaType<$namespaces$::$type$>(\"$type$\");\n";
  239. const char *Templates::RegisterMetaTypeTemplate = "qRegisterMetaType<$namespaces$::$type$>(\"$namespaces$::$type$\");\n";
  240. const char *Templates::RegisterGlobalEnumMetaTypeTemplate = "qRegisterMetaType<$full_type$>(\"$full_type$\");\n";
  241. const char *Templates::RegisterQmlListPropertyMetaTypeTemplate = "qRegisterMetaType<QQmlListProperty<$full_type$>>(\"QQmlListProperty<$full_type$>\");\n";
  242. const char *Templates::QEnumTemplate = "Q_ENUM($type$)\n";
  243. const char *Templates::ClassDefinitionTemplate = "\nclass $classname$ : public $parent_class$\n"
  244. "{\n";
  245. const char *Templates::QObjectMacro = "Q_OBJECT";
  246. const char *Templates::ClientMethodDeclarationSyncTemplate = "QGrpcStatus $method_name$(const $param_type$ &$param_name$, const QPointer<$return_type$> &$return_name$);\n";
  247. const char *Templates::ClientMethodDeclarationAsyncTemplate = "QGrpcCallReplyShared $method_name$(const $param_type$ &$param_name$);\n";
  248. const char *Templates::ClientMethodDeclarationAsync2Template = "Q_INVOKABLE void $method_name$(const $param_type$ &$param_name$, const QObject *context, const std::function<void(QGrpcCallReplyShared)> &callback);\n";
  249. const char *Templates::ClientMethodDeclarationQmlTemplate = "Q_INVOKABLE void $method_name$($param_type$ *$param_name$, const QJSValue &callback, const QJSValue &errorCallback);\n";
  250. const char *Templates::ClientMethodDeclarationQml2Template = "Q_INVOKABLE void $method_name$($param_type$ *$param_name$, $return_type$ *$return_name$, const QJSValue &errorCallback);\n";
  251. const char *Templates::ServerMethodDeclarationTemplate = "Q_INVOKABLE virtual $return_type$ $method_name$(const $param_type$ &$param_name$) = 0;\n";
  252. const char *Templates::ClientConstructorDefinitionTemplate = "\n$classname$::$classname$(QObject *parent) : $parent_class$(\"$service_name$\", parent)\n"
  253. "{\n"
  254. "}\n";
  255. const char *Templates::ClientMethodDefinitionSyncTemplate = "\nQGrpcStatus $classname$::$method_name$(const $param_type$ &$param_name$, const QPointer<$return_type$> &$return_name$)\n"
  256. "{\n"
  257. " return call(\"$method_name$\", $param_name$, $return_name$);\n"
  258. "}\n";
  259. const char *Templates::ClientMethodDefinitionAsyncTemplate = "\nQGrpcCallReplyShared $classname$::$method_name$(const $param_type$ &$param_name$)\n"
  260. "{\n"
  261. " return call(\"$method_name$\", $param_name$);\n"
  262. "}\n";
  263. const char *Templates::ClientMethodDefinitionAsync2Template = "\nvoid $classname$::$method_name$(const $param_type$ &$param_name$, const QObject *context, const std::function<void(QGrpcCallReplyShared)> &callback)\n"
  264. "{\n"
  265. " QGrpcCallReplyShared reply = call(\"$method_name$\", $param_name$);\n"
  266. " QObject::connect(reply.get(), &QGrpcCallReply::finished, context, [reply, callback]() {\n"
  267. " callback(reply);\n"
  268. " });\n"
  269. "}\n";
  270. const char *Templates::ClientMethodDefinitionQmlTemplate = "\nvoid $classname$::$method_name$($param_type$ *$param_name$, const QJSValue &callback, const QJSValue &errorCallback)\n"
  271. "{\n"
  272. " if (!callback.isCallable()) {\n"
  273. " qWarning() << \"Unable to call $classname$::$method_name$, callback is not callable\";\n"
  274. " return;\n"
  275. " }\n\n"
  276. " if (arg == nullptr) {\n"
  277. " qWarning() << \"Invalid argument provided for method $classname$::$method_name$, argument of type '$param_type$ *' expected\";\n"
  278. " return;\n"
  279. " }\n\n"
  280. " QJSEngine *jsEngine = qjsEngine(this);\n"
  281. " if (jsEngine == nullptr) {\n"
  282. " qWarning() << \"Unable to call $classname$::$method_name$, it's only callable from JS engine context\";\n"
  283. " return;\n"
  284. " }\n\n"
  285. " QGrpcCallReplyShared reply = call(\"$method_name$\", *$param_name$);\n"
  286. " reply->subscribe(jsEngine, [this, reply, callback, jsEngine]() {\n"
  287. " auto result = new $return_type$(reply->read<$return_type$>());\n"
  288. " qmlEngine(this)->setObjectOwnership(result, QQmlEngine::JavaScriptOwnership);\n"
  289. " QJSValue(callback).call(QJSValueList{jsEngine->toScriptValue(result)});\n"
  290. " }, [errorCallback, jsEngine](const QGrpcStatus &status) {\n"
  291. " QJSValue(errorCallback).call(QJSValueList{jsEngine->toScriptValue(status)});\n"
  292. " });\n"
  293. "}\n";
  294. const char *Templates::ClientMethodDefinitionQml2Template = "\nvoid $classname$::$method_name$($param_type$ *$param_name$, $return_type$ *$return_name$, const QJSValue &errorCallback)\n"
  295. "{\n"
  296. " if ($return_name$ == nullptr) {\n"
  297. " qWarning() << \"Invalid argument provided for method $classname$::$method_name$, argument of type '$return_type$ *' expected\";\n"
  298. " return;\n"
  299. " }\n\n"
  300. " QPointer<$return_type$> safeReturn($return_name$);\n\n"
  301. " if ($param_name$ == nullptr) {\n"
  302. " qWarning() << \"Invalid argument provided for method $classname$::$method_name$, argument of type '$param_type$ *' expected\";\n"
  303. " return;\n"
  304. " }\n\n"
  305. " QJSEngine *jsEngine = qjsEngine(this);\n"
  306. " if (jsEngine == nullptr) {\n"
  307. " qWarning() << \"Unable to call $classname$::$method_name$, it's only callable from JS engine context\";\n"
  308. " return;\n"
  309. " }\n\n"
  310. " QGrpcCallReplyShared reply = call(\"$method_name$\", *$param_name$);\n"
  311. " reply->subscribe(jsEngine, [this, reply, jsEngine, safeReturn]() {\n"
  312. " if (safeReturn.isNull()) {\n"
  313. " qWarning() << \"Return value is destroyed. Ignore call result\";\n"
  314. " return;\n"
  315. " }\n"
  316. " *safeReturn = $return_type$(reply->read<$return_type$>());\n"
  317. " }, [errorCallback, jsEngine](const QGrpcStatus &status) {\n"
  318. " QJSValue(errorCallback).call(QJSValueList{jsEngine->toScriptValue(status)});\n"
  319. " });\n"
  320. "}\n";
  321. const char *Templates::RegisterSerializersTemplate = "qRegisterProtobufType<$classname$>();\n";
  322. const char *Templates::RegisterEnumSerializersTemplate = "qRegisterProtobufEnumType<$full_type$>();\n";
  323. const char *Templates::RegistrarTemplate = "static QtProtobuf::ProtoTypeRegistrar<$classname$> ProtoTypeRegistrar$classname$(qRegisterProtobufType<$classname$>);\n";
  324. const char *Templates::EnumRegistrarTemplate = "static QtProtobuf::ProtoTypeRegistrar<$enum_gadget$> ProtoTypeRegistrar$enum_gadget$($enum_gadget$::registerTypes);\n";
  325. const char *Templates::QmlRegisterTypeTemplate = "qmlRegisterType<$scope_type$>(\"$qml_package$\", 1, 0, \"$type$\");\n";
  326. const char *Templates::QmlRegisterEnumTypeTemplate = "qmlRegisterUncreatableType<$enum_gadget$>(\"$qml_package$\", 1, 0, \"$type$\", \"$full_type$ Could not be created from qml context\");\n";
  327. const char *Templates::ClientMethodSignalDeclarationTemplate = "Q_SIGNAL void $method_name$Updated(const $return_type$ &);\n";
  328. const char *Templates::ClientMethodServerStreamDeclarationTemplate = "QGrpcStreamShared stream$method_name_upper$(const $param_type$ &$param_name$);\n";
  329. const char *Templates::ClientMethodServerStream2DeclarationTemplate = "QGrpcStreamShared stream$method_name_upper$(const $param_type$ &$param_name$, const QPointer<$return_type$> &$return_name$);\n";
  330. const char *Templates::ClientMethodServerStreamQmlDeclarationTemplate = "Q_INVOKABLE QGrpcStreamShared qmlStream$method_name_upper$_p($param_type$ *$param_name$, $return_type$ *$return_name$);\n";
  331. const char *Templates::ClientMethodServerStreamDefinitionTemplate = "QGrpcStreamShared $classname$::stream$method_name_upper$(const $param_type$ &$param_name$)\n"
  332. "{\n"
  333. " return stream(\"$method_name$\", $param_name$);\n"
  334. "}\n";
  335. const char *Templates::ClientMethodServerStream2DefinitionTemplate = "QGrpcStreamShared $classname$::stream$method_name_upper$(const $param_type$ &$param_name$, const QPointer<$return_type$> &$return_name$)\n"
  336. "{\n"
  337. " return stream(\"$method_name$\", $param_name$, $return_name$);\n"
  338. "}\n";
  339. const char *Templates::ClientMethodServerStreamQmlDefinitionTemplate = "QGrpcStreamShared $classname$::qmlStream$method_name_upper$_p($param_type$ *$param_name$, $return_type$ *$return_name$)\n"
  340. "{\n"
  341. " return stream(\"$method_name$\", *$param_name$, QPointer<$return_type$>($return_name$));\n"
  342. "}\n";
  343. const char *Templates::ListSuffix = "Repeated";
  344. const std::unordered_map<::google::protobuf::FieldDescriptor::Type, std::string> Templates::TypeReflection = {
  345. {::google::protobuf::FieldDescriptor::TYPE_DOUBLE, "double"},
  346. {::google::protobuf::FieldDescriptor::TYPE_FLOAT, "float"},
  347. {::google::protobuf::FieldDescriptor::TYPE_INT64, "int64"}, //Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
  348. {::google::protobuf::FieldDescriptor::TYPE_UINT64,"uint64"}, //Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
  349. {::google::protobuf::FieldDescriptor::TYPE_INT32, "int32"},
  350. {::google::protobuf::FieldDescriptor::TYPE_FIXED64, "fixed64"}, //Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
  351. {::google::protobuf::FieldDescriptor::TYPE_FIXED32, "fixed32"},
  352. {::google::protobuf::FieldDescriptor::TYPE_BOOL, "bool"},
  353. {::google::protobuf::FieldDescriptor::TYPE_STRING, "QString"},
  354. {::google::protobuf::FieldDescriptor::TYPE_BYTES, "QByteArray"},
  355. {::google::protobuf::FieldDescriptor::TYPE_UINT32, "uint32"}, //Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
  356. {::google::protobuf::FieldDescriptor::TYPE_SFIXED32, "sfixed32"},
  357. {::google::protobuf::FieldDescriptor::TYPE_SFIXED64, "sfixed64"}, //Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
  358. {::google::protobuf::FieldDescriptor::TYPE_SINT32, "sint32"},
  359. {::google::protobuf::FieldDescriptor::TYPE_SINT64, "sint64"} //Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
  360. };
  361. const char *Templates::ProtoFileSuffix = ".qpb";
  362. const char *Templates::GrpcFileSuffix = "_grpc";
  363. const char *Templates::EnumClassSuffix = "Gadget";
  364. const char *Templates::QtProtobufNamespace = "QtProtobuf";
  365. const char *Templates::QtProtobufNestedNamespace = "_QtProtobufNested";
  366. const char *Templates::QtProtobufFieldEnum = "QtProtobufFieldEnum";
  367. const char *Templates::FieldEnumTemplate = "enum QtProtobufFieldEnum {\n";
  368. const char *Templates::FieldNumberTemplate = "$property_name_cap$ProtoFieldNumber = $number$,\n";