Comments (10)
I see that make_transformed keeps things consistent with the rest of the library and I think this is more important than the issues mentioned
Yes, this is the main motivation. The bind_then_transform and limit_then_transform do the very similar things and I wanted to have the same way to do these things.
That stuff will be useful.
There are still many things to do, but I hope we can release v5.8.1 in October.
from sobjectizer.
Implemented in v.5.8.1.
from sobjectizer.
Hi, Marco!
It's an interesting idea at first glance, but let me to think a bit about it. I'll return with my thoughts later.
from sobjectizer.
Just to fix some initial thoughts.
It seems that this feature can be implemented this way:
binding->bind<Pair>(input,
so_5::transform_then_redirect_msink(dest_mbox, [](const Pair & src) {
return src.message1;
});
This syntax should allow to reuse existing bind
methods and should allow to combine transform_the_redirect
with delivery filters:
binding->bind<Pair>(input,
so_5::transform_then_redirect_msink(dest_mbox, [](const Pair & src) {
return src.message1;
},
// Delivery filter for source Pair message.
[](const Pair & src) -> bool { return ... /* some predicate */; } );
A functor that performs transformation may receive the source message in the form:
transformer(const Msg &); // Immutable (and mutable?) messages.
transformer(Msg); // Passing by value. Should work with mutable messages too, I think.
transformer(so_5::mhood_t<Msg>); // Should we support this? Immutable messages only.
transformer(so_5::mutable_mhood_t<Msg>); // Should we support this? Mutable messages only.
The return value of this functor can be in the form:
Msg transformer(...); // Immutable message of type Msg.
// The returned value will be used as argument
// to `so_5::send<Msg>(dest, returned_object)`.
so_5::message_holder_t<Msg> transformer(...); // Immutable message of type Msg.
// Allows to use preallocated messages.
so_5::message_holder_t<so_5::mutable_msg<Msg>> transformer(...); // Mutable message of type Msg.
// Allows to use preallocated messages.
The main problem with this approach is that we can bind transformer for message X with source message Y, for example:
auto transformer_sink = so_5::transform_then_redirect_msink(dest_mbox, [](const X & src) {...});
binding->bind<Y>(src_mbox, transformer_msink); // Oops!
This error can only be detected at the run-time.
But the approach with so_5::transform_the_redirect_msink
can be used as a building blocks to free template function(s) like this:
so_5::bind_then_transform(*binding, src_mbox, dest_mbox, [](const X &) {...});
In that case type X can be deduced and passed to the corresponding call of binding->bind
.
from sobjectizer.
There could be another format for so_5::transform_then_redirect_msink
that reuses make_transformed
from message limits:
binding->bind<Pair>(input,
so_5::transform_then_redirect_msink([dest_mbox](const Pair & src) {
return so_5::make_transformed<Message1>(dest_mbox, src.message1);
});
This approach may make SObjectizer more consistent because the same functionality (make_transformed
) will be reused for similar goals in similar contexts.
from sobjectizer.
Thanks for your feedback! I like the idea of using a wrapper like transform_then_redirect_msink
.
Please let us know if you are going to progress on this.
from sobjectizer.
The very first intermediate results allow to write something like that:
// For immutable messages only.
so_5::bind_then_transform(binding, src_mbox,
[dest_mbox](const src_msg_type & msg) /* Type of message is deduced from lambda argument type */ {
return so_5::make_transformed<dest_msg_type>(dest_mbox, ... /* parameters for dest_msg_type ctor */ );
});
// For signals.
so_5::bind_then_transform<signal_type>(binding, src_mbox,
[dest_mbox]() /* Lambda without arguments! */ {
return so_5::make_transformed<dest_msg_type>(dest_mbox, ... /* parameters for dest_msg_type ctor */ );
});
// For mutable messages...
so_5::bind_then_transform<so_5::mutable_msg<src_msg_type>>(binding, src_mbox,
[dest_mbox](auto & msg) /* No need to repeat type name */ {
return so_5::make_transformed<dest_msg_type>(dest_mbox, ... /* parameters for dest_msg_type ctor */ );
});
// ...or for explicit immutable messages.
so_5::bind_then_transform<so_5::immutable_msg<src_msg_type>>(binding, src_mbox,
[dest_mbox](auto & msg) /* No need to repeat type name, it will be const reference anyway */ {
return so_5::make_transformed<dest_msg_type>(dest_mbox, ... /* parameters for dest_msg_type ctor */ );
});
// A transformer can return optional if message has to be skipped sometimes.
so_5::bind_then_transform(binding, src_mbox,
[dest_mbox](const src_msg_type & msg) -> std::optional< so_5::transformed_message_t<dest_msg_type> > {
if(condition_to_redirect) {
return { so_5::make_transformed<dest_msg_type>(dest_mbox, ...) };
}
else {
// Message will be skipped.
return std::nullopt;
}
});
from sobjectizer.
I like the feature!
Just a few comments: make_transformed
adds some boilerplate and exposes the user to typos. For example, dest_mbox
is captured in the lambda and passed into make_transformed
. However, the user can distractedly pass the wrong instance.
Also, the lambda needs to invoke make_transformed
and this is a detail the user should know. It's an idiom, let's say.
On the other hand, something like this:
so_5::bind_then_transform(binding, src_mbox, dest_mbox, [](const src_msg_type & msg) {
return dest_msg_type{...};
});
is more compact and easy to understand.
But I see that make_transformed
keeps things consistent with the rest of the library and I think this is more important than the issues mentioned. After all, idioms are meant to be learned.
Awesome job! That stuff will be useful.
from sobjectizer.
This functionality can be seen in a new example. In a feature branch at the moment but it seems that it will be merged into the current development branch soon.
from sobjectizer.
Thanks for the update!
from sobjectizer.
Related Issues (20)
- Asynchrony of register_coop HOT 4
- Documentation for message_holder_t has to be extended HOT 1
- A usage example for agent_t::limit_then_redirect method in API Reference HOT 1
- [Design] Your opinion on expressing agent intent HOT 2
- Deprecation of coop_t::deregister and coop_t::deregister_normally methods HOT 1
- [idea] An emergency stop of SOEnv on an exception in noexcept context HOT 1
- `so_evt_finish` not called until `so_evt_start` is running? HOT 2
- Should agent_t::so_drop_subscription* methods be marked as noexcept? HOT 1
- Should delivery filters be checked for noexcept-ness?
- so_5::details::make_message_instance_impl metafunction doesn't set message mutability flag properly HOT 1
- limit_then_transform for mutable messages HOT 1
- Allow `const auto &` as an argument for delivery filter in single/multi_sink_binding HOT 1
- Should there be agent_t::so_disp_binder() and agent_t::so_coop_default_disp_binder() methods? HOT 2
- [idea] Make so5extra's revocable timers the default implementation for timers in SObjectizer
- Another constructor for wrapped_env_t that waits completion of init-function HOT 1
- Use of message limits and state_t::time_limit
- Optional name for an agent? HOT 2
- New method `as_string_view` for so_5::stats::prefix_t and so_5::stats::suffix_t HOT 1
- SO_5_TYPE shouldn't be used for so_5::stats::messages::quantity
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sobjectizer.