forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisit_context.cpp
116 lines (102 loc) · 2.33 KB
/
visit_context.cpp
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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "visit_context.hpp"
#include <kdb.hpp>
#include <iostream>
class CountryAustriaLayer : public kdb::Layer
{
public:
std::string id() const { return "country"; }
std::string operator()() const { return "Austria"; }
};
class LanguageGermanLayer : public kdb::Layer
{
public:
std::string id() const override
{
return "language";
}
std::string operator()() const override
{
return "german";
}
};
class ProfileLayer : public kdb::Layer
{
public:
ProfileLayer(kdb::visit::Profile const & profile) :
m_profile(profile) {}
std::string id() const { return "profile"; }
std::string operator()() const { return m_profile; }
private:
kdb::visit::Profile const & m_profile;
};
//{end}
/*
//{layer/profileOld}
class ProfileLayer : public kdb::Layer
{
public:
ProfileLayer(kdb::String const & profile) :
m_profile(profile) {}
std::string id() const override
{ return "profile"; }
std::string operator()() const override
{ return m_profile; }
private:
std::string m_profile;
};
//{end}
*/
// application: e.g. git-pull, git-push
//{application}
class MainApplicationLayer : public kdb::Layer
{
public:
std::string id() const override
{ return "application"; }
std::string operator()() const override
{ return "main"; }
};
void visit(kdb::visit::Person & p) {
p.context().with<CountryAustriaLayer>()
.with<LanguageGermanLayer>()([&] {
std::cout << "visit " << ++p.visits << " in "
<< p.context()["country"] << ": "
<< p.greeting << std::endl; });
std::cout << p.greeting << std::endl;
}
int main()
{
using namespace kdb;
KDB kdb;
KeySet ks;
Context c;
// some predefined values (for convenience):
ks.append(Key("user/visit/%/%/%/person/greeting",
KEY_VALUE,
"Tag",
KEY_END));
ks.append(Key("user/visit/german/Austria/%/person/greeting",
KEY_VALUE,
"Servus",
KEY_END));
ks.append(Key("user/visit/german/switzerland/%/person/greeting",
KEY_VALUE,
"Grüezi",
KEY_END));
KeySet ks2;
kdb.get(ks2, "/visit");
// overwrite them if something is available in config files:
ks.append(ks2);
Parameters par(ks,c);
c.activate<MainApplicationLayer>();
c.activate<ProfileLayer>(par.profile);
for (int i=0; i<3; ++i) ::visit(par.visit.person);
return 0;
}