KSeExpr  4.0.4.0
Context.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2019 Disney Enterprises, Inc.
2 // SPDX-License-Identifier: LicenseRef-Apache-2.0
3 // SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
6 #pragma once
7 
8 #include <string>
9 #include <unordered_map>
10 
11 
12 namespace KSeExpr
13 {
14 class Context
15 {
16 public:
18  bool lookupParameter(const std::string &parameterName, std::string &value) const
19  {
20  auto it = _parameters.find(parameterName);
21  if (it != _parameters.end()) {
22  value = it->second;
23  return true;
24  } else if (_parent)
25  return _parent->lookupParameter(parameterName, value);
26  else
27  return false;
28  }
30  void setParameter(const std::string &parameterName, const std::string &value);
32  Context *createChildContext() const;
33 
34  // Parent access uses pointers as it is acceptable to set/get a NULL parent
35  void setParent(const Context *context)
36  {
37  _parent = context;
38  }
39  const Context *getParent() const
40  {
41  return _parent;
42  }
43 
44  bool hasContext(const Context *context) const
45  {
46  if (this == context)
47  return true;
48  if (_parent)
49  return _parent->hasContext(context);
50  return false;
51  }
52 
54  static Context &global();
55 
56  ~Context() = default;
57 
60  Context(const Context &) = delete;
61  Context(Context &&) = delete;
62  Context &operator=(const Context &) = delete;
63  Context &operator=(Context &&) = delete;
64 
65 private:
66  Context(const Context *parent);
67 
69  const Context *_parent{nullptr};
70 
71  using ParameterMap = std::unordered_map<std::string, std::string>;
74 };
75 } // namespace KSeExpr
~Context()=default
static Context & global()
The global default context of the seexpr.
Definition: Context.cpp:25
Context & operator=(Context &&)=delete
std::unordered_map< std::string, std::string > ParameterMap
Definition: Context.h:71
const Context * getParent() const
Definition: Context.h:39
void setParent(const Context *context)
Definition: Context.h:35
Context(Context &&)=delete
bool hasContext(const Context *context) const
Definition: Context.h:44
Context & operator=(const Context &)=delete
ParameterMap _parameters
Attribute/value pairs.
Definition: Context.h:73
bool lookupParameter(const std::string &parameterName, std::string &value) const
Lookup a Context parameter by name.
Definition: Context.h:18
Context(const Context &)=delete
Context * createChildContext() const
Create a context that is a child of this context.
Definition: Context.cpp:20
const Context * _parent
The parent scope.
Definition: Context.h:69
void setParameter(const std::string &parameterName, const std::string &value)
Set a parameter. NOTE: this must be done when no threads are accessing lookupParameter for safety.
Definition: Context.cpp:15