Doxygen
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
25class Definition;
26
27//! Class representing a local class definition found while
28//! generating syntax highlighted code.
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 */
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())) {}
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:
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 */
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 }
97 {
98 m_scopes.clear();
99 }
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 }
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:
134 std::vector<Scope> m_scopes;
135};
136
137//-----------------------------------------------------------------------------
138
139/** Represents the call context */
141{
142 public:
143 struct Ctx
144 {
145 Ctx(const DString &name_,const DString &type_, int bracketCount_) : name(name_), type(type_), bracketCount(bracketCount_) {}
150 };
151
153 {
154 clear();
155 }
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 }
182 {
183 return m_stvList.back().stv;
184 }
185
186 private:
187 std::vector<Ctx> m_stvList;
188};
189
190
191#endif
const ScopedTypeVariant getScope() const
void pushScope(const DString &name_, const DString &type_, int bracketCount_)
void setScope(const ScopedTypeVariant &stv)
std::vector< Ctx > m_stvList
void popScope(DString &name_, DString &type_, int &bracketCount_)
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:84
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:148
const std::string & str() const
Definition dstring.h:645
The common base class of all entity definitions found in the sources.
Definition definition.h:77
std::vector< DString > baseClasses() const
void insertBaseClass(const DString &name)
std::vector< DString > m_baseClasses
DString name() const
const LocalDef * localDef() const
const Definition * GlobalDef
std::variant< DummyDef, LocalDef, GlobalDef > Variant
const Definition * globalDef() const
ScopedTypeVariant(GlobalDef d=nullptr)
ScopedTypeVariant(const DString &name)
bool atGlobalScope() const
void addVariable(const DString &name, ScopedTypeVariant stv)
std::vector< Scope > m_scopes
std::unordered_map< std::string, ScopedTypeVariant > Scope
const ScopedTypeVariant * findVariable(const DString &name)
ScopedTypeVariant stv
Ctx(const DString &name_, const DString &type_, int bracketCount_)