Doxygen
Toggle main menu visibility
Loading...
Searching...
No Matches
scopedtypevariant.h
Go to the documentation of this file.
1
/******************************************************************************
2
*
3
* Copyright (C) 1997-2020 by Dimitri van Heesch.
4
*
5
* Permission to use, copy, modify, and distribute this software and its
6
* documentation under the terms of the GNU General Public License is hereby
7
* granted. No representations are made about the suitability of this software
8
* for any purpose. It is provided "as is" without express or implied warranty.
9
* See the GNU General Public License for more details.
10
*
11
* Documents produced by Doxygen are derivative works derived from the
12
* input used in their production; they are not affected by this license.
13
*
14
*/
15
16
#ifndef SCOPEDTYPEVARIANT_H
17
#define SCOPEDTYPEVARIANT_H
18
19
#include <utility>
20
#include <variant>
21
#include <vector>
22
23
#include "
dstring.h
"
24
25
class
Definition
;
26
27
//! Class representing a local class definition found while
28
//! generating syntax highlighted code.
29
class
LocalDef
30
{
31
public
:
32
void
insertBaseClass
(
const
DString
&name) {
m_baseClasses
.push_back(name); }
33
std::vector<DString>
baseClasses
()
const
{
return
m_baseClasses
; }
34
private
:
35
std::vector<DString>
m_baseClasses
;
36
};
37
38
//-----------------------------------------------------------------------------
39
40
/*! Variant class for a scoped type.
41
*
42
* Variants:
43
* - DummyDef: a type used for hiding a global type.
44
* - LocalDef: a locally defined type (e.g. found inside a function)
45
* - GlobalDef: a globally defined type (processed by doxygen in an earlier pass).
46
*/
47
class
ScopedTypeVariant
48
{
49
public
:
50
struct
DummyDef
{};
51
using
GlobalDef
=
const
Definition
*;
52
using
Variant
= std::variant<DummyDef,LocalDef,GlobalDef>;
53
explicit
ScopedTypeVariant
(
GlobalDef
d =
nullptr
)
54
:
m_name
(d ? d->
name
() :
DString
()),
m_variant
(d ?
Variant
(d) :
Variant
(
DummyDef
())) {}
55
explicit
ScopedTypeVariant
(
const
DString
&
name
)
56
:
m_name
(
name
),
m_variant
(
LocalDef
()) {}
57
DString
name
()
const
{
return
m_name
; }
58
LocalDef
*
localDef
() {
return
std::get_if<LocalDef>(&
m_variant
); }
59
const
LocalDef
*
localDef
()
const
{
return
std::get_if<LocalDef>(&
m_variant
); }
60
const
Definition
*
globalDef
()
const
{
auto
pp = std::get_if<GlobalDef>(&
m_variant
);
return
pp ? *pp :
nullptr
; }
61
bool
isDummy
()
const
{
return
std::holds_alternative<DummyDef>(
m_variant
); }
62
63
private
:
64
DString
m_name
;
65
Variant
m_variant
;
66
};
67
68
//-----------------------------------------------------------------------------
69
70
/*! Represents a stack of variable to class mappings as found in the
71
* code. Each scope is enclosed in pushScope() and popScope() calls.
72
* Variables are added by calling addVariables() and one can search
73
* for variable using findVariable().
74
*/
75
class
VariableContext
76
{
77
public
:
78
using
Scope
= std::unordered_map<std::string,ScopedTypeVariant>;
79
80
void
pushScope
()
81
{
82
m_scopes
.emplace_back();
83
}
84
void
popScope
()
85
{
86
if
(!
m_scopes
.empty())
87
{
88
m_scopes
.pop_back();
89
}
90
}
91
void
clear
()
92
{
93
m_scopes
.clear();
94
m_globalScope
.clear();
95
}
96
void
clearExceptGlobal
()
97
{
98
m_scopes
.clear();
99
}
100
void
addVariable
(
const
DString
&name,
ScopedTypeVariant
stv)
101
{
102
Scope
*scope =
m_scopes
.empty() ? &
m_globalScope
: &
m_scopes
.back();
103
scope->emplace(name.
str
(),std::move(stv));
// add it to a list
104
}
105
const
ScopedTypeVariant
*
findVariable
(
const
DString
&name)
106
{
107
const
ScopedTypeVariant
*result =
nullptr
;
108
if
(name.
empty
())
return
result;
109
110
// search from inner to outer scope
111
auto
it = std::rbegin(
m_scopes
);
112
while
(it != std::rend(
m_scopes
))
113
{
114
auto
it2 = it->find(name.
str
());
115
if
(it2 != std::end(*it))
116
{
117
result = &it2->second;
118
return
result;
119
}
120
++it;
121
}
122
// nothing found -> also try the global scope
123
auto
it2 =
m_globalScope
.find(name.
str
());
124
if
(it2 !=
m_globalScope
.end())
125
{
126
result = &it2->second;
127
}
128
return
result;
129
}
130
bool
atGlobalScope
()
const
{
return
m_scopes
.empty(); }
131
132
private
:
133
Scope
m_globalScope
;
134
std::vector<Scope>
m_scopes
;
135
};
136
137
//-----------------------------------------------------------------------------
138
139
/** Represents the call context */
140
class
CallContext
141
{
142
public
:
143
struct
Ctx
144
{
145
Ctx
(
const
DString
&name_,
const
DString
&type_,
int
bracketCount_) :
name
(name_),
type
(type_),
bracketCount
(bracketCount_) {}
146
DString
name
;
147
DString
type
;
148
int
bracketCount
;
149
ScopedTypeVariant
stv
;
150
};
151
152
CallContext
()
153
{
154
clear
();
155
}
156
void
setScope
(
const
ScopedTypeVariant
&stv)
157
{
158
Ctx
&ctx =
m_stvList
.back();
159
ctx.
stv
=stv;
160
}
161
void
pushScope
(
const
DString
&name_,
const
DString
&type_,
int
bracketCount_)
162
{
163
m_stvList
.emplace_back(name_,type_,bracketCount_);
164
}
165
void
popScope
(
DString
&name_,
DString
&type_,
int
&bracketCount_)
166
{
167
if
(
m_stvList
.size()>1)
168
{
169
const
Ctx
&ctx =
m_stvList
.back();
170
name_ = ctx.
name
;
171
type_ = ctx.
type
;
172
bracketCount_ = ctx.
bracketCount
;
173
m_stvList
.pop_back();
174
}
175
}
176
void
clear
()
177
{
178
m_stvList
.clear();
179
m_stvList
.emplace_back(
DString
(),
DString
(),0);
180
}
181
const
ScopedTypeVariant
getScope
()
const
182
{
183
return
m_stvList
.back().stv;
184
}
185
186
private
:
187
std::vector<Ctx>
m_stvList
;
188
};
189
190
191
#endif
CallContext::getScope
const ScopedTypeVariant getScope() const
Definition
scopedtypevariant.h:181
CallContext::pushScope
void pushScope(const DString &name_, const DString &type_, int bracketCount_)
Definition
scopedtypevariant.h:161
CallContext::clear
void clear()
Definition
scopedtypevariant.h:176
CallContext::setScope
void setScope(const ScopedTypeVariant &stv)
Definition
scopedtypevariant.h:156
CallContext::m_stvList
std::vector< Ctx > m_stvList
Definition
scopedtypevariant.h:187
CallContext::CallContext
CallContext()
Definition
scopedtypevariant.h:152
CallContext::popScope
void popScope(DString &name_, DString &type_, int &bracketCount_)
Definition
scopedtypevariant.h:165
DString
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition
dstring.h:84
DString::empty
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition
dstring.h:148
DString::str
const std::string & str() const
Definition
dstring.h:645
Definition
The common base class of all entity definitions found in the sources.
Definition
definition.h:77
LocalDef
Definition
scopedtypevariant.h:30
LocalDef::baseClasses
std::vector< DString > baseClasses() const
Definition
scopedtypevariant.h:33
LocalDef::insertBaseClass
void insertBaseClass(const DString &name)
Definition
scopedtypevariant.h:32
LocalDef::m_baseClasses
std::vector< DString > m_baseClasses
Definition
scopedtypevariant.h:35
ScopedTypeVariant
Definition
scopedtypevariant.h:48
ScopedTypeVariant::name
DString name() const
Definition
scopedtypevariant.h:57
ScopedTypeVariant::isDummy
bool isDummy() const
Definition
scopedtypevariant.h:61
ScopedTypeVariant::localDef
const LocalDef * localDef() const
Definition
scopedtypevariant.h:59
ScopedTypeVariant::m_name
DString m_name
Definition
scopedtypevariant.h:64
ScopedTypeVariant::GlobalDef
const Definition * GlobalDef
Definition
scopedtypevariant.h:51
ScopedTypeVariant::localDef
LocalDef * localDef()
Definition
scopedtypevariant.h:58
ScopedTypeVariant::Variant
std::variant< DummyDef, LocalDef, GlobalDef > Variant
Definition
scopedtypevariant.h:52
ScopedTypeVariant::globalDef
const Definition * globalDef() const
Definition
scopedtypevariant.h:60
ScopedTypeVariant::m_variant
Variant m_variant
Definition
scopedtypevariant.h:65
ScopedTypeVariant::ScopedTypeVariant
ScopedTypeVariant(GlobalDef d=nullptr)
Definition
scopedtypevariant.h:53
ScopedTypeVariant::ScopedTypeVariant
ScopedTypeVariant(const DString &name)
Definition
scopedtypevariant.h:55
VariableContext
Definition
scopedtypevariant.h:76
VariableContext::m_globalScope
Scope m_globalScope
Definition
scopedtypevariant.h:133
VariableContext::clear
void clear()
Definition
scopedtypevariant.h:91
VariableContext::atGlobalScope
bool atGlobalScope() const
Definition
scopedtypevariant.h:130
VariableContext::addVariable
void addVariable(const DString &name, ScopedTypeVariant stv)
Definition
scopedtypevariant.h:100
VariableContext::m_scopes
std::vector< Scope > m_scopes
Definition
scopedtypevariant.h:134
VariableContext::popScope
void popScope()
Definition
scopedtypevariant.h:84
VariableContext::Scope
std::unordered_map< std::string, ScopedTypeVariant > Scope
Definition
scopedtypevariant.h:78
VariableContext::pushScope
void pushScope()
Definition
scopedtypevariant.h:80
VariableContext::findVariable
const ScopedTypeVariant * findVariable(const DString &name)
Definition
scopedtypevariant.h:105
VariableContext::clearExceptGlobal
void clearExceptGlobal()
Definition
scopedtypevariant.h:96
dstring.h
CallContext::Ctx
Definition
scopedtypevariant.h:144
CallContext::Ctx::type
DString type
Definition
scopedtypevariant.h:147
CallContext::Ctx::stv
ScopedTypeVariant stv
Definition
scopedtypevariant.h:149
CallContext::Ctx::bracketCount
int bracketCount
Definition
scopedtypevariant.h:148
CallContext::Ctx::Ctx
Ctx(const DString &name_, const DString &type_, int bracketCount_)
Definition
scopedtypevariant.h:145
CallContext::Ctx::name
DString name
Definition
scopedtypevariant.h:146
ScopedTypeVariant::DummyDef
Definition
scopedtypevariant.h:50
src
scopedtypevariant.h
Generated by
1.19.0