Doxygen
scopedtypevariant.h
浏览该文件的文档.
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 <vector>
21 
22 #include "qcstring.h"
23 #include "definition.h"
24 
25 //! Class representing a local class definition found while
26 //! generating syntax highlighted code.
27 class LocalDef
28 {
29  public:
30  void insertBaseClass(const QCString &name) { m_baseClasses.push_back(name); }
31  std::vector<QCString> baseClasses() const { return m_baseClasses; }
32  private:
33  std::vector<QCString> m_baseClasses;
34 };
35 
36 //-----------------------------------------------------------------------------
37 
38 /*! Variant class for a scoped type.
39  *
40  * Variants:
41  * - Dummy: a type used for hiding a global type.
42  * - Local: a locally defined type (e.g. found inside a function)
43  * - Global: a globally defined type (processed by doxygen in an earlier pass).
44  */
46 {
47  public:
48  //! possible variant types
49  enum Variant
50  {
54  };
55  //! default constructor for creating a variant of type Dummy
57  {
58  m_u.globalDef = 0;
59  }
60  //! constructor for creating a variant of type Global
61  explicit ScopedTypeVariant(const Definition *d)
62  {
63  if (d)
64  {
65  m_name = d->name();
66  m_variant = Global;
67  m_u.globalDef = d;
68  }
69  else
70  {
71  m_variant = Dummy;
72  m_u.globalDef = 0;
73  }
74  }
75  //! constructor for creating a variant of type Local
76  explicit ScopedTypeVariant(const QCString &name)
77  {
78  m_name = name;
79  m_variant = Local;
80  m_u.localDef = new LocalDef;
81  }
82  //! copy constructor
84  {
85  m_variant = stv.m_variant;
86  m_name = stv.m_name;
87  if (m_variant==Local)
88  {
89  m_u.localDef = new LocalDef(*stv.m_u.localDef);
90  }
91  else if (m_variant==Global)
92  {
93  m_u.globalDef = stv.m_u.globalDef;
94  }
95  }
96  //! move constructor
98  {
99  swap(*this,stv);
100  }
101  //! assignment operator
103  {
104  swap(*this,stv);
105  return *this;
106  }
107  //! destructor
109  {
110  if (m_variant==Local)
111  {
112  delete m_u.localDef;
113  }
114  }
115  //! swap function
116  friend void swap(ScopedTypeVariant &first,ScopedTypeVariant &second)
117  {
118  using std::swap; // enable ADL
119  swap(first.m_variant,second.m_variant);
120  swap(first.m_name,second.m_name);
121  swap(first.m_u.globalDef,second.m_u.globalDef);
122  }
123  //! Turn the variant into a Global type
124  void setGlobal(const Definition *def)
125  {
126  if (m_variant==Local)
127  {
128  delete m_u.localDef;
129  }
130  m_variant = Global;
131  m_name = def->name();
132  m_u.globalDef = def;
133  }
134  //! Turn the variant into a Local type
136  {
137  if (m_variant==Local)
138  {
139  delete m_u.localDef;
140  }
141  m_variant = Local;
142  m_name = name;
143  m_u.localDef = new LocalDef;
144  return m_u.localDef;
145  }
146  //! Turn the variant into a Dummy type
147  void setDummy()
148  {
149  if (m_variant==Local)
150  {
151  delete m_u.localDef;
152  }
153  m_variant = Dummy;
154  m_name = "";
155  m_u.localDef=0;
156  }
157  Variant type() const { return m_variant; }
158  QCString name() const { return m_name; }
159  LocalDef *localDef() const { return m_variant==Local ? m_u.localDef : 0; }
160  const Definition *globalDef() const { return m_variant==Global ? m_u.globalDef : 0; }
161 
162  private:
165  union
166  {
169  } m_u;
170 };
171 
172 //-----------------------------------------------------------------------------
173 
174 /*! Represents a stack of variable to class mappings as found in the
175  * code. Each scope is enclosed in pushScope() and popScope() calls.
176  * Variables are added by calling addVariables() and one can search
177  * for variable using findVariable().
178  */
180 {
181  public:
182  using Scope = std::unordered_map<std::string,ScopedTypeVariant>;
183 
184  void pushScope()
185  {
186  m_scopes.push_back(Scope());
187  }
188  void popScope()
189  {
190  if (!m_scopes.empty())
191  {
192  m_scopes.pop_back();
193  }
194  }
195  void clear()
196  {
197  m_scopes.clear();
198  m_globalScope.clear();
199  }
201  {
202  m_scopes.clear();
203  }
204  void addVariable(const QCString &name,ScopedTypeVariant stv)
205  {
206  Scope *scope = m_scopes.empty() ? &m_globalScope : &m_scopes.back();
207  scope->emplace(std::make_pair(name.str(),std::move(stv))); // add it to a list
208  }
210  {
211  const ScopedTypeVariant *result = 0;
212  if (name.isEmpty()) return result;
213 
214  // search from inner to outer scope
215  auto it = std::rbegin(m_scopes);
216  while (it != std::rend(m_scopes))
217  {
218  auto it2 = it->find(name.str());
219  if (it2 != std::end(*it))
220  {
221  result = &it2->second;
222  return result;
223  }
224  ++it;
225  }
226  // nothing found -> also try the global scope
227  auto it2 = m_globalScope.find(name.str());
228  if (it2 != m_globalScope.end())
229  {
230  result = &it2->second;
231  }
232  return result;
233  }
234  bool atGlobalScope() const { return m_scopes.empty(); }
235 
236  private:
238  std::vector<Scope> m_scopes;
239 };
240 
241 //-----------------------------------------------------------------------------
242 
243 /** Represents the call context */
245 {
246  public:
247  struct Ctx
248  {
249  Ctx(const QCString &name_,const QCString &type_) : name(name_), type(type_) {}
253  };
254 
256  {
257  clear();
258  }
259  void setScope(const ScopedTypeVariant &stv)
260  {
261  Ctx &ctx = m_stvList.back();
262  ctx.stv=std::move(stv);
263  }
264  void pushScope(const QCString &name_,const QCString &type_)
265  {
266  m_stvList.push_back(Ctx(name_,type_));
267  }
268  void popScope(QCString &name_,QCString &type_)
269  {
270  if (m_stvList.size()>1)
271  {
272  const Ctx &ctx = m_stvList.back();
273  name_ = ctx.name;
274  type_ = ctx.type;
275  m_stvList.pop_back();
276  }
277  }
278  void clear()
279  {
280  m_stvList.clear();
281  m_stvList.push_back(Ctx(QCString(),QCString()));
282  }
284  {
285  return m_stvList.back().stv;
286  }
287 
288  private:
289  std::vector<Ctx> m_stvList;
290 };
291 
292 
293 #endif
CallContext::setScope
void setScope(const ScopedTypeVariant &stv)
Definition: scopedtypevariant.h:259
ScopedTypeVariant::globalDef
const Definition * globalDef() const
Definition: scopedtypevariant.h:160
VariableContext::pushScope
void pushScope()
Definition: scopedtypevariant.h:184
Variant
Implementation of a variant container (similar to C++17's std::variant).
Definition: variant.h:176
Definition
The common base class of all entity definitions found in the sources.
Definition: definition.h:76
VariableContext::atGlobalScope
bool atGlobalScope() const
Definition: scopedtypevariant.h:234
VariableContext::addVariable
void addVariable(const QCString &name, ScopedTypeVariant stv)
Definition: scopedtypevariant.h:204
VariableContext::popScope
void popScope()
Definition: scopedtypevariant.h:188
ScopedTypeVariant
Definition: scopedtypevariant.h:45
CallContext::getScope
const ScopedTypeVariant getScope() const
Definition: scopedtypevariant.h:283
VariableContext::clear
void clear()
Definition: scopedtypevariant.h:195
QCString::isEmpty
bool isEmpty() const
Returns TRUE iff the string is empty
Definition: qcstring.h:144
LocalDef::insertBaseClass
void insertBaseClass(const QCString &name)
Definition: scopedtypevariant.h:43
ScopedTypeVariant::ScopedTypeVariant
ScopedTypeVariant(const Definition *d)
constructor for creating a variant of type Global
Definition: scopedtypevariant.h:61
CallContext::Ctx::Ctx
Ctx(const QCString &name_, const QCString &type_)
Definition: scopedtypevariant.h:249
QCString::str
std::string str() const
Definition: qcstring.h:442
ScopedTypeVariant::~ScopedTypeVariant
~ScopedTypeVariant()
destructor
Definition: scopedtypevariant.h:108
VariableContext::m_scopes
std::vector< Scope > m_scopes
Definition: scopedtypevariant.h:238
ScopedTypeVariant::m_u
union ScopedTypeVariant::@3 m_u
VariableContext::m_globalScope
Scope m_globalScope
Definition: scopedtypevariant.h:237
ScopedTypeVariant::swap
friend void swap(ScopedTypeVariant &first, ScopedTypeVariant &second)
swap function
Definition: scopedtypevariant.h:116
qcstring.h
ScopedTypeVariant::ScopedTypeVariant
ScopedTypeVariant(ScopedTypeVariant &&stv) noexcept
move constructor
Definition: scopedtypevariant.h:97
end
DirIterator end(const DirIterator &) noexcept
Definition: dir.cpp:128
ScopedTypeVariant::setDummy
void setDummy()
Turn the variant into a Dummy type
Definition: scopedtypevariant.h:147
ScopedTypeVariant::setLocal
LocalDef * setLocal(const QCString &name)
Turn the variant into a Local type
Definition: scopedtypevariant.h:135
CallContext::pushScope
void pushScope(const QCString &name_, const QCString &type_)
Definition: scopedtypevariant.h:264
CallContext::CallContext
CallContext()
Definition: scopedtypevariant.h:255
CallContext::Ctx
Definition: scopedtypevariant.h:247
ScopedTypeVariant::Global
@ Global
Definition: scopedtypevariant.h:51
Definition::name
virtual QCString name() const =0
ScopedTypeVariant::localDef
LocalDef * localDef() const
Definition: scopedtypevariant.h:159
ScopedTypeVariant::ScopedTypeVariant
ScopedTypeVariant(const ScopedTypeVariant &stv)
copy constructor
Definition: scopedtypevariant.h:83
CallContext::Ctx::stv
ScopedTypeVariant stv
Definition: scopedtypevariant.h:252
ScopedTypeVariant::localDef
LocalDef * localDef
Definition: scopedtypevariant.h:168
LocalDef::baseClasses
std::vector< QCString > baseClasses() const
Definition: scopedtypevariant.h:44
LocalDef
Class representing a local class definition found while generating syntax highlighted code.
Definition: scopedtypevariant.h:27
ScopedTypeVariant::type
Variant type() const
Definition: scopedtypevariant.h:157
VariableContext
Definition: scopedtypevariant.h:179
ScopedTypeVariant::Local
@ Local
Definition: scopedtypevariant.h:52
VariableContext::findVariable
const ScopedTypeVariant * findVariable(const QCString &name)
Definition: scopedtypevariant.h:209
CallContext::popScope
void popScope(QCString &name_, QCString &type_)
Definition: scopedtypevariant.h:268
ScopedTypeVariant::Dummy
@ Dummy
Definition: scopedtypevariant.h:53
definition.h
CallContext
Represents the call context
Definition: scopedtypevariant.h:244
ScopedTypeVariant::ScopedTypeVariant
ScopedTypeVariant()
default constructor for creating a variant of type Dummy
Definition: scopedtypevariant.h:56
VariableContext::clearExceptGlobal
void clearExceptGlobal()
Definition: scopedtypevariant.h:200
ScopedTypeVariant::globalDef
const Definition * globalDef
Definition: scopedtypevariant.h:167
ScopedTypeVariant::setGlobal
void setGlobal(const Definition *def)
Turn the variant into a Global type
Definition: scopedtypevariant.h:124
ScopedTypeVariant::name
QCString name() const
Definition: scopedtypevariant.h:158
CallContext::Ctx::type
QCString type
Definition: scopedtypevariant.h:251
CallContext::m_stvList
std::vector< Ctx > m_stvList
Definition: scopedtypevariant.h:289
ScopedTypeVariant::m_variant
Variant m_variant
Definition: scopedtypevariant.h:163
CallContext::Ctx::name
QCString name
Definition: scopedtypevariant.h:250
LocalDef::m_baseClasses
std::vector< QCString > m_baseClasses
Definition: scopedtypevariant.h:46
ScopedTypeVariant::operator=
ScopedTypeVariant & operator=(ScopedTypeVariant stv)
assignment operator
Definition: scopedtypevariant.h:102
ScopedTypeVariant::ScopedTypeVariant
ScopedTypeVariant(const QCString &name)
constructor for creating a variant of type Local
Definition: scopedtypevariant.h:76
ScopedTypeVariant::m_name
QCString m_name
Definition: scopedtypevariant.h:164
VariableContext::Scope
std::unordered_map< std::string, ScopedTypeVariant > Scope
Definition: scopedtypevariant.h:182
CallContext::clear
void clear()
Definition: scopedtypevariant.h:278
QCString
This is an alternative implementation of QCString.
Definition: qcstring.h:108