-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef ALIA_FLOW_UTILITIES_HPP | ||
#define ALIA_FLOW_UTILITIES_HPP | ||
|
||
#include <alia/context/interface.hpp> | ||
#include <alia/flow/data_graph.hpp> | ||
|
||
namespace alia { | ||
|
||
// make_returnable_ref(ctx, x) stores a copy of x within the data graph of ctx | ||
// and returns a reference to that copy. (It will move instead of copying when | ||
// possible.) | ||
|
||
template<class T> | ||
struct returnable_ref_node : data_node | ||
{ | ||
// TODO: Use optional instead. | ||
std::unique_ptr<T> value; | ||
|
||
void | ||
clear_cache() | ||
{ | ||
value.reset(); | ||
} | ||
}; | ||
|
||
template<class T> | ||
T& | ||
make_returnable_ref(context ctx, T x) | ||
{ | ||
returnable_ref_node<T>* node; | ||
if (get_data_node(ctx, &node)) | ||
node->value.reset(new T(x)); | ||
else | ||
*node->value = std::move(x); | ||
return *node->value; | ||
} | ||
|
||
} // namespace alia | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#define ALIA_LOWERCASE_MACROS | ||
|
||
#include <alia/flow/utilities.hpp> | ||
|
||
#include <alia/signals/basic.hpp> | ||
|
||
#include <testing.hpp> | ||
|
||
#include "traversal.hpp" | ||
|
||
using namespace alia; | ||
|
||
using std::string; | ||
|
||
TEST_CASE("make_returnable_ref", "[flow][for_each]") | ||
{ | ||
alia::system sys; | ||
initialize_system(sys, [](context) {}); | ||
|
||
auto function_that_returns = [](context ctx) -> readable<string> { | ||
return make_returnable_ref(ctx, value(string("something"))); | ||
}; | ||
|
||
auto controller = [&](context ctx) { | ||
auto s = function_that_returns(ctx); | ||
REQUIRE(signal_has_value(s)); | ||
REQUIRE(read_signal(s) == "something"); | ||
}; | ||
|
||
do_traversal(sys, controller); | ||
} |