3 #ifndef INCL_MADARA_RCW_TRANSACTION_H 4 #define INCL_MADARA_RCW_TRANSACTION_H 17 #include <type_traits> 18 #include <initializer_list> 22 #include "madara/MADARA_export.h" 49 template<
class T,
class K,
bool Const =
false,
50 bool RD =
true,
bool WR =
true,
51 bool Prefix =
false,
bool Init =
false>
66 template<
class B,
class I,
class T,
class dummy =
void>
75 template<
class B,
class I,
class T>
77 typename
std::enable_if<
78 supports_get_value<T>::value &&
79 supports_indexed_get_value<T>::value &&
80 supports_size<T>::value>::type>
82 typedef typename std::decay<decltype(get_value(std::declval<T>()[0]))>::type
V;
84 I
init(std::initializer_list<V> list)
const 86 const B &
self =
static_cast<const B &
>(*this);
87 *
self.tracked_ = list;
118 template<
class T,
class K,
bool Const,
119 bool RD,
bool WR,
bool Prefix,
bool Init>
121 Builder<T, K, Const, RD, WR, Prefix, Init>,
122 Builder<T, K, Const, RD, WR, Prefix, true>, T>
125 typedef typename std::conditional<Const,
const Transaction *,
127 typedef decltype(*(trans_type)
nullptr) trans_ref_type;
128 const trans_type trans_;
131 static const
bool rd_ = RD;
132 static const
bool wr_ = WR;
133 static const
bool prefix_ = Prefix;
134 static const
bool init_ = Init;
137 : trans_(&trans), key_(key), tracked_(&val) {}
139 template<
bool R,
bool W,
bool P,
bool I>
141 : trans_(o.trans_), key_(o.key_), tracked_(o.tracked_) {}
153 static_assert(!Const,
"Can't use init() on build() of const Transaction");
159 prefix_type
prefix()
const {
return *
this; }
162 ro_type
ro()
const {
return *
this; }
170 wo_type
wo()
const {
return *
this; }
182 *tracked_ = std::forward<V>(val);
195 friend class Transaction;
198 template<
class T,
class K,
bool Const,
bool RD,
bool WR,
208 template<
class T,
class K>
215 template<
class T,
class S>
235 template<
class T,
bool RD,
bool WR,
bool Init>
238 std::unique_ptr<PrefixTracker<T, VariableReference, RD, WR>>
241 p->PrefixTracker<T, VariableReference, RD, WR>::force_push(
kb_);
246 template<
class T,
bool RD,
bool WR,
bool Init>
250 std::unique_ptr<Tracker<T, VariableReference, RD, WR>>
253 p->Tracker<T, VariableReference, RD, WR>::force_push(
kb_);
258 template<
class T,
bool RD,
bool WR>
261 std::unique_ptr<PrefixTracker<T, VariableReference, RD, WR>>
266 template<
class T,
bool RD,
bool WR>
270 std::unique_ptr<Tracker<T, VariableReference, RD, WR>>
280 template<
class T,
class K>
281 void add(K &&key, T &val)
const 283 build(std::forward<K>(key), val).add();
291 template<
class T,
class K>
294 build(std::forward<K>(key), val).init().add();
301 template<
class T,
class K>
304 build(std::forward<K>(prefix), val).prefix().add();
312 template<
class T,
class K>
315 build(std::forward<K>(prefix), val).prefix().init().add();
323 bool remove(
const char *name)
const 326 return strcmp(name, cur->get_name()) == 0;
343 return remove(name.c_str());
352 bool remove(
const T *var)
const 355 return (
void *)var == cur->get_tracked();
376 #endif // INCL_MADARA_RCW_TRANSACTION_H Builder< T, const char * > build(K &&key, T &val)
Create a new mapping, using the Builder interface.
init_type init() const
If used, the mapping will push the current value of the variable into the knowledge base...
ro_type ro() const
The mapping will only read from the knowledge base, not write back.
void add_prefix_init(K &&prefix, T &val)
Add a new prefix-based mapping for MADARA variable and ref .
Manages a Read-Compute-Write cycle for registered variables.
Builder object returned by build()
void add_tracker(tracker_ptr t) const
Add a tracker object directly.
void swap_remove(iter_type i) const
KnowledgeBase kb_
The knowledge base to push to and pull from.
void add() const
Finish configuring this Builder, and add the new mapping.
void add(const Builder< T, const char * > &b)
Add a mapping from a Builder object.
Builder< T, K, Const, false, true, Prefix, Init > wo_type
void add(K &&key, T &val) const
Add a new default mapping for MADARA variable and reference .
Optimized reference to a variable within the knowledge base.
std::conditional< Const, const Transaction *, Transaction * >::type trans_type
std::decay< decltype(get_value(std::declval< T >)[0]))>::type V
Builder< T, K, Const, RD, WR, Prefix, true > init_type
A thread-safe guard for a context or knowledge base.
wo_type writeonly() const
The mapping will only write to the knowledge base.
wo_type wo() const
The mapping will only write to the knowledge base.
void add_prefix(K &&prefix, T &val) const
Add a new prefix-based mapping for MADARA variable and ref .
std::unique_ptr< BaseTracker > tracker_ptr
This class provides a distributed knowledge base to users.
Transaction(KnowledgeBase kb)
The constructor.
trackers_vec trackers_
Stores all registered trackers.
Helper type to enable initialization using initializer_list Fallback implementation for types which d...
Builder< T, K, Const, RD, WR, true, Init > prefix_type
Tracker which puts variable values into a single KnowledgeRecord.
prefix_type prefix() const
If used, the name given in build() will be used as a prefix, as apropriate for the variable type...
Tracker that puts values into a multiple prefixed KnowledgeRecords.
I init(std::initializer_list< V > list) const
void add_init(K &&key, T &val)
Add a new mapping for MADARA variable and reference .
trackers_vec::iterator iter_type
Provides functions and classes for the distributed knowledge base.
Builder(const Builder< T, K, Const, R, W, P, I > &o)
init_type init(V &&val) const
As init(), but first set the variable to .
void pull(void)
Call to update values of registered variables from the knowledge base.
Copyright (c) 2015 Carnegie Mellon University.
ro_type readonly() const
The mapping will only read from the knowledge base, not write back Synonym for.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.
Builder< T, K, Const, true, false, Prefix, Init > ro_type
void push(void)
Call to push all values of registered variables into the knowledge base.
std::vector< tracker_ptr > trackers_vec
Type of a vector which stores trackers.