MPD  0.20.18
StateFile.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_STATE_FILE_HXX
21 #define MPD_STATE_FILE_HXX
22 
23 #include "event/TimeoutMonitor.hxx"
24 #include "fs/AllocatedPath.hxx"
25 #include "Compiler.h"
26 
27 #include <string>
28 #include <chrono>
29 
30 struct Partition;
31 class OutputStream;
33 
34 class StateFile final : private TimeoutMonitor {
35  const AllocatedPath path;
36  const std::string path_utf8;
37 
38  const std::chrono::steady_clock::duration interval;
39 
40  Partition &partition;
41 
46  unsigned prev_volume_version = 0, prev_output_version = 0,
47  prev_playlist_version = 0;
48 
49 #ifdef ENABLE_DATABASE
50  unsigned prev_storage_version = 0;
51 #endif
52 
53 public:
54  static constexpr std::chrono::steady_clock::duration DEFAULT_INTERVAL = std::chrono::minutes(2);
55 
56  StateFile(AllocatedPath &&path, std::chrono::steady_clock::duration interval,
57  Partition &partition, EventLoop &loop);
58 
59  void Read();
60  void Write();
61 
65  void CheckModified();
66 
67 private:
68  void Write(OutputStream &os);
69  void Write(BufferedOutputStream &os);
70 
74  void RememberVersions() noexcept;
75 
80  gcc_pure
81  bool IsModified() const noexcept;
82 
83  /* virtual methods from TimeoutMonitor */
84  void OnTimeout() override;
85 };
86 
87 #endif /* STATE_FILE_H */
void Write()
This class monitors a timeout.
static constexpr std::chrono::steady_clock::duration DEFAULT_INTERVAL
Definition: StateFile.hxx:54
void Read()
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:52
A path name in the native file system character set.
A partition of the Music Player Daemon.
Definition: Partition.hxx:42
void CheckModified()
Schedules a write if MPD's state was modified.
An OutputStream wrapper that buffers its output to reduce the number of OutputStream::Write() calls...
StateFile(AllocatedPath &&path, std::chrono::steady_clock::duration interval, Partition &partition, EventLoop &loop)
#define gcc_pure
Definition: Compiler.h:116