/
refuge.cpp
103 lines (90 loc) · 2.31 KB
/
refuge.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
/** @file refuge.cpp Persistent data storage.
*
* @authors Copyright © 2014 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/Refuge"
#include "de/App"
#include "de/Archive"
#include "de/Reader"
#include "de/Writer"
namespace de {
DENG2_PIMPL_NOREF(Refuge)
{
String persistentPath;
Record names;
};
Refuge::Refuge(String const &persistentPath) : d(new Instance)
{
d->persistentPath = persistentPath;
try
{
read();
}
catch(Error const &er)
{
LOG_AS("Refuge");
LOGDEV_RES_MSG("\"%s\" could not be read: %s") << persistentPath << er.asText();
}
}
String Refuge::path() const
{
return d->persistentPath;
}
Refuge::~Refuge()
{
// Musn't throw exceptions from destructor...
try
{
write();
}
catch(Error const &er)
{
LOG_AS("~Refuge");
LOG_ERROR("\"%s\" could not be written: %s") << d->persistentPath << er.asText();
}
}
void Refuge::read()
{
if(App::hasPersistentData())
{
Reader(App::persistentData().entryBlock(d->persistentPath)).withHeader() >> d->names;
}
}
void Refuge::write() const
{
if(App::hasPersistentData())
{
Writer(App::mutablePersistentData().entryBlock(d->persistentPath)).withHeader()
<< d->names;
}
}
Time Refuge::lastWrittenAt() const
{
if(App::hasPersistentData())
{
return App::persistentData().entryStatus(d->persistentPath).modifiedAt;
}
return Time::invalidTime();
}
Record &Refuge::names()
{
return d->names;
}
Record const &Refuge::names() const
{
return d->names;
}
} // namespace de