forked from svarshavchik/vera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc_loaderfwd.H
122 lines (99 loc) · 2.75 KB
/
proc_loaderfwd.H
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
/*
** Copyright 2022-2024 Double Precision, Inc.
** See COPYING for distribution information.
*/
#ifndef proc_loaderfwd_h
#define proc_loaderfwd_h
#include <tuple>
#include <string>
#include <vector>
#include <algorithm>
#include <memory>
#include <exception>
#include "messages.H"
//! Container overrides
struct proc_override {
//! Overridden container state
enum class state_t {
none, //!< No state overrides
masked, //!< Container masked, as if it does not exist.
enabled, //!< Container is enabled (in its runlevel, presumably).
};
//! Return the container state override
state_t get_state() const { return state; }
//! Set the container state override
void set_state(state_t s) { state=s; }
//! Control group resources
typedef std::vector<std::tuple<std::string, std::string>> resources_t;
//! Return the current resources
const resources_t &get_resources() const { return resources; };
//! Set the resource
//! The existing value(s) are replaced, or appended to the end of the
//! existing resources. Any additional existing values get removed.
//!
//! A delete, therefore, is effected by passing in an empty list of
//! values.
void set_resource(const std::string &key,
std::vector<std::string> values)
{
auto b=resources.begin(), e=resources.end();
auto vb=values.begin(), ve=values.end();
while ((b=std::find_if(b, e,
[&]
(auto &element)
{
auto &[this_key,this_v]=element;
return key == this_key;
})) != e)
{
if (vb == ve)
{
b=resources.erase(b);
e=resources.end();
}
else
{
auto &[this_key, this_v]=*b;
this_v=std::move(*vb++);
++b;
}
}
resources_insert(resources.end(), key, vb, ve);
}
//! Add resource to the end of the existing resources.
void add_resource(const std::string &key,
std::vector<std::string> values)
{
return add_resource(key, std::move(values),
resources.size());
}
//! Add resource to the end of the existing resources.
void add_resource(const std::string &key,
std::vector<std::string> values,
size_t insert_pos)
{
if (insert_pos > resources.size())
throw std::runtime_error{
_("Invalid resource add position")
};
return resources_insert(resources.begin()+insert_pos,
key,
values.begin(),
values.end());
}
void install_resources(resources_t new_resources)
{
resources=std::move(new_resources);
}
private:
//! State override value
state_t state{state_t::none};
//! Resources
resources_t resources;
//! Insert values into resources.
void resources_insert(resources_t::iterator insert_pos,
const std::string &key,
std::vector<std::string>::iterator begin_iter,
std::vector<std::string>::iterator end_iter);
};
#endif