Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
berryEvaluationContext.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 BlueBerry Platform
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "berryEvaluationContext.h"
18 
19 namespace berry {
20 
22  const Object::ConstPointer& defaultVariable)
23  : fParent(parent), fDefaultVariable(defaultVariable), fAllowPluginActivation(-1)
24 {
25  poco_assert(defaultVariable != 0);
26 }
27 
29  const Object::ConstPointer& defaultVariable,
30  const std::vector<IVariableResolver*>& resolvers)
31  : fParent(parent), fDefaultVariable(defaultVariable),
32  fVariableResolvers(resolvers), fAllowPluginActivation(-1)
33 {
34  poco_assert(defaultVariable != 0);
35  poco_assert(resolvers.size() != 0);
36 }
37 
39 {
40  return fParent;
41 }
42 
44 {
45  if (fParent == nullptr)
46  return const_cast<EvaluationContext*>(this);
47  return fParent->GetRoot();
48 }
49 
51 {
52  return fDefaultVariable;
53 }
54 
56 {
57  fAllowPluginActivation= value ? 1 : 0;
58 }
59 
61 {
62  if (fAllowPluginActivation < 0)
63  {
64  if (fParent)
65  {
66  return fParent->GetAllowPluginActivation();
67  }
68  return false;
69  }
70  return fAllowPluginActivation;
71 }
72 
73 void
74 EvaluationContext::AddVariable(const QString &name, const Object::ConstPointer& value)
75 {
76  poco_assert(name.size() != 0);
77  poco_assert(value.IsNotNull());
78 
79  fVariables[name] = value;
80 }
81 
84 {
85  poco_assert(name.size() != 0);
86 
87  Object::ConstPointer elem(fVariables[name]);
88  fVariables.remove(name);
89  return elem;
90 }
91 
93 EvaluationContext::GetVariable(const QString& name) const
94 {
95  poco_assert(name.size() != 0);
96 
97  Object::ConstPointer result;
98 
99  QHash<QString, Object::ConstPointer>::const_iterator iter(fVariables.find(name));
100  if (iter != fVariables.end())
101  {
102  result = iter.value();
103  }
104 
105  if (!result.IsNull())
106  return result;
107 
108  if (fParent != nullptr)
109  return fParent->GetVariable(name);
110 
111  return result;
112 }
113 
115 EvaluationContext::ResolveVariable(const QString &name, const QList<Object::Pointer>& args) const
116 {
117  if (fVariableResolvers.size() > 0) {
118  for (unsigned int i= 0; i < fVariableResolvers.size(); ++i) {
119  IVariableResolver* resolver = fVariableResolvers[i];
120  Object::Pointer variable(resolver->Resolve(name, args));
121  if (!variable.IsNull())
122  return variable;
123  }
124  }
125 
126  if (fParent != nullptr)
127  return fParent->ResolveVariable(name, args);
128 
129  return Object::Pointer();
130 }
131 
132 }
virtual Object::Pointer Resolve(const QString &name, const QList< Object::Pointer > &args)=0
IEvaluationContext * GetRoot() const override
Object::ConstPointer ResolveVariable(const QString &name, const QList< Object::Pointer > &args) const override
bool GetAllowPluginActivation() const override
virtual IEvaluationContext * GetRoot() const =0
IEvaluationContext * GetParent() const override
Object::ConstPointer GetVariable(const QString &name) const override
berry::SmartPointer< Self > Pointer
Definition: berryObject.h:88
void SetAllowPluginActivation(bool value) override
virtual bool GetAllowPluginActivation() const =0
EvaluationContext(IEvaluationContext *parent, const Object::ConstPointer &defaultVariable)
Object::ConstPointer GetDefaultVariable() const override
void AddVariable(const QString &name, const Object::ConstPointer &value) override
virtual Object::ConstPointer ResolveVariable(const QString &name, const QList< Object::Pointer > &args) const =0
Object::ConstPointer RemoveVariable(const QString &name) override
virtual Object::ConstPointer GetVariable(const QString &name) const =0