Skip to content

Commit 83455ae

Browse files
committed
Prevent code duplication for enum
Prevent some code duplication and more generic way to get string values
1 parent 44f85e4 commit 83455ae

File tree

6 files changed

+220
-263
lines changed

6 files changed

+220
-263
lines changed

src/defgen.cpp

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,8 @@ static void generateDEFForMember(const MemberDef *md,
114114
<< md->getOutputFileBase() << "_1" << md->anchor()
115115
<< "';\n";
116116

117-
t << memPrefix << "virt = ";
118-
switch (md->virtualness())
119-
{
120-
case Specifier::Normal: t << "normal;\n"; break;
121-
case Specifier::Virtual: t << "virtual;\n"; break;
122-
case Specifier::Pure: t << "pure-virtual;\n"; break;
123-
default: ASSERT(0);
124-
}
125-
126-
t << memPrefix << "prot = ";
127-
switch(md->protection())
128-
{
129-
case Protection::Public: t << "public;\n"; break;
130-
case Protection::Protected: t << "protected;\n"; break;
131-
case Protection::Private: t << "private;\n"; break;
132-
case Protection::Package: t << "package;\n"; break;
133-
}
117+
t << memPrefix << "virt = " << to_string_lower(md->virtualness()) << ";\n";
118+
t << memPrefix << "prot = " << to_string_lower(md->protection()) << ";\n";
134119

135120
if (md->memberType()!=MemberType::Define &&
136121
md->memberType()!=MemberType::Enumeration
@@ -341,47 +326,19 @@ static void generateDEFForClass(const ClassDef *cd,TextStream &t)
341326
for (const auto &bcd : cd->baseClasses())
342327
{
343328
t << " cp-ref = {\n" << " ref-type = base;\n";
344-
t << " ref-id = '"
345-
<< bcd.classDef->getOutputFileBase() << "';\n";
346-
t << " ref-prot = ";
347-
switch (bcd.prot)
348-
{
349-
case Protection::Public: t << "public;\n"; break;
350-
case Protection::Package: // package scope is not possible
351-
case Protection::Protected: t << "protected;\n"; break;
352-
case Protection::Private: t << "private;\n"; break;
353-
}
354-
t << " ref-virt = ";
355-
switch(bcd.virt)
356-
{
357-
case Specifier::Normal: t << "non-virtual;"; break;
358-
case Specifier::Virtual: t << "virtual;"; break;
359-
case Specifier::Pure: t << "pure-virtual;"; break;
360-
}
361-
t << "\n };\n";
329+
t << " ref-id = '" << bcd.classDef->getOutputFileBase() << "';\n";
330+
t << " ref-prot = " << to_string_lower_class(bcd.prot) << ";\n";
331+
t << " ref-virt = " << to_string_lower(bcd.virt) << ";\n";
332+
t << " };\n";
362333
}
363334

364335
for (const auto &bcd : cd->subClasses())
365336
{
366337
t << " cp-ref = {\n" << " ref-type = derived;\n";
367-
t << " ref-id = '"
368-
<< bcd.classDef->getOutputFileBase() << "';\n";
369-
t << " ref-prot = ";
370-
switch (bcd.prot)
371-
{
372-
case Protection::Public: t << "public;\n"; break;
373-
case Protection::Package: // packet scope is not possible!
374-
case Protection::Protected: t << "protected;\n"; break;
375-
case Protection::Private: t << "private;\n"; break;
376-
}
377-
t << " ref-virt = ";
378-
switch (bcd.virt)
379-
{
380-
case Specifier::Normal: t << "non-virtual;"; break;
381-
case Specifier::Virtual: t << "virtual;"; break;
382-
case Specifier::Pure: t << "pure-virtual;"; break;
383-
}
384-
t << "\n };\n";
338+
t << " ref-id = '" << bcd.classDef->getOutputFileBase() << "';\n";
339+
t << " ref-prot = " << to_string_lower_class(bcd.prot) << ";\n";
340+
t << " ref-virt = " << to_string_lower(bcd.virt) << ";\n";
341+
t << " };\n";
385342
}
386343

387344
size_t numMembers = 0;

src/message.h

Lines changed: 6 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -156,108 +156,47 @@ template<> struct fmt::formatter<QCString> : formatter<std::string>
156156
template<> struct fmt::formatter<Protection> : formatter<std::string>
157157
{
158158
auto format(Protection prot, format_context& ctx) const {
159-
std::string result="Unknown";
160-
switch (prot)
161-
{
162-
case Protection::Public: result="Public"; break;
163-
case Protection::Protected: result="Protected"; break;
164-
case Protection::Private: result="Private"; break;
165-
case Protection::Package: result="Package"; break;
166-
}
167-
return formatter<std::string>::format(result, ctx);
159+
return formatter<std::string>::format(to_string(prot), ctx);
168160
}
169161
};
170162

171163
//! adds support for formatting Specifier
172164
template<> struct fmt::formatter<Specifier> : formatter<std::string>
173165
{
174166
auto format(Specifier spec, format_context& ctx) const {
175-
std::string result="Unknown";
176-
switch (spec)
177-
{
178-
case Specifier::Normal: result="Normal"; break;
179-
case Specifier::Virtual: result="Virtual"; break;
180-
case Specifier::Pure: result="Pure"; break;
181-
}
182-
return formatter<std::string>::format(result, ctx);
167+
return formatter<std::string>::format(to_string(spec), ctx);
183168
}
184169
};
185170

186171
//! adds support for formatting MethodTypes
187172
template<> struct fmt::formatter<MethodTypes> : formatter<std::string>
188173
{
189174
auto format(MethodTypes mtype, format_context& ctx) const {
190-
std::string result="Unknown";
191-
switch (mtype)
192-
{
193-
case MethodTypes::Method: result="Method"; break;
194-
case MethodTypes::Signal: result="Signal"; break;
195-
case MethodTypes::Slot: result="Slot"; break;
196-
case MethodTypes::DCOP: result="DCOP"; break;
197-
case MethodTypes::Property: result="Property"; break;
198-
case MethodTypes::Event: result="Event"; break;
199-
}
200-
return formatter<std::string>::format(result, ctx);
175+
return formatter<std::string>::format(to_string(mtype), ctx);
201176
}
202177
};
203178

204179
//! adds support for formatting RelatesType
205180
template<> struct fmt::formatter<RelatesType> : formatter<std::string>
206181
{
207182
auto format(RelatesType type, format_context& ctx) const {
208-
std::string result="Unknown";
209-
switch (type)
210-
{
211-
case RelatesType::Simple: result="Simple"; break;
212-
case RelatesType::Duplicate: result="Duplicate"; break;
213-
case RelatesType::MemberOf: result="MemberOf"; break;
214-
}
215-
return formatter<std::string>::format(result, ctx);
183+
return formatter<std::string>::format(to_string(type), ctx);
216184
}
217185
};
218186

219187
//! adds support for formatting RelationShip
220188
template<> struct fmt::formatter<Relationship> : formatter<std::string>
221189
{
222190
auto format(Relationship relation, format_context& ctx) const {
223-
std::string result="Unknown";
224-
switch (relation)
225-
{
226-
case Relationship::Member: result="Member"; break;
227-
case Relationship::Related: result="Related"; break;
228-
case Relationship::Foreign: result="Foreign"; break;
229-
}
230-
return formatter<std::string>::format(result, ctx);
191+
return formatter<std::string>::format(to_string(relation), ctx);
231192
}
232193
};
233194

234195
//! adds support for formatting SrcLangExt
235196
template<> struct fmt::formatter<SrcLangExt> : formatter<std::string>
236197
{
237198
auto format(SrcLangExt lang, format_context& ctx) const {
238-
std::string result="Unknown";
239-
switch (lang)
240-
{
241-
case SrcLangExt::Unknown: result="Unknown"; break;
242-
case SrcLangExt::IDL: result="IDL"; break;
243-
case SrcLangExt::Java: result="Java"; break;
244-
case SrcLangExt::CSharp: result="C#"; break;
245-
case SrcLangExt::D: result="D"; break;
246-
case SrcLangExt::PHP: result="PHP"; break;
247-
case SrcLangExt::ObjC: result="Objective-C"; break;
248-
case SrcLangExt::Cpp: result="C++"; break;
249-
case SrcLangExt::JS: result="Javascript"; break;
250-
case SrcLangExt::Python: result="Python"; break;
251-
case SrcLangExt::Fortran: result="Fortran"; break;
252-
case SrcLangExt::VHDL: result="VHDL"; break;
253-
case SrcLangExt::XML: result="XML"; break;
254-
//case SrcLangExt::Tcl: result="Tcl"; break;
255-
case SrcLangExt::Markdown: result="Markdown"; break;
256-
case SrcLangExt::SQL: result="SQL"; break;
257-
case SrcLangExt::Slice: result="Slice"; break;
258-
case SrcLangExt::Lex: result="Lex"; break;
259-
}
260-
return formatter<std::string>::format(result, ctx);
199+
return formatter<std::string>::format(to_string(lang), ctx);
261200
}
262201
};
263202

src/perlmodgen.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,25 +1386,12 @@ static void addPerlModDocBlock(PerlModOutput &output,
13861386

13871387
static const char *getProtectionName(Protection prot)
13881388
{
1389-
switch (prot)
1390-
{
1391-
case Protection::Public: return "public";
1392-
case Protection::Protected: return "protected";
1393-
case Protection::Private: return "private";
1394-
case Protection::Package: return "package";
1395-
}
1396-
return nullptr;
1389+
return to_string_lower(prot);
13971390
}
13981391

13991392
static const char *getVirtualnessName(Specifier virt)
14001393
{
1401-
switch(virt)
1402-
{
1403-
case Specifier::Normal: return "non_virtual";
1404-
case Specifier::Virtual: return "virtual";
1405-
case Specifier::Pure: return "pure_virtual";
1406-
}
1407-
return nullptr;
1394+
return to_string_lower(virt);
14081395
}
14091396

14101397
static QCString pathDoxyfile;

0 commit comments

Comments
 (0)