Skip to content

Commit 5fa2180

Browse files
Refactor FileChanges to be immutable and update usage
- Make FileChanges immutable and produced only by FileChangesBuilder - Move push_* methods to private, expose only for_each and special members - Update CMake and tests to use FileChangesBuilder where needed - Improve documentation to clarify usage and construction - Remove push_* symbols from public ABI
1 parent 4a3a55c commit 5fa2180

8 files changed

Lines changed: 154 additions & 27 deletions

File tree

include/miral/miral/live_config.h

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@
3030

3131
namespace miral::live_config
3232
{
33-
/// A pimpl'd, ordered batch of file-system change events.
33+
/// An immutable, ordered batch of file-system change events.
3434
///
35-
/// Callers build the batch with push_unchanged(), push_moved_or_new(),
36-
/// push_modified(), push_dropped(), and push_renamed(), then pass it to
37-
/// IniFileWithOverrides::load(). The store consumes the batch by calling
38-
/// for_each() with one callback per event kind.
35+
/// Produced internally by FileChangesBuilder and passed to IniFileWithOverrides::load().
36+
/// The store consumes the batch by calling for_each() with one callback per event kind.
3937
///
4038
/// Events are dispatched in push order, so relative file priority is
4139
/// preserved across kinds.
@@ -60,6 +58,22 @@ class FileChanges
6058
using Renamed =
6159
std::move_only_function<void(std::filesystem::path const&, std::filesystem::path const&, std::istream&)>;
6260

61+
/// Iterate all events in push order, invoking the matching callback for each.
62+
void for_each(
63+
Load unchanged,
64+
Load moved_or_new,
65+
Load modified,
66+
Dropped dropped,
67+
Renamed renamed) const;
68+
69+
~FileChanges();
70+
71+
FileChanges(FileChanges const&) = default;
72+
FileChanges& operator=(FileChanges const&) = default;
73+
74+
private:
75+
friend class FileChangesBuilder;
76+
6377
FileChanges();
6478

6579
auto push_unchanged(
@@ -81,19 +95,6 @@ class FileChanges
8195
std::filesystem::path new_filepath,
8296
std::function<std::unique_ptr<std::istream>()> stream) -> FileChanges&;
8397

84-
/// Iterate all events in push order, invoking the matching callback for each.
85-
void for_each(
86-
Load unchanged,
87-
Load moved_or_new,
88-
Load modified,
89-
Dropped dropped,
90-
Renamed renamed) const;
91-
92-
~FileChanges() = default;
93-
FileChanges(FileChanges const&) = default;
94-
FileChanges& operator=(FileChanges const&) = default;
95-
96-
private:
9798
struct Self;
9899
std::shared_ptr<Self> self;
99100
};

src/miral/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ add_library(miral-internal STATIC
3636
window_info_defaults.h
3737
window_specification_internal.cpp window_specification_internal.h
3838
basic_store.cpp basic_store.h
39+
file_changes_builder.cpp file_changes_builder.h
40+
live_config.cpp ${miral_include}/miral/live_config.h
3941
)
4042

4143
# Already implied by the linker's symbol version script, but can avoid accidents
@@ -148,8 +150,7 @@ add_library(miral-external OBJECT
148150
mousekeys_config.cpp ${miral_include}/miral/mousekeys_config.h
149151
output_filter.cpp ${miral_include}/miral/output_filter.h
150152
simulated_secondary_click.cpp ${miral_include}/miral/simulated_secondary_click.h
151-
live_config.cpp ${miral_include}/miral/live_config.h
152-
live_config_ini_file.cpp ${miral_include}/miral/live_config_ini_file.h
153+
live_config_ini_file.cpp ${miral_include}/miral/live_config_ini_file.h
153154
ini_file_with_overrides.cpp ${miral_include}/miral/ini_file_with_overrides.h
154155
hover_click.cpp ${miral_include}/miral/hover_click.h
155156
magnifier.cpp ${miral_include}/miral/magnifier.h

src/miral/file_changes_builder.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright © Canonical Ltd.
3+
*
4+
* This program is free software: you can redistribute it and/or modify it
5+
* under the terms of the GNU General Public License version 2 or 3 as
6+
* published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#include "file_changes_builder.h"
18+
19+
namespace mlc = miral::live_config;
20+
21+
mlc::FileChangesBuilder::FileChangesBuilder() :
22+
changes_{}
23+
{
24+
}
25+
26+
auto mlc::FileChangesBuilder::push_unchanged(
27+
std::filesystem::path filepath,
28+
std::function<std::unique_ptr<std::istream>()> stream) -> FileChangesBuilder&
29+
{
30+
changes_.push_unchanged(std::move(filepath), std::move(stream));
31+
return *this;
32+
}
33+
34+
auto mlc::FileChangesBuilder::push_moved_or_new(
35+
std::filesystem::path filepath,
36+
std::function<std::unique_ptr<std::istream>()> stream) -> FileChangesBuilder&
37+
{
38+
changes_.push_moved_or_new(std::move(filepath), std::move(stream));
39+
return *this;
40+
}
41+
42+
auto mlc::FileChangesBuilder::push_modified(
43+
std::filesystem::path filepath,
44+
std::function<std::unique_ptr<std::istream>()> stream) -> FileChangesBuilder&
45+
{
46+
changes_.push_modified(std::move(filepath), std::move(stream));
47+
return *this;
48+
}
49+
50+
auto mlc::FileChangesBuilder::push_dropped(std::filesystem::path filepath) -> FileChangesBuilder&
51+
{
52+
changes_.push_dropped(std::move(filepath));
53+
return *this;
54+
}
55+
56+
auto mlc::FileChangesBuilder::push_renamed(
57+
std::filesystem::path old_filepath,
58+
std::filesystem::path new_filepath,
59+
std::function<std::unique_ptr<std::istream>()> stream) -> FileChangesBuilder&
60+
{
61+
changes_.push_renamed(std::move(old_filepath), std::move(new_filepath), std::move(stream));
62+
return *this;
63+
}
64+
65+
mlc::FileChangesBuilder::operator mlc::FileChanges const&() const
66+
{
67+
return changes_;
68+
}

src/miral/file_changes_builder.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright © Canonical Ltd.
3+
*
4+
* This program is free software: you can redistribute it and/or modify it
5+
* under the terms of the GNU General Public License version 2 or 3 as
6+
* published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#ifndef MIRAL_FILE_CHANGES_BUILDER_H
18+
#define MIRAL_FILE_CHANGES_BUILDER_H
19+
20+
#include <miral/live_config.h>
21+
22+
namespace miral::live_config
23+
{
24+
/// Internal builder for FileChanges batches.
25+
/// Only Mir's file-watching infrastructure should construct these;
26+
/// consumers receive a FileChanges const& and call for_each().
27+
class FileChangesBuilder
28+
{
29+
public:
30+
FileChangesBuilder();
31+
32+
auto push_unchanged(
33+
std::filesystem::path filepath,
34+
std::function<std::unique_ptr<std::istream>()> stream) -> FileChangesBuilder&;
35+
36+
auto push_moved_or_new(
37+
std::filesystem::path filepath,
38+
std::function<std::unique_ptr<std::istream>()> stream) -> FileChangesBuilder&;
39+
40+
auto push_modified(
41+
std::filesystem::path filepath,
42+
std::function<std::unique_ptr<std::istream>()> stream) -> FileChangesBuilder&;
43+
44+
auto push_dropped(std::filesystem::path filepath) -> FileChangesBuilder&;
45+
46+
auto push_renamed(
47+
std::filesystem::path old_filepath,
48+
std::filesystem::path new_filepath,
49+
std::function<std::unique_ptr<std::istream>()> stream) -> FileChangesBuilder&;
50+
51+
/// Implicit conversion so a builder can be passed directly to load(FileChanges const&).
52+
operator FileChanges const&() const;
53+
54+
private:
55+
FileChanges changes_;
56+
};
57+
}
58+
59+
#endif // MIRAL_FILE_CHANGES_BUILDER_H

src/miral/live_config.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ miral::live_config::FileChanges::FileChanges() :
111111
{
112112
}
113113

114+
miral::live_config::FileChanges::~FileChanges() = default;
115+
114116
auto miral::live_config::FileChanges::push_unchanged(
115117
std::filesystem::path filepath,
116118
std::function<std::unique_ptr<std::istream>()> stream) -> FileChanges&

src/miral/symbols.map

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -781,11 +781,6 @@ global:
781781
extern "C++" {
782782
miral::live_config::FileChanges::FileChanges*;
783783
miral::live_config::FileChanges::?FileChanges*;
784-
miral::live_config::FileChanges::push_unchanged*;
785-
miral::live_config::FileChanges::push_moved_or_new*;
786-
miral::live_config::FileChanges::push_modified*;
787-
miral::live_config::FileChanges::push_dropped*;
788-
miral::live_config::FileChanges::push_renamed*;
789784
miral::live_config::FileChanges::for_each*;
790785
miral::live_config::IniFileWithOverrides::?IniFileWithOverrides*;
791786
miral::live_config::IniFileWithOverrides::IniFileWithOverrides*;

tests/miral/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ mir_add_wrapped_executable(miral-test-internal NOINSTALL
6060
test_hover_click.cpp
6161
test_locate_pointer.cpp
6262
basic_store.cpp
63+
ini_file_with_overrides.cpp
6364
${MIRAL_TEST_SOURCES}
6465
)
6566

@@ -83,7 +84,6 @@ mir_add_wrapped_executable(miral-test NOINSTALL
8384
config_file.cpp
8485
live_config.cpp
8586
live_config_ini_file.cpp
86-
ini_file_with_overrides.cpp
8787
magnifier.cpp
8888
output_filter.cpp
8989
sticky_keys.cpp

tests/miral/ini_file_with_overrides.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include <miral/ini_file_with_overrides.h>
18+
#include "file_changes_builder.h"
1819

1920
#include <gmock/gmock.h>
2021
#include <gtest/gtest.h>
@@ -49,7 +50,7 @@ struct IniFileWithOverridesTest : Test
4950
using FileContents = std::vector<std::pair<std::string, std::filesystem::path>>;
5051
void load(FileContents contents_and_paths)
5152
{
52-
mlc::FileChanges changes;
53+
mlc::FileChangesBuilder changes;
5354
for (auto const& [contents, path] : contents_and_paths)
5455
changes.push_modified(
5556
path,

0 commit comments

Comments
 (0)