templates.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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::DefaultProtobufIncludesTemplate = "#include <QMetaType>\n"
  28. "#include <QList>\n"
  29. "#include <protobufobject.h>\n"
  30. "#include <unordered_map>\n\n";
  31. const char *Templates::GlobalEnumClassNameTemplate = "GlobalEnums";
  32. const char *Templates::PreambleTemplate = "/* This file is autogenerated. DO NOT CHANGE. All changes will be lost */\n\n"
  33. "#pragma once\n\n"
  34. "#include <QObject>\n";
  35. const char *Templates::InternalIncludeTemplate = "#include \"$include$.h\"\n";
  36. const char *Templates::ExternalIncludeTemplate = "#include <$include$>\n";
  37. const char *Templates::GlobalEnumIncludeTemplate = "#include <globalenums.h>\n";
  38. const char *Templates::UsingQtProtobufNamespaceTemplate = "\nusing namespace qtprotobuf;\n";
  39. const char *Templates::ComplexTypeRegistrationMethodTemplate = "static void registerTypes();\n";
  40. const char *Templates::ComplexTypeRegistrationTemplate = "void $classname$::registerTypes()\n{\n"
  41. " static bool registationDone = false;\n"
  42. " if (!registationDone) {\n\n"
  43. " qRegisterMetaType<$classname$>(\"$classname$\");\n"
  44. " qRegisterMetaType<$classname$List>(\"$classname$List\");\n"
  45. " qRegisterMetaType<$classname$>(\"$namespaces$::$classname$\");\n"
  46. " qRegisterMetaType<$classname$List>(\"$namespaces$::$classname$List\");\n"
  47. " registerSerializers();\n";
  48. const char *Templates::ComplexListTypeUsingTemplate = "using $classname$List = QList<$classname$>;\n";
  49. const char *Templates::MapTypeUsingTemplate = "using $classname$ = QMap<$key$, $value$>;\n";
  50. const char *Templates::EnumTypeUsingTemplate = "using $enum$List = QList<$enum$>;\n";
  51. const char *Templates::NamespaceTemplate = "\nnamespace $namespace$ {\n";
  52. const char *Templates::UsingNamespaceTemplate = "using namespace $namespace$;\n";
  53. const char *Templates::NonProtoClassDefinitionTemplate = "\nclass $classname$ : public QObject\n"
  54. "{\n"
  55. " Q_OBJECT\n";
  56. const char *Templates::ProtoClassDefinitionTemplate = "\nclass $classname$ final : public qtprotobuf::ProtobufObject<$classname$>\n"
  57. "{\n"
  58. " Q_OBJECT\n";
  59. const char *Templates::PropertyTemplate = "Q_PROPERTY($type$ $property_name$ READ $property_name$ WRITE set$property_name_cap$ NOTIFY $property_name$Changed)\n";
  60. const char *Templates::MessagePropertyTemplate = "Q_PROPERTY($type$ $property_name$ READ $property_name$ WRITE set$property_name_cap$ NOTIFY $property_name$Changed)\n";
  61. const char *Templates::MemberTemplate = "$type$ m_$property_name$;\n";
  62. const char *Templates::EnumMemberTemplate = "::$type$ m_$property_name$;\n";
  63. const char *Templates::PublicBlockTemplate = "\npublic:\n";
  64. const char *Templates::PrivateBlockTemplate = "\nprivate:\n";
  65. const char *Templates::EnumDefinitionTemplate = "enum $enum$ {\n";
  66. const char *Templates::EnumFieldTemplate = "$enumvalue$ = $value$,\n";
  67. const char *Templates::ProtoConstructorTemplate = "$classname$($parameter_list$QObject *parent = nullptr) : ProtobufObject(parent)";
  68. const char *Templates::ConstructorTemplate = "$classname$();\n";
  69. const char *Templates::CopyConstructorTemplate = "$classname$(const $classname$ &other) : ProtobufObject() {\n";
  70. const char *Templates::MoveConstructorTemplate = "$classname$($classname$ &&other) : ProtobufObject() {\n";
  71. const char *Templates::CopyFieldTemplate = "m_$property_name$ = other.m_$property_name$;\n";
  72. const char *Templates::MoveComplexFieldTemplate = "m_$property_name$ = std::move(other.m_$property_name$);\n";
  73. const char *Templates::MoveFieldTemplate = "m_$property_name$ = std::exchange(other.m_$property_name$, 0);\n";
  74. const char *Templates::EnumMoveFieldTemplate = "m_$property_name$ = other.m_$property_name$;\n";
  75. const char *Templates::AssignmentOperatorTemplate = "$classname$ &operator =(const $classname$ &other) {\n";
  76. const char *Templates::AssignmentOperatorReturnTemplate = "return *this;\n";
  77. const char *Templates::MoveAssignmentOperatorTemplate = "$classname$ &operator =($classname$ &&other) {\n";
  78. const char *Templates::EqualOperatorTemplate = "bool operator ==(const $type$ &other) const {\n"
  79. " return ";
  80. const char *Templates::EqualOperatorPropertyTemplate = "m_$property_name$ == other.m_$property_name$";
  81. const char *Templates::NotEqualOperatorTemplate = "bool operator !=(const $type$ &other) const {\n"
  82. " return !this->operator ==(other);\n"
  83. "}\n\n";
  84. const char *Templates::GetterTemplate = "$type$ $property_name$() const {\n"
  85. " return m_$property_name$;\n"
  86. "}\n\n";
  87. const char *Templates::SetterTemplateSimpleType = "void set$property_name_cap$($type$ $property_name$) {\n"
  88. " if (m_$property_name$ != $property_name$) {\n"
  89. " m_$property_name$ = $property_name$;\n"
  90. " $property_name$Changed();\n"
  91. " }\n"
  92. "}\n\n";
  93. const char *Templates::SetterTemplateComplexType = "void set$property_name_cap$(const $type$ &$property_name$) {\n"
  94. " if (m_$property_name$ != $property_name$) {\n"
  95. " m_$property_name$ = $property_name$;\n"
  96. " $property_name$Changed();\n"
  97. " }\n"
  98. "}\n\n";
  99. const char *Templates::SignalsBlockTemplate = "\nsignals:\n";
  100. const char *Templates::SignalTemplate = "void $property_name$Changed();\n";
  101. const char *Templates::FieldsOrderingDefinitionContainerTemplate = "static const std::unordered_map<int/*field number*/, int/*property number*/> propertyOrdering;\n";
  102. const char *Templates::FieldsOrderingContainerTemplate = "const std::unordered_map<int, int> $type$::propertyOrdering = {";
  103. const char *Templates::FieldOrderTemplate = "{$field_number$,$property_number$}";
  104. const char *Templates::EnumTemplate = "$type$";
  105. const char *Templates::SimpleBlockEnclosureTemplate = "}\n\n";
  106. const char *Templates::SemicolonBlockEnclosureTemplate = "};\n";
  107. const char *Templates::EmptyBlockTemplate = "{}\n\n";
  108. const char *Templates::PropertyInitializerTemplate = "\n ,m_$property_name$($property_name$)";
  109. const char *Templates::ConstructorContentTemplate = "\n{\n registerTypes();\n}\n";
  110. const char *Templates::DeclareMetaTypeTemplate = "Q_DECLARE_METATYPE($namespaces$::$classname$)\n";
  111. const char *Templates::DeclareComplexListTypeTemplate = "Q_DECLARE_METATYPE($namespaces$::$classname$List)\n";
  112. const char *Templates::RegisterMetaTypeDefaultTemplate = "qRegisterMetaType<$namespaces$::$type$>();\n";
  113. const char *Templates::RegisterMetaTypeTemplateNoNamespace = "qRegisterMetaType<$namespaces$::$type$>(\"$type$\");\n";
  114. const char *Templates::RegisterMetaTypeTemplate = "qRegisterMetaType<$namespaces$::$type$>(\"$namespaces$::$type$\");\n";
  115. const char *Templates::QEnumTemplate = "Q_ENUM($type$)\n";
  116. const char *Templates::MapSerializationRegisterTemplate = "qtprotobuf::ProtobufObjectPrivate::wrapSerializer<$classname$::$type$>(\n"
  117. "qtprotobuf::ProtobufObjectPrivate::serializeMap<$classname$::$type$::key_type, $classname$::$type$::mapped_type>,\n"
  118. "qtprotobuf::ProtobufObjectPrivate::deserializeMap<$classname$::$type$::key_type, $classname$::$type$::mapped_type>\n"
  119. ", qtprotobuf::LengthDelimited);\n";
  120. const char *Templates::ClassDefinitionTemplate = "\nclass $classname$ : public $parent_class$\n"
  121. "{\n";
  122. const char *Templates::ClientMethodDeclarationSyncTemplate = "Q_INVOKABLE bool $method_name$(const $param_type$ &$param_name$, $return_type$ &$return_name$);\n";
  123. const char *Templates::ClientMethodDeclarationAsyncTemplate = "Q_INVOKABLE bool $method_name$(const $param_type$ &$param_name$, const qtprotobuf::AsyncReply<$return_type$> &reply);\n";
  124. const char *Templates::ServerMethodDeclarationTemplate = "Q_INVOKABLE virtual $return_type$ $method_name$(const $param_type$ &$param_name$) = 0;\n";
  125. const char *Templates::ConstructorDefinitionSyncTemplate = "\n$classname$::$classname$() : $parent_class$(\"$service_name$\")\n"
  126. "{\n"
  127. "}\n";
  128. const char *Templates::ClientMethodDefinitionSyncTemplate = "\nbool $classname$::$method_name$(const $param_type$ &$param_name$, $return_type$ &$return_name$)\n"
  129. "{\n"
  130. " return call(\"$method_name$\", $param_name$, $return_name$);\n"
  131. "}\n";
  132. const char *Templates::ClientMethodDefinitionAsyncTemplate = "\nbool $classname$::$method_name$(const $param_type$ &$param_name$, const qtprotobuf::AsyncReply<$return_type$> &reply)\n"
  133. "{\n"
  134. " //TODO: call transport method to serialize this method\n"
  135. " return false;\n"
  136. "}\n";
  137. const std::unordered_map<::google::protobuf::FieldDescriptor::Type, std::string> Templates::TypeReflection = {
  138. {::google::protobuf::FieldDescriptor::TYPE_DOUBLE, "double"},
  139. {::google::protobuf::FieldDescriptor::TYPE_FLOAT, "float"},
  140. {::google::protobuf::FieldDescriptor::TYPE_INT64, "int64"}, //Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
  141. {::google::protobuf::FieldDescriptor::TYPE_UINT64,"uint64"}, //Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
  142. {::google::protobuf::FieldDescriptor::TYPE_INT32, "int32"},
  143. {::google::protobuf::FieldDescriptor::TYPE_FIXED64, "fint64"}, //Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
  144. {::google::protobuf::FieldDescriptor::TYPE_FIXED32, "fint32"},
  145. {::google::protobuf::FieldDescriptor::TYPE_BOOL, "bool"},
  146. {::google::protobuf::FieldDescriptor::TYPE_STRING, "QString"},
  147. {::google::protobuf::FieldDescriptor::TYPE_BYTES, "QByteArray"},
  148. {::google::protobuf::FieldDescriptor::TYPE_UINT32, "uint32"}, //Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
  149. {::google::protobuf::FieldDescriptor::TYPE_SFIXED32, "sfint32"},
  150. {::google::protobuf::FieldDescriptor::TYPE_SFIXED64, "sfint64"}, //Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
  151. {::google::protobuf::FieldDescriptor::TYPE_SINT32, "sint32"},
  152. {::google::protobuf::FieldDescriptor::TYPE_SINT64, "sint64"} //Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
  153. };