2022-10-05 17:11:31 +00:00
|
|
|
// Copyright (C) 2022 The Qt Company Ltd.
|
|
|
|
// Copyright (C) 2019 Alexey Edelev <semlanik@gmail.com>
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
|
|
|
|
|
|
|
#ifndef GENERATOROPTIONS_H
|
|
|
|
#define GENERATOROPTIONS_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2022-11-21 17:31:49 +00:00
|
|
|
namespace qtprotoccommon {
|
2022-10-05 17:11:31 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\ingroup generator
|
|
|
|
\private
|
|
|
|
\brief The GeneratorOptions class
|
|
|
|
*/
|
|
|
|
class Options
|
|
|
|
{
|
|
|
|
Options();
|
|
|
|
|
|
|
|
static Options &mutableInstance();
|
|
|
|
public:
|
2024-09-08 11:10:37 +00:00
|
|
|
enum class HeaderGuardType {
|
|
|
|
Pragma,
|
|
|
|
ProtoFilename,
|
|
|
|
};
|
2023-04-24 12:01:36 +00:00
|
|
|
enum GeneratorType {
|
|
|
|
QtProtobufGen = 0,
|
|
|
|
QtGrpcGen
|
|
|
|
};
|
2022-10-05 17:11:31 +00:00
|
|
|
~Options();
|
|
|
|
Options(const Options &) = delete;
|
|
|
|
Options &operator=(const Options &) = delete;
|
|
|
|
Options(Options &&) = delete;
|
|
|
|
Options &operator=(Options &&) = delete;
|
|
|
|
|
|
|
|
static const Options &instance();
|
2025-03-27 18:51:55 +00:00
|
|
|
static void setFromString(const std::string &options, GeneratorType type, std::string *error);
|
2022-10-05 17:11:31 +00:00
|
|
|
|
2024-09-03 16:01:37 +00:00
|
|
|
bool hasQml() const { return m_qml; }
|
2022-10-05 17:11:31 +00:00
|
|
|
bool generateComments() const { return m_generateComments; }
|
|
|
|
bool isFolder() const { return m_isFolder; }
|
|
|
|
bool generateFieldEnum() const { return m_generateFieldEnum; }
|
|
|
|
const std::string &extraNamespace() const { return m_extraNamespace; }
|
|
|
|
const std::string &exportMacro() const { return m_exportMacro; }
|
2024-02-03 10:25:33 +00:00
|
|
|
const std::string &exportMacroFilename() const { return m_exportMacroFilename; }
|
|
|
|
bool generateMacroExportFile() const { return m_generateMacroExportFile; }
|
2024-09-08 11:10:37 +00:00
|
|
|
HeaderGuardType headerGuard() const { return m_headerGuard; }
|
Long live mutable getters
The functionality is desirable by some users, we re-enable it.
New API addresses the core issue that is partially related to
QTBUG-119912, but has wider concequences in blind use in user
projects. We add the 'mut' prefix to all mutable getters to make
the mutable access explicit. Overload approach leads to unwanted
detaches not only whe is used be moc-generated code, but also
in user projects if developers do not pay enough attention to
const modifiers of their variables/references. We declined to
restore it, dispite it was the better looking API, in favor to
the code safety.
This also reveals the code clashing scenario, when the overload
might happen if the message has both 'a' and 'mutA' fields in
its definition. This scenario is kindly forbidden by our generator,
and sanitized at very early stages. We expect that it won't happen
in user projects, but even if it will, the solution is to rename
the field when generating Qt code. The serialization/deserialization
do not depend on field naming directly. json_name attribute also
will help to workaround this.
The undocumented ALLOW_MUTABLE_GETTER_CONFLICTS option allows clashing
the mutable getter names, but its usage currently limited by our
internal code only. The reason unfixed QTBUG-119912 issue in moc.
Once the issue is fixed, the moc version check should get the proper
version, but not '99' as for now and the ALLOW_MUTABLE_GETTER_CONFLICTS
will become public.
Another design solution is the use of overloaded functions that
return pointers, like it's done it the reference protobuf. But this
kind of API leaves the pointer ownership undefined and decided to
not be used.
[ChangeLog][Protobuf] The generated messages now have the mutable
getters for the fiels of the message type. The getters have 'mut'
prefix and implicily allocate the respective fields if needed, so the
use of intermediate message objects is not required.
Task-number: QTBUG-119913
Change-Id: I09b9ee37e1fbbe37b9c3cb501e92442da8ad3e4b
Reviewed-by: Dennis Oberst <dennis.oberst@qt.io>
2025-03-14 11:40:54 +00:00
|
|
|
bool mutableGetterConflicts() const { return m_mutableGetterConflicts; }
|
2022-10-05 17:11:31 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_generateComments;
|
|
|
|
bool m_isFolder;
|
|
|
|
bool m_generateFieldEnum;
|
|
|
|
std::string m_extraNamespace;
|
|
|
|
std::string m_exportMacro;
|
2024-02-03 10:25:33 +00:00
|
|
|
std::string m_exportMacroFilename;
|
|
|
|
bool m_generateMacroExportFile;
|
2023-04-24 12:01:36 +00:00
|
|
|
bool m_qml;
|
Long live mutable getters
The functionality is desirable by some users, we re-enable it.
New API addresses the core issue that is partially related to
QTBUG-119912, but has wider concequences in blind use in user
projects. We add the 'mut' prefix to all mutable getters to make
the mutable access explicit. Overload approach leads to unwanted
detaches not only whe is used be moc-generated code, but also
in user projects if developers do not pay enough attention to
const modifiers of their variables/references. We declined to
restore it, dispite it was the better looking API, in favor to
the code safety.
This also reveals the code clashing scenario, when the overload
might happen if the message has both 'a' and 'mutA' fields in
its definition. This scenario is kindly forbidden by our generator,
and sanitized at very early stages. We expect that it won't happen
in user projects, but even if it will, the solution is to rename
the field when generating Qt code. The serialization/deserialization
do not depend on field naming directly. json_name attribute also
will help to workaround this.
The undocumented ALLOW_MUTABLE_GETTER_CONFLICTS option allows clashing
the mutable getter names, but its usage currently limited by our
internal code only. The reason unfixed QTBUG-119912 issue in moc.
Once the issue is fixed, the moc version check should get the proper
version, but not '99' as for now and the ALLOW_MUTABLE_GETTER_CONFLICTS
will become public.
Another design solution is the use of overloaded functions that
return pointers, like it's done it the reference protobuf. But this
kind of API leaves the pointer ownership undefined and decided to
not be used.
[ChangeLog][Protobuf] The generated messages now have the mutable
getters for the fiels of the message type. The getters have 'mut'
prefix and implicily allocate the respective fields if needed, so the
use of intermediate message objects is not required.
Task-number: QTBUG-119913
Change-Id: I09b9ee37e1fbbe37b9c3cb501e92442da8ad3e4b
Reviewed-by: Dennis Oberst <dennis.oberst@qt.io>
2025-03-14 11:40:54 +00:00
|
|
|
bool m_mutableGetterConflicts;
|
|
|
|
|
2024-09-08 11:10:37 +00:00
|
|
|
HeaderGuardType m_headerGuard = Options::HeaderGuardType::ProtoFilename;
|
2022-10-05 17:11:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // GENERATOROPTIONS_H
|