MADARA  3.4.1
IntegerStaged.cpp
Go to the documentation of this file.
1 #include "IntegerStaged.h"
3 
5  const KnowledgeUpdateSettings& settings)
6  : BaseContainer("", settings), context_(0), value_(0), has_changed_(false)
7 {
8 }
9 
11  const std::string& name, KnowledgeBase& knowledge,
12  const KnowledgeUpdateSettings& settings)
13  : BaseContainer(name, settings),
14  context_(&(knowledge.get_context())),
15  has_changed_(false)
16 {
17  variable_ = knowledge.get_ref(name, settings_);
18  value_ = knowledge.get(variable_).to_integer();
19 }
20 
22  const std::string& name, Variables& knowledge,
23  const KnowledgeUpdateSettings& settings)
24  : BaseContainer(name, settings),
25  context_(knowledge.get_context()),
26  has_changed_(false)
27 {
28  variable_ = knowledge.get_ref(name, settings_);
29  value_ = knowledge.get(variable_).to_integer();
30 }
31 
33  const std::string& name, KnowledgeBase& knowledge, type value,
34  const KnowledgeUpdateSettings& settings)
35  : BaseContainer(name, settings),
36  context_(&(knowledge.get_context())),
37  value_(value),
38  has_changed_(false)
39 {
40  variable_ = knowledge.get_ref(name);
41  context_->set(variable_, value, settings);
42 }
43 
45  const std::string& name, Variables& knowledge, type value,
46  const KnowledgeUpdateSettings& settings)
47  : BaseContainer(name, settings),
48  context_(knowledge.get_context()),
49  value_(value),
50  has_changed_(false)
51 {
52  variable_ = knowledge.get_ref(name);
53  context_->set(variable_, value, settings);
54 }
55 
57  const IntegerStaged& rhs)
58  : BaseContainer(rhs),
59  context_(rhs.context_),
60  variable_(rhs.variable_),
61  value_(rhs.value_)
62 {
63 }
64 
66 {
67  write();
68 }
69 
71 {
72  modify();
73 }
74 
76 {
77  return get_debug_info();
78 }
79 
82 {
83  if (context_ && other.context_)
84  {
85  std::lock(*context_, *other.context_, mutex_, other.mutex_);
86 
87  ContextGuard context_guard(*context_, std::adopt_lock);
88  ContextGuard other_context_guard(*other.context_, std::adopt_lock);
89  MADARA_GUARD_TYPE guard(mutex_, std::adopt_lock),
90  guard2(other.mutex_, std::adopt_lock);
91 
92  type temp = *other;
93  other = **this;
94  *this = temp;
95  }
96 }
97 
99 {
100  return is_true();
101 }
102 
104 {
105  return is_false();
106 }
const ThreadSafeContext * context_
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:24
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
Settings for applying knowledge updates.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
This class is an abstract base class for all containers.
Definition: BaseContainer.h:34
KnowledgeUpdateSettings settings_
Settings for modifications.
MADARA_LOCK_TYPE mutex_
guard for access and changes
Stages an integer value to and from the knowledge base.
Definition: IntegerStaged.h:39
ThreadSafeContext * context_
Variable context that we are modifying.
IntegerStaged(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true.
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
Definition: IntegerStaged.h:42
virtual void modify_(void)
Polymorphic modify method used by collection containers.
virtual bool is_false_(void) const
Polymorphic is false method which can be used to determine if at least one value in the container is ...
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
VariableReference variable_
Variable reference.
void exchange(IntegerStaged &other)
Exchanges the integer at this location with the integer at another location.
constexpr string_t string
Provides functions and classes for the distributed knowledge base.