2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 14:38:15 +00:00

Use getters in MTP classes.

This commit is contained in:
John Preston
2019-07-05 15:38:38 +02:00
parent 3ca28c0cf9
commit 567bf60644
100 changed files with 2807 additions and 2672 deletions

View File

@@ -97,7 +97,7 @@ with open(input_file) as f:
layerline = re.match(r'// LAYER (\d+)', line)
if (layerline):
layerIndex = int(layerline.group(1));
layer = 'constexpr auto CurrentLayer = mtpPrime(' + str(layerIndex) + ');';
layer = 'inline constexpr mtpPrime CurrentLayer = mtpPrime(' + str(layerIndex) + ');';
else:
lines.append(line);
@@ -336,37 +336,38 @@ for line in lines:
funcsText += '\t\tMAX_FIELD = (1U << ' + str(maxbit) + '),\n';
funcsText += '\t};\n';
funcsText += '\tusing Flags = base::flags<Flag>;\n';
funcsText += '\tfriend inline constexpr auto is_flag_type(Flag) { return true; };\n';
funcsText += '\tfriend inline constexpr bool is_flag_type(Flag) { return true; };\n';
funcsText += '\n';
if (len(conditions)):
for paramName in conditionsList:
if (paramName in trivialConditions):
funcsText += '\tbool is_' + paramName + '() const { return v' + hasFlags + '.v & Flag::f_' + paramName + '; }\n';
else:
funcsText += '\tbool has_' + paramName + '() const { return v' + hasFlags + '.v & Flag::f_' + paramName + '; }\n';
funcsText += '\n';
if (len(prms) > len(trivialConditions)):
for paramName in prmsList:
if (paramName in trivialConditions):
continue;
paramType = prms[paramName];
prmsInit.append('v' + paramName + '(_' + paramName + ')');
prmsNames.append('_' + paramName);
prmsInit.append('_' + paramName + '(' + paramName + '_)');
prmsNames.append(paramName + '_');
if (paramName == isTemplate):
ptypeFull = paramType;
else:
ptypeFull = 'MTP' + paramType;
funcsText += '\t' + ptypeFull + ' v' + paramName + ';\n';
if (paramType in ['int', 'Int', 'bool', 'Bool', 'flags<Flags>']):
prmsStr.append(ptypeFull + ' _' + paramName);
prmsStr.append(ptypeFull + ' ' + paramName + '_');
else:
prmsStr.append('const ' + ptypeFull + ' &_' + paramName);
funcsText += '\n';
prmsStr.append('const ' + ptypeFull + ' &' + paramName + '_');
funcsText += '\tMTP' + name + '() = default;\n'; # constructor
funcsText += '\tMTP' + name + '();\n';# = default; # constructor
if (isTemplate != ''):
methodBodies += 'template <typename TQueryType>\n'
methodBodies += 'MTP' + name + '<TQueryType>::MTP' + name + '() = default;\n';
else:
methodBodies += 'MTP' + name + '::MTP' + name + '() = default;\n';
if (len(prms) > len(trivialConditions)):
funcsText += '\tMTP' + name + '(' + ', '.join(prmsStr) + ') : ' + ', '.join(prmsInit) + ' {\n\t}\n';
funcsText += '\tMTP' + name + '(' + ', '.join(prmsStr) + ');\n';
if (isTemplate != ''):
methodBodies += 'template <typename TQueryType>\n'
methodBodies += 'MTP' + name + '<TQueryType>::MTP' + name + '(' + ', '.join(prmsStr) + ') : ' + ', '.join(prmsInit) + ' {\n}\n';
else:
methodBodies += 'MTP' + name + '::MTP' + name + '(' + ', '.join(prmsStr) + ') : ' + ', '.join(prmsInit) + ' {\n}\n';
funcsText += '\n';
funcsText += '\tuint32 innerLength() const;\n'; # count size
@@ -380,9 +381,9 @@ for line in lines:
v = prms[k];
if (k in conditionsList):
if (not k in trivialConditions):
size.append('(has_' + k + '() ? v' + k + '.innerLength() : 0)');
size.append('((_' + hasFlags + '.v & Flag::f_' + k + ') ? _' + k + '.innerLength() : 0)');
else:
size.append('v' + k + '.innerLength()');
size.append('_' + k + '.innerLength()');
if (not len(size)):
size.append('0');
methodBodies += '\treturn ' + ' + '.join(size) + ';\n';
@@ -400,9 +401,9 @@ for line in lines:
v = prms[k];
if (k in conditionsList):
if (not k in trivialConditions):
methodBodies += '\tif (has_' + k + '()) { v' + k + '.read(from, end); } else { v' + k + ' = MTP' + v + '(); }\n';
methodBodies += '\tif (_' + hasFlags + '.v & Flag::f_' + k + ') { _' + k + '.read(from, end); } else { _' + k + ' = MTP' + v + '(); }\n';
else:
methodBodies += '\tv' + k + '.read(from, end);\n';
methodBodies += '\t_' + k + '.read(from, end);\n';
methodBodies += '}\n';
funcsText += '\tvoid write(mtpBuffer &to) const;\n'; # write method
@@ -415,18 +416,31 @@ for line in lines:
v = prms[k];
if (k in conditionsList):
if (not k in trivialConditions):
methodBodies += '\tif (has_' + k + '()) v' + k + '.write(to);\n';
methodBodies += '\tif (_' + hasFlags + '.v & Flag::f_' + k + ') _' + k + '.write(to);\n';
else:
methodBodies += '\tv' + k + '.write(to);\n';
methodBodies += '\t_' + k + '.write(to);\n';
methodBodies += '}\n';
if (isTemplate != ''):
funcsText += '\n\tusing ResponseType = typename TQueryType::ResponseType;\n';
funcsText += '\n\tusing ResponseType = typename TQueryType::ResponseType;\n\n';
inlineMethods += methodBodies;
else:
funcsText += '\n\tusing ResponseType = MTP' + resType + ';\n'; # method return type
funcsText += '\n\tusing ResponseType = MTP' + resType + ';\n\n'; # method return type
methods += methodBodies;
if (len(prms) > len(trivialConditions)):
funcsText += 'private:\n';
for paramName in prmsList:
if (paramName in trivialConditions):
continue;
paramType = prms[paramName];
if (paramName == isTemplate):
ptypeFull = paramType;
else:
ptypeFull = 'MTP' + paramType;
funcsText += '\t' + ptypeFull + ' _' + paramName + ';\n';
funcsText += '\n';
funcsText += '};\n'; # class ending
if (isTemplate != ''):
funcsText += 'template <typename TQueryType>\n';
@@ -630,25 +644,27 @@ for restype in typesList:
dataText += '\t\tMAX_FIELD = (1U << ' + str(maxbit) + '),\n';
dataText += '\t};\n';
dataText += '\tusing Flags = base::flags<Flag>;\n';
dataText += '\tfriend inline constexpr auto is_flag_type(Flag) { return true; };\n';
dataText += '\tfriend inline constexpr bool is_flag_type(Flag) { return true; };\n';
dataText += '\n';
if (len(conditions)):
for paramName in conditionsList:
if (paramName in trivialConditions):
dataText += '\tbool is_' + paramName + '() const { return v' + hasFlags + '.v & Flag::f_' + paramName + '; }\n';
else:
dataText += '\tbool has_' + paramName + '() const { return v' + hasFlags + '.v & Flag::f_' + paramName + '; }\n';
dataText += '\t[[nodiscard]] bool is_' + paramName + '() const;\n';
constructsBodies += 'bool MTPD' + name + '::is_' + paramName + '() const {\n';
constructsBodies += '\treturn _' + hasFlags + '.v & Flag::f_' + paramName + ';\n';
constructsBodies += '}\n';
dataText += '\n';
switchLines += '\tcase mtpc_' + name + ': '; # for by-type-id type constructor
getters += '\tconst MTPD' + name + ' &c_' + name + '() const;\n'; # const getter
getters += '\t[[nodiscard]] const MTPD' + name + ' &c_' + name + '() const;\n'; # const getter
visitor += '\tcase mtpc_' + name + ': return base::match_method(c_' + name + '(), std::forward<Method>(method), std::forward<Methods>(methods)...);\n';
forwards += 'class MTPD' + name + ';\n'; # data class forward declaration
if (len(prms) > len(trivialConditions)):
dataText += '\tMTPD' + name + '() = default;\n'; # default constructor
dataText += '\tMTPD' + name + '();\n'; # default constructor
switchLines += 'setData(new MTPD' + name + '()); ';
constructsBodies += 'MTPD' + name + '::MTPD' + name + '() = default;\n';
constructsBodies += 'const MTPD' + name + ' &MTP' + restype + '::c_' + name + '() const {\n';
if (withType):
constructsBodies += '\tExpects(_type == mtpc_' + name + ');\n\n';
@@ -670,35 +686,59 @@ for restype in typesList:
paramType = prms[paramName];
if (paramType in ['int', 'Int', 'bool', 'Bool']):
prmsStr.append('MTP' + paramType + ' _' + paramName);
creatorParams.append('MTP' + paramType + ' _' + paramName);
prmsStr.append('MTP' + paramType + ' ' + paramName + '_');
creatorParams.append('MTP' + paramType + ' ' + paramName + '_');
else:
prmsStr.append('const MTP' + paramType + ' &_' + paramName);
creatorParams.append('const MTP' + paramType + ' &_' + paramName);
creatorParamsList.append('_' + paramName);
prmsInit.append('v' + paramName + '(_' + paramName + ')');
prmsStr.append('const MTP' + paramType + ' &' + paramName + '_');
creatorParams.append('const MTP' + paramType + ' &' + paramName + '_');
creatorParamsList.append(paramName + '_');
prmsInit.append('_' + paramName + '(' + paramName + '_)');
if (withType):
readText += '\t';
writeText += '\t';
if (paramName in conditions):
readText += '\tif (v->has_' + paramName + '()) { v->v' + paramName + '.read(from, end); } else { v->v' + paramName + ' = MTP' + paramType + '(); }\n';
writeText += '\tif (v.has_' + paramName + '()) v.v' + paramName + '.write(to);\n';
sizeList.append('(v.has_' + paramName + '() ? v.v' + paramName + '.innerLength() : 0)');
readText += '\tif (v' + paramName + '()) { _' + paramName + '.read(from, end); } else { _' + paramName + ' = MTP' + paramType + '(); }\n';
writeText += '\tif (const auto v' + paramName + ' = v.v' + paramName + '()) v' + paramName + '->write(to);\n';
sizeList.append('(v.v' + paramName + '() ? v.v' + paramName + '()->innerLength() : 0)');
else:
readText += '\tv->v' + paramName + '.read(from, end);\n';
writeText += '\tv.v' + paramName + '.write(to);\n';
sizeList.append('v.v' + paramName + '.innerLength()');
readText += '\t_' + paramName + '.read(from, end);\n';
writeText += '\tv.v' + paramName + '().write(to);\n';
sizeList.append('v.v' + paramName + '().innerLength()');
dataText += ', '.join(prmsStr) + ');\n';
constructsBodies += 'MTPD' + name + '::MTPD' + name + '(' + ', '.join(prmsStr) + ') : ' + ', '.join(prmsInit) + ' {\n}\n';
dataText += '\tMTPD' + name + '(const mtpPrime *&from, const mtpPrime *end);\n'; # reading constructor
constructsBodies += 'MTPD' + name + '::MTPD' + name + '(const mtpPrime *&from, const mtpPrime *end) {\n';
constructsBodies += readText;
constructsBodies += '}\n';
dataText += '\n';
for paramName in prmsList: # fields declaration
if (paramName in trivialConditions):
continue;
paramType = prms[paramName];
dataText += '\tMTP' + paramType + ' v' + paramName + ';\n';
if len(prmsList) > 0:
for paramName in prmsList: # getters
if (paramName in trivialConditions):
continue;
paramType = prms[paramName];
if (paramName in conditions):
dataText += '\t[[nodiscard]] MTP::conditional<MTP' + paramType + '> v' + paramName + '() const;\n';
constructsBodies += 'MTP::conditional<MTP' + paramType + '> MTPD' + name + '::v' + paramName + '() const {\n';
constructsBodies += '\treturn (_' + hasFlags + '.v & Flag::f_' + paramName + ') ? &_' + paramName + ' : nullptr;\n';
constructsBodies += '}\n';
else:
dataText += '\t[[nodiscard]] const MTP' + paramType + ' &v' + paramName + '() const;\n';
constructsBodies += 'const MTP' + paramType + ' &MTPD' + name + '::v' + paramName + '() const {\n';
constructsBodies += '\treturn _' + paramName + ';\n';
constructsBodies += '}\n';
dataText += '\n';
dataText += 'private:\n';
for paramName in prmsList: # fields declaration
if (paramName in trivialConditions):
continue;
paramType = prms[paramName];
dataText += '\tMTP' + paramType + ' _' + paramName + ';\n';
dataText += '\n';
sizeCases += '\tcase mtpc_' + name + ': {\n';
sizeCases += '\t\tconst MTPD' + name + ' &v(c_' + name + '());\n';
sizeCases += '\t\treturn ' + ' + '.join(sizeList) + ';\n';
@@ -740,24 +780,20 @@ for restype in typesList:
reader += '\tcase mtpc_' + name + ': _type = cons; '; # read switch line
if (len(prms) > len(trivialConditions)):
reader += '{\n';
reader += '\t\tauto v = new MTPD' + name + '();\n';
reader += '\t\tsetData(v);\n';
reader += readText;
reader += '\t\tsetData(new MTPD' + name + '(from, end));\n';
reader += '\t} break;\n';
writer += '\tcase mtpc_' + name + ': {\n'; # write switch line
writer += '\t\tauto &v = c_' + name + '();\n';
writer += '\t\tconst MTPD' + name + ' &v = c_' + name + '();\n';
writer += writeText;
writer += '\t} break;\n';
else:
reader += 'break;\n';
else:
if (len(prms) > len(trivialConditions)):
reader += '\n\tauto v = new MTPD' + name + '();\n';
reader += '\tsetData(v);\n';
reader += readText;
reader += '\tsetData(new MTPD' + name + '(from, end));\n';
writer += '\tconst auto &v = c_' + name + '();\n';
writer += '\tconst MTPD' + name + ' &v = c_' + name + '();\n';
writer += writeText;
forwards += '\n';
@@ -767,12 +803,11 @@ for restype in typesList:
typesText += ' : private MTP::internal::TypeDataOwner'; # if has data fields
typesText += ' {\n';
typesText += 'public:\n';
typesText += '\tMTP' + restype + '()'; # default constructor
typesText += '\tMTP' + restype + '();\n'; # default constructor
if (withData and not withType):
typesText += ';\n';
methods += '\nMTP' + restype + '::MTP' + restype + '() : TypeDataOwner(' + newFast + ') {\n}\n';
else:
typesText += ' = default;\n';
methods += '\nMTP' + restype + '::MTP' + restype + '() = default;\n';
typesText += getters;
typesText += '\n';