/usr/include/elektra/contextual.hpp is in libelektra-dev 0.8.7-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 | #ifndef ELEKTRA_CONTEXTUAL_HPP
#define ELEKTRA_CONTEXTUAL_HPP
#include <set>
#include <map>
#include <vector>
#include <memory>
#include <cassert>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <algorithm>
#include <functional>
#include <unordered_map>
#include <keyset.hpp>
#define ELEKTRA_INLINE __attribute__((noinline))
namespace kdb
{
class Layer
{
public:
virtual std::string id() const = 0;
virtual std::string operator()() const = 0;
};
class Observer
{
public:
virtual ~Observer() = 0;
virtual void update() const = 0;
typedef std::reference_wrapper<Observer> reference;
};
bool operator <(Observer const & lhs, Observer const & rhs)
{
return &lhs < &rhs;
}
inline Observer::~Observer()
{}
class Subject
{
public:
virtual ~Subject() = 0;
typedef std::vector<std::string> Events;
virtual void attach(std::string const & event, Observer &);
// notify all given events
virtual void notify(Events const & events) const;
// notify all events
virtual void notify() const;
protected:
Subject();
private:
typedef std::set<Observer::reference> ObserverSet;
mutable std::unordered_map<
std::string,
ObserverSet> m_observers;
};
inline Subject::Subject()
{}
inline Subject::~Subject()
{}
inline void Subject::attach(std::string const & event, Observer & observer)
{
auto it = m_observers.find(event);
if (it == m_observers.end())
{
// add first observer for that event
// (creating a new vector)
ObserverSet & os = m_observers[event];
os.insert(std::ref(observer));
}
else
{
it->second.insert(std::ref(observer));
}
}
inline void Subject::notify(Events const & events) const
{
ObserverSet os;
for (auto & e: events)
{
auto it = m_observers.find(e);
if (it != m_observers.end())
{
for (auto & o: it->second)
{
os.insert(o); // (discarding duplicates)
}
}
#if DEBUG && VERBOSE
else
{
std::cout << "Trying to notify " << e << " but event does not exist" << std::endl;
}
#endif
}
// now call any observer exactly once
for (auto & o: os)
{
o.get().update();
}
}
inline void Subject::notify() const
{
Events events;
for (auto & o: m_observers)
{
events.push_back(o.first);
}
notify(events);
}
/**
* @brief Provides a context for configuration
*
* Is a subject for observers.
*
* Holds currently active layers and allows
* global/scoped activation of layers.
*/
class Context : public Subject
{
public:
Context() :
m_active_layers()
{
}
/**
* Lookup value for a current active layer
*
* @param layer the name of the requested layer
*/
std::string operator[](std::string const & layer) const
{
auto f = m_active_layers.find(layer);
if (f != m_active_layers.end())
{
assert(f->second && "no null pointers in active_layers");
return (*f->second)();
}
return ""; // this line is surprisingly expensive
}
/**
* Attach observer using to all events given by
* its specification (name)
*
* @param key_name the name with placeholders to be used for attaching
* @param observer the observer to attach to
*/
void attachByName(std::string const & key_name, Observer & observer)
{
evaluate(key_name, [&](std::string const & current_id, std::string &, bool){
this->attach(current_id, observer);
return false;
});
}
/**
* Evaluate a specification (name) and return
* a key name under current context
*
* @param key_name the name with placeholders to be evaluated
*/
std::string evaluate(std::string const & key_name) const
{
return evaluate(key_name, [&](std::string const & current_id, std::string & ret, bool in_group){
auto f = m_active_layers.find(current_id);
bool left_group = true;
if (f != m_active_layers.end())
{
assert(f->second && "no null pointers in active_layers");
std::string r = (*f->second)();
if (!r.empty())
{
if (in_group)
{
ret += "%";
}
ret += r;
left_group = false;
}
else if (!in_group)
{
ret += "%";
}
}
else if (!in_group)
{
ret += "%";
}
return left_group;
});
}
/**
* Evaluate specification with this context.
*
* @param key_name the keyname with placeholders to evaluate
* @param on_layer the function to be called for every
* placeholder found
*
* @par on_layer is called for every layer in the
* specification.
*/
std::string evaluate(std::string const & key_name, std::function<bool(std::string const &, std::string &, bool in_group)> const & on_layer) const
{
size_t const & s = key_name.size();
std::string ret;
std::string current_id;
bool capture_id = false; // we are currently within a % block (group or single layer)
bool left_group = false; // needed to omit layers that do not matter in a group anymore
bool is_in_group = false;
// heuristic how much too allocate
ret.reserve(s*2);
current_id.reserve(32);
for (std::string::size_type i=0; i<s; ++i)
{
if (key_name[i] == '%')
{
if (capture_id)
{
// finish capturing
if (!left_group)
{
on_layer(current_id, ret, is_in_group);
}
current_id.clear();
capture_id = false;
}
else
{
// start capturing
capture_id = true;
left_group = false;
is_in_group = false;
}
}
else if (capture_id && key_name[i] == ' ' && !left_group)
{
// found group separator in active
// group
left_group = on_layer(current_id, ret, true);
if (!is_in_group && left_group)
{
ret += "%"; // empty groups
}
else
{
is_in_group = true;
}
current_id.clear();
}
else // non % character
{
if (capture_id)
{
current_id += key_name[i];
}
else
{
ret += key_name[i];
}
}
}
assert (!capture_id && "number of % incorrect");
return ret;
}
private:
// activates layer, records it, but does not notify
template <typename T, typename... Args>
void lazyActivate(Args&&... args)
{
std::shared_ptr<Layer>layer = std::make_shared<T>(std::forward<Args>(args)...);
lazyActivateLayer(layer);
}
void lazyActivateLayer(std::shared_ptr<Layer>&layer)
{
std::string const & id = layer->id(); // optimisation
auto p = m_active_layers.emplace(std::make_pair(id, layer));
if (!p.second)
{
m_with_stack.push_back(*p.first);
p.first->second = layer; // update
}
else
{
// no layer was not active before, remember that
m_with_stack.push_back(std::make_pair(id, std::shared_ptr<Layer>()));
}
#if DEBUG && VERBOSE
std::cout << "lazy activate layer: " << id << std::endl;
#endif
}
public:
/**
* @brief Globally activate the layer
*
* @tparam T the layer to activate
* @tparam Args the types for the arguments to pass to layer construction
* @param args the arguments to pass to layer construction
*/
template <typename T, typename... Args>
void activate(Args&&... args)
{
std::shared_ptr<Layer>layer = std::make_shared<T>(std::forward<Args>(args)...);
auto p = m_active_layers.emplace(std::make_pair(layer->id(), layer));
if (!p.second)
{
p.first->second = layer; // update
}
notify({layer->id()});
#if DEBUG && VERBOSE
std::cout << "activate layer: " << layer->id() << std::endl;
#endif
}
private:
template <typename T, typename... Args>
void lazyDeactivate(Args&&... args)
{
std::shared_ptr<Layer>layer = std::make_shared<T>(std::forward<Args>(args)...);
auto p = m_active_layers.find(layer->id());
if (p != m_active_layers.end())
{
m_with_stack.push_back(*p);
m_active_layers.erase(p);
}
// else: deactivate whats not there:
// nothing to do!
#if DEBUG && VERBOSE
std::cout << "lazy deactivate layer: " << layer->id() << std::endl;
#endif
}
public:
template <typename T, typename... Args>
void deactivate(Args&&... args)
{
std::shared_ptr<Layer>layer = std::make_shared<T>(std::forward<Args>(args)...);
m_active_layers.erase(layer->id());
#if DEBUG && VERBOSE
std::cout << "deactivate layer: " << layer->id() << std::endl;
#endif
notify({layer->id()});
}
public:
Context & withl(std::shared_ptr<Layer>&l)
{
// build up staple (until function is executed)
lazyActivateLayer(l);
return *this;
}
template <typename T, typename... Args>
Context & with(Args&&... args)
{
// build up staple (until function is executed)
lazyActivate<T, Args...>(std::forward<Args>(args)...);
return *this;
}
template <typename T, typename... Args>
Context & without(Args&&... args)
{
// build up staple (until function is executed)
lazyDeactivate<T, Args...>(std::forward<Args>(args)...);
return *this;
}
Context & operator()(std::function<void()> const & f)
{
execHelper(f);
return *this;
}
Context & withl(std::shared_ptr<Layer>&l, std::function<void()> const & f)
{
lazyActivateLayer(l);
execHelper(f);
return *this;
}
private:
typedef std::vector<std::pair<std::string, std::shared_ptr<Layer>>> WithStack;
void execHelper(std::function<void()> const & f)
{
WithStack with_stack = m_with_stack;
m_with_stack.clear(); // allow with to be called recursively
// last step, now lets really activate
Subject::Events to_notify;
for (auto & s: with_stack)
{
to_notify.push_back(s.first);
}
notify(to_notify);
// now do the function call,
// keep roll back information on the stack
f();
// now roll everything back before all those with()
// and without()
while(!with_stack.empty())
{
auto s = with_stack.back();
with_stack.pop_back();
if (!s.second)
{
// do not add null pointer
// but erase layer instead
m_active_layers.erase(s.first);
}
else
{
auto it = m_active_layers.insert(s);
if (!it.second)
{
it.first->second = s.second;
}
}
}
notify(to_notify);
}
std::unordered_map<std::string, std::shared_ptr<Layer>> m_active_layers;
// the with stack holds all layers that were
// changed in the current .with().with()
// invocation chain
WithStack m_with_stack;
};
// standard types
template<typename T>
class ContextualValue : public Observer
{
public:
typedef T type;
// not to be constructed yourself
ContextualValue<T>(KeySet & ks, Context & context_, kdb::Key meta) :
m_cache(),
m_ks(ks),
m_context(context_),
m_meta(meta),
m_evaluated_name(m_context.evaluate(m_meta.getString()))
{
assert(m_meta.getString()[0] == '/');
syncCache(); // read what we have in our context
m_context.attachByName(m_meta.getString(), *this);
}
ContextualValue<T>(ContextualValue<T> const & other, KeySet & ks) :
m_cache(other.m_cache),
m_ks(ks),
m_context(other.m_context),
m_meta(other.m_meta),
m_evaluated_name(other.m_evaluated_name)
{
assert(m_meta.getString()[0] == '/');
// cache already in sync
// attach copy, too:
m_context.attachByName(m_meta.getString(), *this);
}
public:
ContextualValue<T> const & operator= (type n)
{
m_cache = n;
return *this;
}
type operator ++()
{
return ++m_cache;
}
type operator ++(int)
{
return m_cache++;
}
operator type() const
{
return m_cache;
}
bool operator == (ContextualValue<T> const & other) const
{
return m_cache == other.m_cache ;
}
type getDefault() const
{
return m_meta.getMeta<type>("default");
}
bool isEmpty() const
{
return !m_ks.lookup(m_meta.getString(), 0);
}
/// We allow manipulation of context for const
/// objects
Context & context() const
{
return const_cast<Context&>(m_context);
}
/// Do not inline so that we can use it for debugging
ELEKTRA_INLINE std::string const & getEvaluatedName() const
{
return m_evaluated_name;
}
// keyset to cache
void syncCache() const
{
kdb::Key found = m_ks.lookup(m_evaluated_name, 0);
if (found)
{
m_cache = found.get<type>();
}
else
{
m_cache = getDefault();
}
/*
m_context_changed = false;
m_atomic_context_changed = false;
m_volatile_context_changed = false;
*/
#if DEBUG && VERBOSE
std::cout << "got name: " << m_evaluated_name << " to " << m_cache << std::endl;
#endif
}
// cache to keyset
void syncKeySet() const
{
#if DEBUG && VERBOSE
std::cout << "set name: " << m_evaluated_name << " to " << m_cache << std::endl;
#endif
kdb::Key found = m_ks.lookup(m_evaluated_name, 0);
if(!found)
{
kdb::Key k("user/"+m_evaluated_name, KEY_END);
k.set<type>(m_cache);
m_ks.append(k);
}
else
{
found.set<type>(m_cache);
}
}
private:
virtual void update() const
{
std::string evaluated_name = m_context.evaluate(m_meta.getString());
#if DEBUG && VERBOSE
std::cout << "update " << evaluated_name << " from " << m_evaluated_name << std::endl;
#endif
if (evaluated_name != m_evaluated_name)
{
syncKeySet(); // flush out what currently is in cache
m_evaluated_name = evaluated_name;
syncCache(); // read what we have under new context
}
}
private:
mutable type m_cache;
KeySet & m_ks;
Context & m_context;
Key m_meta;
mutable std::string m_evaluated_name;
};
typedef ContextualValue<uint32_t>Integer;
typedef ContextualValue<bool>Boolean;
typedef ContextualValue<std::string>String;
}
#endif
|