Doxygen
Loading...
Searching...
No Matches
VariableContext Class Reference

#include <src/scopedtypevariant.h>

Public Types

using Scope = std::unordered_map<std::string,ScopedTypeVariant>

Public Member Functions

void pushScope ()
void popScope ()
void clear ()
void clearExceptGlobal ()
void addVariable (const DString &name, ScopedTypeVariant stv)
const ScopedTypeVariantfindVariable (const DString &name)
bool atGlobalScope () const

Private Attributes

Scope m_globalScope
std::vector< Scopem_scopes

Detailed Description

Represents a stack of variable to class mappings as found in the code. Each scope is enclosed in pushScope() and popScope() calls. Variables are added by calling addVariables() and one can search for variable using findVariable().

Definition at line 75 of file scopedtypevariant.h.

Member Typedef Documentation

◆ Scope

using VariableContext::Scope = std::unordered_map<std::string,ScopedTypeVariant>

Definition at line 78 of file scopedtypevariant.h.

Member Function Documentation

◆ addVariable()

void VariableContext::addVariable ( const DString & name,
ScopedTypeVariant stv )
inline

Definition at line 100 of file scopedtypevariant.h.

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 }
const std::string & str() const
Definition dstring.h:649
std::vector< Scope > m_scopes
std::unordered_map< std::string, ScopedTypeVariant > Scope

References m_globalScope, m_scopes, and DString::str().

◆ atGlobalScope()

bool VariableContext::atGlobalScope ( ) const
inline

Definition at line 130 of file scopedtypevariant.h.

130{ return m_scopes.empty(); }

References m_scopes.

◆ clear()

void VariableContext::clear ( )
inline

Definition at line 91 of file scopedtypevariant.h.

92 {
93 m_scopes.clear();
94 m_globalScope.clear();
95 }

References m_globalScope, and m_scopes.

◆ clearExceptGlobal()

void VariableContext::clearExceptGlobal ( )
inline

Definition at line 96 of file scopedtypevariant.h.

97 {
98 m_scopes.clear();
99 }

References m_scopes.

◆ findVariable()

const ScopedTypeVariant * VariableContext::findVariable ( const DString & name)
inline

Definition at line 105 of file scopedtypevariant.h.

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 }
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:152

References DString::empty(), m_globalScope, m_scopes, and DString::str().

◆ popScope()

void VariableContext::popScope ( )
inline

Definition at line 84 of file scopedtypevariant.h.

85 {
86 if (!m_scopes.empty())
87 {
88 m_scopes.pop_back();
89 }
90 }

References m_scopes.

◆ pushScope()

void VariableContext::pushScope ( )
inline

Definition at line 80 of file scopedtypevariant.h.

81 {
82 m_scopes.emplace_back();
83 }

References m_scopes.

Member Data Documentation

◆ m_globalScope

Scope VariableContext::m_globalScope
private

Definition at line 133 of file scopedtypevariant.h.

Referenced by addVariable(), clear(), and findVariable().

◆ m_scopes

std::vector<Scope> VariableContext::m_scopes
private

The documentation for this class was generated from the following file: