forked from david-grs/boost_multi_index_container
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.cc
149 lines (123 loc) · 3.39 KB
/
session.cc
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
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <string>
#include <chrono>
#include <map>
#include <iostream>
#include <vector>
#include <algorithm>
#include <experimental/string_view>
using namespace boost::multi_index;
struct session
{
session() =default;
session(const std::string& user, const std::string& script) :
user_name(user),
script_name(script)
{}
std::string id() const { return user_name.substr(0, 3) + script_name.substr(0, 3); }
std::experimental::string_view user_name_view() const { return user_name; }
std::experimental::string_view script_name_view() const { return script_name; }
std::string user_name;
std::string script_name;
std::chrono::time_point<std::chrono::system_clock> started_time;
};
struct by_name{};
void simple_index()
{
boost::multi_index_container<
session,
indexed_by<
hashed_unique<
tag<by_name>,
member<session, std::string, &session::user_name>
>
>
> sessions;
auto&& v = sessions.get<by_name>();
v.insert({"john", "foo.py"});
for (auto&& e : v)
std::cout << e.user_name << std::endl;
auto it = v.find("john");
std::cout << it->user_name << std::endl;
}
void composed_index()
{
boost::multi_index_container<
session,
indexed_by<
hashed_unique<
composite_key<
session,
const_mem_fun<session, std::experimental::string_view, &session::user_name_view>,
const_mem_fun<session, std::experimental::string_view, &session::script_name_view>
>,
composite_key_hash<
std::hash<std::experimental::string_view>,
std::hash<std::experimental::string_view>
>
>
>
> sessions;
sessions.insert({"john", "foo.py"});
auto it = sessions.find(boost::make_tuple("john", "foo.py"));
std::cout << it->user_name << std::endl;
}
void function_index()
{
boost::multi_index_container<
session,
indexed_by<
hashed_unique<
tag<by_name>,
const_mem_fun<session, std::string, &session::id>
>
>
> sessions;
auto&& v = sessions.get<by_name>();
v.insert({"john", "foo.py"});
auto it = v.find("johfoo");
std::cout << it->user_name << std::endl;
}
void map_multiple_index()
{
using full_name = std::pair<std::string, std::string>;
std::map<full_name, session> m;
m[{"john", "foo.py"}] = session{};
for (auto&& p : m)
std::cout << p.first.first << " " << p.first.second << std::endl;
auto it = m.find(std::make_pair("john", "foo.py"));
std::cout << it->second.user_name << std::endl;
}
void bla()
{
struct session
{
std::string user_name;
std::string script_name;
};
boost::multi_index_container<
session,
indexed_by<
hashed_unique<
tag<by_name>,
composite_key<
session,
member<session, std::string, &session::user_name>,
member<session, std::string, &session::script_name>
>
>
>
> sessions;
}
int main()
{
map_multiple_index();
simple_index();
composed_index();
function_index();
return 0;
}