The Battle for Wesnoth
1.21.0-dev
Loading...
Searching...
No Matches
persist_manager.cpp
Go to the documentation of this file.
1
/*
2
Copyright (C) 2010 - 2025
3
by Jody Northup
4
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY.
12
13
See the COPYING file for more details.
14
*/
15
16
#include "
persist_context.hpp
"
17
#include "
persist_manager.hpp
"
18
19
persist_manager::persist_manager
()
20
: in_transaction_(false)
21
, contexts_()
22
{}
23
24
persist_manager::~persist_manager
() {
25
cancel_transaction
();
26
}
27
28
persist_context
&
persist_manager::get_context
(
const
std::string &ns)
29
{
30
persist_context::name_space
name(ns,
true
);
31
std::string key(name.
root_
);
32
context_map::iterator iter =
contexts_
.find(key);
33
if
(iter ==
contexts_
.end()) {
34
auto
pfc = std::make_unique<persist_file_context>(key);
35
if
(
in_transaction_
) pfc->start_transaction();
36
std::tie(iter, std::ignore) =
contexts_
.emplace(key, std::move(pfc));
37
}
38
auto
& ret = iter->second;
39
if
(ret->get_node() != ns)
40
ret->set_node(name.
descendants_
);
41
return
*ret;
42
}
43
44
bool
persist_manager::start_transaction
() {
45
if
(
in_transaction_
)
return
false
;
46
bool
result =
true
;
47
for
(context_map::reference ctx :
contexts_
) {
48
result &= ctx.second->start_transaction();
49
}
50
in_transaction_
=
true
;
51
return
result;
52
}
53
54
bool
persist_manager::end_transaction
() {
55
if
(!
in_transaction_
)
return
false
;
56
bool
result =
true
;
57
for
(context_map::reference ctx :
contexts_
) {
58
result &= ctx.second->end_transaction();
59
}
60
in_transaction_
= !result;
61
return
result;
62
}
63
64
bool
persist_manager::cancel_transaction
() {
65
if
(!
in_transaction_
)
return
false
;
66
bool
result =
true
;
67
for
(context_map::reference ctx :
contexts_
) {
68
result &= ctx.second->cancel_transaction();
69
}
70
in_transaction_
=
false
;
71
return
result;
72
}
persist_context
Definition
persist_context.hpp:25
persist_manager::contexts_
context_map contexts_
Definition
persist_manager.hpp:29
persist_manager::start_transaction
bool start_transaction()
Definition
persist_manager.cpp:44
persist_manager::in_transaction_
bool in_transaction_
Definition
persist_manager.hpp:28
persist_manager::get_context
persist_context & get_context(const std::string &ns)
Definition
persist_manager.cpp:28
persist_manager::persist_manager
persist_manager()
Definition
persist_manager.cpp:19
persist_manager::end_transaction
bool end_transaction()
Definition
persist_manager.cpp:54
persist_manager::~persist_manager
virtual ~persist_manager()
Definition
persist_manager.cpp:24
persist_manager::cancel_transaction
bool cancel_transaction()
Definition
persist_manager.cpp:64
persist_context.hpp
persist_manager.hpp
persist_context::name_space
Definition
persist_context.hpp:30
persist_context::name_space::descendants_
std::string descendants_
Definition
persist_context.hpp:35
persist_context::name_space::root_
std::string root_
Definition
persist_context.hpp:32
Generated by
1.9.8