This file is indexed.

/usr/include/elektra/kdbcontext.hpp is in libelektra-dev 0.8.14-5.

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
#ifndef ELEKTRA_KDBCONTEXT_HPP
#define ELEKTRA_KDBCONTEXT_HPP

#include <kdbconfig.h>

#include <kdbvalue.hpp>

#include <set>
#include <string>
#include <vector>
#include <memory>
#include <cassert>
#include <functional>
#include <unordered_map>

namespace kdb
{

class Subject
{
public:
	virtual ~Subject() = 0;
	typedef std::vector<std::string> Events;
	virtual void attachObserver(std::string const & event, ValueObserver &);
	// notify all given events
	virtual void notifyByEvents(Events const & events) const;
	// notify all events
	virtual void notifyAllEvents() const;

protected:
	Subject();

private:
	typedef std::set<ValueObserver::reference> ObserverSet;
	mutable std::unordered_map<
		std::string,
		ObserverSet> m_observers;
};

inline Subject::Subject()
{}

inline Subject::~Subject()
{}

inline void Subject::attachObserver(std::string const & event, ValueObserver & 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::notifyByEvents(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().updateContext();
	}
}

inline void Subject::notifyAllEvents() const
{
		Events events;
		for (auto & o: m_observers)
		{
			events.push_back(o.first);
		}
		notifyByEvents(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()
	{
	}

	virtual void execute(Command & c)
	{
		c();
	}

	/**
	 * Lookup value for a current active layer
	 *
	 * @param layer the name of the requested layer
	 * @return the 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
	}

	/**
	 * @return size of all current layers (to be used with operator[])
	 */
	size_t size() const
	{
		return m_active_layers.size();
	}

	/**
	 * 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, ValueObserver & observer)
	{
		evaluate(key_name, [&](std::string const & current_id, std::string &, bool){
			this->attachObserver(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.
	 * @return the evaluated string
	*/
	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;
	}

protected:
	// 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);
	}

	// needed for with
	void lazyActivateLayer(std::shared_ptr<Layer> const & 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
	}

	void clearAllLayer()
	{
		m_active_layers.clear();
	}

	// needed for global activation
	void activateLayer(std::shared_ptr<Layer> const & layer)
	{
		auto p = m_active_layers.emplace(std::make_pair(layer->id(), layer));
		if (!p.second)
		{
			p.first->second = layer; // update
		}

#if DEBUG && VERBOSE
		std::cout << "activate layer: " << 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>
	std::shared_ptr<Layer> activate(Args&&... args)
	{
		std::shared_ptr<Layer>layer = std::make_shared<T>(std::forward<Args>(args)...);
		activateLayer(layer);
		notifyByEvents({layer->id()});
		return layer;
	}

protected:
	template <typename T, typename... Args>
	void lazyDeactivate(Args&&... args)
	{
		std::shared_ptr<Layer>layer = std::make_shared<T>(std::forward<Args>(args)...);
		lazyDeactivateLayer(layer);
	}

	void lazyDeactivateLayer(std::shared_ptr<Layer> const & layer)
	{
		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
	}

	void deactivateLayer(std::shared_ptr<Layer> const & layer)
	{
		m_active_layers.erase(layer->id());

#if DEBUG && VERBOSE
		std::cout << "deactivate layer: " << layer->id() << std::endl;
#endif
	}

public:
	template <typename T, typename... Args>
	std::shared_ptr<Layer> deactivate(Args&&... args)
	{
		std::shared_ptr<Layer>layer = std::make_shared<T>(std::forward<Args>(args)...);
		deactivateLayer(layer);
		notifyByEvents({layer->id()});
		return layer;
	}

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);
		}
		notifyByEvents(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;
				}
			}
		}
		notifyByEvents(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;
};

template<typename T,
	typename PolicySetter1 = DefaultPolicyArgs,
	typename PolicySetter2 = DefaultPolicyArgs,
	typename PolicySetter3 = DefaultPolicyArgs,
	typename PolicySetter4 = DefaultPolicyArgs,
	typename PolicySetter5 = DefaultPolicyArgs
	>
using ContextualValue = Value <T,
	ContextPolicyIs<Context>,
	PolicySetter1,
	PolicySetter2,
	PolicySetter3,
	PolicySetter4,
	PolicySetter5
	>;

typedef ContextualValue<uint32_t>Integer;
typedef ContextualValue<bool>Boolean;
typedef ContextualValue<std::string>String;

} // namespace kdb

#endif