Skip to content

Commit 6de4422

Browse files
committed
Added type-safe wrapper class around low-level bitfield type
1 parent c2cc416 commit 6de4422

File tree

11 files changed

+108
-74
lines changed

11 files changed

+108
-74
lines changed

src/cite.h

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,47 @@
2222
#include "qcstring.h"
2323
#include "construct.h"
2424

25+
class CiteInfoOption
26+
{
27+
public:
28+
CiteInfoOption() {}
29+
static CiteInfoOption makeNumber() { return CiteInfoOption(NUMBER); }
30+
static CiteInfoOption makeShortAuthor() { return CiteInfoOption(SHORTAUTHOR); }
31+
static CiteInfoOption makeYear() { return CiteInfoOption(YEAR); }
32+
void setNoPar() { m_bits |= NOPAR_BIT; }
33+
void setNoCite() { m_bits |= NOCITE_BIT; }
34+
35+
bool isUnknown() const { return (m_bits & TypeMask)==0; }
36+
bool isNumber() const { return (m_bits & NUMBER)!=0; }
37+
bool isShortAuthor() const { return (m_bits & SHORTAUTHOR)!=0; }
38+
bool isYear() const { return (m_bits & YEAR)!=0; }
39+
40+
bool noPar() const { return (m_bits & NOPAR_BIT)!=0; }
41+
bool noCite() const { return (m_bits & NOCITE_BIT)!=0; }
42+
43+
friend inline bool operator==(const CiteInfoOption &t1,const CiteInfoOption &t2) { return t1.m_bits==t2.m_bits; }
44+
friend inline bool operator!=(const CiteInfoOption &t1,const CiteInfoOption &t2) { return !(operator==(t1,t2)); }
45+
46+
private:
47+
CiteInfoOption(int bits) : m_bits(bits) {}
48+
49+
enum Bits
50+
{
51+
UNKNOWN = 0,
52+
NUMBER = (1<<0),
53+
SHORTAUTHOR = (1<<1),
54+
YEAR = (1<<2),
55+
56+
TypeMask = 0x0000FFFF,
57+
OptionMask = 0xFFFF0000,
58+
59+
NOPAR_BIT = (1<<16), //< Don't use square brackets
60+
NOCITE_BIT = (1<<17) //< Don't create a link
61+
};
62+
unsigned int m_bits = UNKNOWN;
63+
};
64+
65+
2566
/// Citation-related data.
2667
struct CiteInfo
2768
{
@@ -31,16 +72,6 @@ struct CiteInfo
3172
virtual QCString text() const = 0;
3273
virtual QCString shortAuthor() const = 0;
3374
virtual QCString year() const = 0;
34-
35-
enum CiteOptionType {
36-
UNKNOWN = 0x00,
37-
NUMBER = 0x01,
38-
SHORTAUTHOR = 0x02,
39-
YEAR = 0x04,
40-
41-
NOPAR_BIT = 0x001000, //< Don't use square brackets
42-
NOCITE_BIT = 0x100000, //< Don't create a link
43-
};
4475
};
4576

4677
/**

src/docbookvisitor.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -603,20 +603,20 @@ void DocbookDocVisitor::operator()(const DocCite &cite)
603603
{
604604
DB_VIS_C
605605
if (m_hide) return;
606-
int opt = cite.option();
606+
auto opt = cite.option();
607607
if (!cite.file().isEmpty())
608608
{
609-
if (!(opt & CiteInfo::NOCITE_BIT)) startLink(cite.file(),filterId(cite.anchor()));
609+
if (!opt.noCite()) startLink(cite.file(),filterId(cite.anchor()));
610610

611611
filter(cite.getText());
612612

613-
if (!(opt & CiteInfo::NOCITE_BIT)) endLink();
613+
if (!opt.noCite()) endLink();
614614
}
615615
else
616616
{
617-
if (!(opt & CiteInfo::NOPAR_BIT)) filter("[");
617+
if (!opt.noPar()) filter("[");
618618
filter(cite.target());
619-
if (!(opt & CiteInfo::NOPAR_BIT)) filter("]");
619+
if (!opt.noPar()) filter("]");
620620

621621
}
622622

src/docnode.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ void DocRef::parse()
900900

901901
//---------------------------------------------------------------------------
902902

903-
DocCite::DocCite(DocParser *parser,DocNodeVariant *parent,const QCString &target,const QCString &,int opt) : DocNode(parser,parent)
903+
DocCite::DocCite(DocParser *parser,DocNodeVariant *parent,const QCString &target,const QCString &,CiteInfoOption opt) : DocNode(parser,parent)
904904
{
905905
size_t numBibFiles = Config_getList(CITE_BIB_FILES).size();
906906
//printf("DocCite::DocCite(target=%s)\n",qPrint(target));
@@ -939,20 +939,20 @@ DocCite::DocCite(DocParser *parser,DocNodeVariant *parent,const QCString &target
939939
QCString DocCite::getText() const
940940
{
941941
QCString txt;
942-
int opt = m_option;
942+
auto opt = m_option;
943943
const CitationManager &ct = CitationManager::instance();
944944
const CiteInfo *citeInfo = ct.find(m_target);
945945

946-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "[";
946+
if (!opt.noPar()) txt += "[";
947947

948948
if (citeInfo)
949949
{
950-
if (opt & CiteInfo::NUMBER) txt += citeInfo->text();
951-
else if (opt & CiteInfo::SHORTAUTHOR) txt += citeInfo->shortAuthor();
952-
else if (opt & CiteInfo::YEAR) txt += citeInfo->year();
950+
if (opt.isNumber()) txt += citeInfo->text();
951+
else if (opt.isShortAuthor()) txt += citeInfo->shortAuthor();
952+
else if (opt.isYear()) txt += citeInfo->year();
953953
}
954954

955-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "]";
955+
if (!opt.noPar()) txt += "]";
956956
return txt;
957957
}
958958

@@ -3329,9 +3329,7 @@ void DocPara::handleCite(char cmdChar,const QCString &cmdName)
33293329
// get the argument of the cite command.
33303330
Token tok=parser()->tokenizer.lex();
33313331

3332-
int option = CiteInfo::UNKNOWN;
3333-
bool nopar_bit = false;
3334-
bool nocite_bit = false;
3332+
CiteInfoOption option;
33353333
if (tok.is(TokenRetval::TK_WORD) && parser()->context.token->name=="{")
33363334
{
33373335
parser()->tokenizer.setStateOptions();
@@ -3341,48 +3339,52 @@ void DocPara::handleCite(char cmdChar,const QCString &cmdName)
33413339
{
33423340
if (opt == "number")
33433341
{
3344-
if (option != CiteInfo::UNKNOWN)
3342+
if (!option.isUnknown())
33453343
{
33463344
warn(parser()->context.fileName,parser()->tokenizer.getLineNr(),"Multiple options specified with \\{}, discarding '{}'", saveCmdName, opt);
33473345
}
33483346
else
33493347
{
3350-
option = CiteInfo::NUMBER;
3348+
option = CiteInfoOption::makeNumber();
33513349
}
33523350
}
33533351
else if (opt == "year")
33543352
{
3355-
if (option != CiteInfo::UNKNOWN)
3353+
if (!option.isUnknown())
33563354
{
33573355
warn(parser()->context.fileName,parser()->tokenizer.getLineNr(),"Multiple options specified with \\{}, discarding '{}'", saveCmdName, opt);
33583356
}
33593357
else
33603358
{
3361-
option = CiteInfo::YEAR;
3359+
option = CiteInfoOption::makeYear();
33623360
}
33633361
}
33643362
else if (opt == "shortauthor")
33653363
{
3366-
if (option != CiteInfo::UNKNOWN)
3364+
if (!option.isUnknown())
33673365
{
33683366
warn(parser()->context.fileName,parser()->tokenizer.getLineNr(),"Multiple options specified with \\{}, discarding '{}'", saveCmdName, opt);
33693367
}
33703368
else
33713369
{
3372-
option = CiteInfo::SHORTAUTHOR;
3370+
option = CiteInfoOption::makeShortAuthor();
33733371
}
33743372
}
3375-
else if (opt == "nopar") nopar_bit = true;
3376-
else if (opt == "nocite") nocite_bit = true;
3373+
else if (opt == "nopar")
3374+
{
3375+
option.setNoPar();
3376+
}
3377+
else if (opt == "nocite")
3378+
{
3379+
option.setNoCite();
3380+
}
33773381
else
33783382
{
33793383
warn(parser()->context.fileName,parser()->tokenizer.getLineNr(),"Unkown option specified with \\{}, discarding '{}'", saveCmdName, opt);
33803384
}
33813385
}
33823386

3383-
if (option == CiteInfo::UNKNOWN) option = CiteInfo::NUMBER;
3384-
if (nopar_bit) option |= CiteInfo::NOPAR_BIT;
3385-
if (nocite_bit) option |= CiteInfo::NOCITE_BIT;
3387+
if (option.isUnknown()) option = CiteInfoOption::makeNumber();
33863388

33873389
parser()->tokenizer.setStatePara();
33883390
tok=parser()->tokenizer.lex();
@@ -3401,7 +3403,7 @@ void DocPara::handleCite(char cmdChar,const QCString &cmdName)
34013403
}
34023404
else
34033405
{
3404-
option = CiteInfo::NUMBER;
3406+
option = CiteInfoOption::makeNumber();
34053407
}
34063408

34073409
parser()->tokenizer.setStateCite();

src/docnode.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "section.h"
3333
#include "construct.h"
3434
#include "doctokenizer.h"
35+
#include "cite.h"
3536

3637
class MemberDef;
3738
class Definition;
@@ -243,13 +244,13 @@ class DocAnchor : public DocNode
243244
class DocCite : public DocNode
244245
{
245246
public:
246-
DocCite(DocParser *parser,DocNodeVariant *parent,const QCString &target,const QCString &context, int opt);
247+
DocCite(DocParser *parser,DocNodeVariant *parent,const QCString &target,const QCString &context, CiteInfoOption opt);
247248
QCString file() const { return m_file; }
248249
QCString relPath() const { return m_relPath; }
249250
QCString ref() const { return m_ref; }
250251
QCString anchor() const { return m_anchor; }
251252
QCString target() const { return m_target; }
252-
int option() const { return m_option; }
253+
CiteInfoOption option() const { return m_option; }
253254
QCString getText() const;
254255

255256
private:
@@ -258,7 +259,7 @@ class DocCite : public DocNode
258259
QCString m_ref;
259260
QCString m_anchor;
260261
QCString m_target;
261-
int m_option;
262+
CiteInfoOption m_option;
262263
};
263264

264265

src/htmldocvisitor.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -996,19 +996,19 @@ void HtmlDocVisitor::operator()(const DocSimpleSectSep &)
996996
void HtmlDocVisitor::operator()(const DocCite &cite)
997997
{
998998
if (m_hide) return;
999-
int opt = cite.option();
999+
auto opt = cite.option();
10001000
if (!cite.file().isEmpty())
10011001
{
1002-
if (!(opt & CiteInfo::NOCITE_BIT)) startLink(cite.ref(),cite.file(),cite.relPath(),cite.anchor());
1002+
if (!opt.noCite()) startLink(cite.ref(),cite.file(),cite.relPath(),cite.anchor());
10031003
filter(cite.getText());
1004-
if (!(opt & CiteInfo::NOCITE_BIT)) endLink();
1004+
if (!opt.noCite()) endLink();
10051005
}
10061006
else
10071007
{
10081008
m_t << "<b>";
1009-
if (!(opt & CiteInfo::NOPAR_BIT)) filter("[");
1009+
if (!opt.noPar()) filter("[");
10101010
filter(cite.target());
1011-
if (!(opt & CiteInfo::NOPAR_BIT)) filter("]");
1011+
if (!opt.noPar()) filter("]");
10121012
m_t << "</b>";
10131013
}
10141014
}

src/latexdocvisitor.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -737,19 +737,19 @@ void LatexDocVisitor::operator()(const DocSimpleSectSep &)
737737
void LatexDocVisitor::operator()(const DocCite &cite)
738738
{
739739
if (m_hide) return;
740-
int opt = cite.option();
740+
auto opt = cite.option();
741741
QCString txt;
742-
if (opt & CiteInfo::NOCITE_BIT)
742+
if (opt.noCite())
743743
{
744744
if (!cite.file().isEmpty())
745745
{
746746
txt = cite.getText();
747747
}
748748
else
749749
{
750-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "[";
750+
if (!opt.noPar()) txt += "[";
751751
txt += cite.target();
752-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "]";
752+
if (!opt.noPar()) txt += "]";
753753
}
754754
m_t << "{\\bfseries ";
755755
filter(txt);
@@ -764,28 +764,28 @@ void LatexDocVisitor::operator()(const DocCite &cite)
764764
anchor = anchor.mid(anchorPrefix.length()); // strip prefix
765765

766766
txt = "\\DoxyCite{" + anchor + "}";
767-
if (opt & CiteInfo::NUMBER)
767+
if (opt.isNumber())
768768
{
769769
txt += "{number}";
770770
}
771-
else if (opt & CiteInfo::SHORTAUTHOR)
771+
else if (opt.isShortAuthor())
772772
{
773773
txt += "{shortauthor}";
774774
}
775-
else if (opt & CiteInfo::YEAR)
775+
else if (opt.isYear())
776776
{
777777
txt += "{year}";
778778
}
779-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "{1}";
779+
if (!opt.noPar()) txt += "{1}";
780780
else txt += "{0}";
781781

782782
m_t << txt;
783783
}
784784
else
785785
{
786-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "[";
786+
if (!opt.noPar()) txt += "[";
787787
txt += cite.target();
788-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "]";
788+
if (!opt.noPar()) txt += "]";
789789
m_t << "{\\bfseries ";
790790
filter(txt);
791791
m_t << "}";

src/mandocvisitor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,17 +428,17 @@ void ManDocVisitor::operator()(const DocCite &cite)
428428
{
429429
if (m_hide) return;
430430
m_t << "\\fB";
431-
int opt = cite.option();
431+
auto opt = cite.option();
432432
QCString txt;
433433
if (!cite.file().isEmpty())
434434
{
435435
txt = cite.getText();
436436
}
437437
else
438438
{
439-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "[";
439+
if (!opt.noPar()) txt += "[";
440440
txt += cite.target();
441-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "]";
441+
if (!opt.noPar()) txt += "]";
442442
}
443443
filter(txt);
444444
m_t << "\\fP";

src/perlmodgen.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,17 +745,17 @@ void PerlModDocVisitor::operator()(const DocSimpleSectSep &)
745745
void PerlModDocVisitor::operator()(const DocCite &cite)
746746
{
747747
openItem("cite");
748-
int opt = cite.option();
748+
auto opt = cite.option();
749749
QCString txt;
750750
if (!cite.file().isEmpty())
751751
{
752752
txt = cite.getText();
753753
}
754754
else
755755
{
756-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "[";
756+
if (!opt.noPar()) txt += "[";
757757
txt += cite.target();
758-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "]";
758+
if (!opt.noPar()) txt += "]";
759759
}
760760
m_output.addFieldQuotedString("text", txt);
761761
closeItem();

src/printdocvisitor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,17 @@ class PrintDocVisitor
254254
void operator()(const DocCite &cite)
255255
{
256256
indent_leaf();
257-
int opt = cite.option();
257+
auto opt = cite.option();
258258
QCString txt;
259259
if (!cite.file().isEmpty())
260260
{
261261
txt = cite.getText();
262262
}
263263
else
264264
{
265-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "[";
265+
if (!opt.noPar()) txt += "[";
266266
txt += cite.target();
267-
if (!(opt & CiteInfo::NOPAR_BIT)) txt += "]";
267+
if (!opt.noPar()) txt += "]";
268268
}
269269
printf("<cite ref=\"%s\" file=\"%s\" "
270270
"anchor=\"%s\" text=\"%s\""

0 commit comments

Comments
 (0)