Skip to content

Commit 25ecbcb

Browse files
committed
async_fstream cacheing
1 parent f61881e commit 25ecbcb

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
obj/
2-
bin/
2+
bin/test
33
*.make
44
Makefile

bin/test.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

include/kit/async/async_fstream.h

+18-5
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,24 @@ class async_fstream
6868
[this]{return m_Filename;}
6969
);
7070
};
71+
std::future<std::string> buffer() const {
72+
return m_pCircuit->task<std::string>([this]{
73+
_cache();
74+
return m_Buffer;
75+
});
76+
}
7177

7278
template<class T>
7379
std::future<T> with(std::function<T(std::fstream& f)> func) {
7480
return m_pCircuit->task<T>([this,func]{ return func(m_File); });
7581
}
82+
template<class T>
83+
std::future<T> with(std::function<T(const std::string&)> func) {
84+
return m_pCircuit->task<T>([this,func]{
85+
_cache();
86+
return func(m_Buffer);
87+
});
88+
}
7689

7790
std::future<void> invalidate() {
7891
return m_pCircuit->task<void>(
@@ -85,7 +98,7 @@ class async_fstream
8598
[this]{_invalidate();_cache();}
8699
);
87100
}
88-
std::future<void> cache() {
101+
std::future<void> cache() const {
89102
return m_pCircuit->task<void>(
90103
[this]{_cache();}
91104
);
@@ -103,9 +116,9 @@ class async_fstream
103116
void _invalidate() {
104117
m_Buffer.clear();
105118
}
106-
void _cache() {
119+
void _cache() const {
107120
if(m_Buffer.empty()){
108-
m_File.seekg(0, std::ios::end);
121+
m_File.seekg(0, std::ios::end);
109122
m_Buffer.reserve(m_File.tellg());
110123
m_File.seekg(0, std::ios::beg);
111124
m_Buffer.assign(
@@ -116,10 +129,10 @@ class async_fstream
116129
}
117130

118131
Multiplexer::Circuit* const m_pCircuit;
119-
std::fstream m_File;
132+
mutable std::fstream m_File;
120133
std::string m_Filename;
121134

122-
std::string m_Buffer;
135+
mutable std::string m_Buffer;
123136
};
124137

125138
#endif

tests/async.test.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ TEST_CASE("Multiplexer","[multiplexer]") {
305305
}
306306

307307
TEST_CASE("Coroutines","[coroutines]") {
308-
SECTION("Coroutines inside multiplexer"){
308+
SECTION("Interleaved"){
309309
// In most apps, we'd use the singleton multiplexer "MX"
310310
// and use AWAIT() instead of AWAIT_MX(mx, ...)
311311
// But since we want to isolate the multiplexer across unit tests
@@ -414,6 +414,10 @@ TEST_CASE("async_fstream","[async_fstream]") {
414414
return f.is_open();
415415
}).get() == true);
416416
REQUIRE(file.filename().get() == fn);
417+
//std::string buf = file.with<string>([](const std::string& b){
418+
// return b;
419+
//}).get();
420+
REQUIRE(file.buffer().get() == "test\n"); // contents of file
417421
file.close().get();
418422
REQUIRE(file.filename().get() == "");
419423

0 commit comments

Comments
 (0)