★ wanayoo — archive 1999 https://github.com/actor-framework/actor-framework/issues/173Nouvelle recherche | Portail wanayoo
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for constructing nested messages from other messages #173

Closed
mavam opened this issue Aug 14, 2014 · 12 comments
Closed

Allow for constructing nested messages from other messages #173

mavam opened this issue Aug 14, 2014 · 12 comments
Assignees

Comments

@mavam
Copy link
Collaborator

@mavam mavam commented Aug 14, 2014

It would be nice to have the ability to next messages. Here is a fictitious example:

message_handler h =
  [=](expensive const& e) {
     send(sink, make_message(unwrap(last_dequeued()), 42));
  };

The scenario is that a data structure with an expensive copy constructor exists already as a message (and thus internally as a reference-counted object). Then I'd like to be able to still reuse it in differently typed messages. It appears to be already possible to nest messages and extract them via apply, but as a user, I don't want to work with this extra level of type-erased indirection. Rather, I'd like to directly reuse the value. In the above example, the send statement with the fictitious unwrap would create a message of type <expensive, int> as opposed to <message, int>. I'm not saying that an unwrap function is the right primitive, but I hope it illustrates the point.

@Neverlord
Copy link
Member

@Neverlord Neverlord commented Aug 14, 2014

I think the more general term for this would be concat_messages or simply message concat(message lhs, message rhs). Your example then would become send(sink, concat(std::move(last_dequeued), make_message(42));.

@mavam
Copy link
Collaborator Author

@mavam mavam commented Aug 14, 2014

Yeah, I can see that concatenation does the trick in this case as well. But do you purposefully want to restrict yourself to concatenation at the message boundary? Consider the messages <T0, T1> and <T2, T3>. With a purely concatenation-based building block, it would be impossible to create messages of the form <T0, T3>.

Not sure if get<N> has already an overload for messages, but this would also feel natural: make_message(get<0>(m0), get<1>(m1), 42).

@Neverlord
Copy link
Member

@Neverlord Neverlord commented Aug 14, 2014

Messages are type-erased. You simply cannot provide any meaningful overload for get. However, you can combine messages the way you want nonetheless: concat(m0.take(1), m1.take_right(1)).

@mavam
Copy link
Collaborator Author

@mavam mavam commented Aug 14, 2014

Right, in order for get<N> to be meaningful and consistent, it should return a concrete type. The semantics I envisioned are actually the ones as available via the take* member functions. I hedged for get because it's often already known at compile-time what message offset one would like to extract. In this case, get would have to throw a bad_get or something...something that take would have to do too for out-of-bounds access. (Or you simply make it a precondition that one must know what to take.)

@mavam
Copy link
Collaborator Author

@mavam mavam commented Aug 15, 2014

I just realized that a get-like API exists already: message::get_as<T>(size_t).

@Neverlord
Copy link
Member

@Neverlord Neverlord commented Aug 15, 2014

Keep in mind, though, that you're copying out of the message when using get_as. Combining messages with take, concat, etc. uses structural sharing, i.e., you build a tree of reference-counted messages pointing into each others data without actually copying the data.

@mavam
Copy link
Collaborator Author

@mavam mavam commented Aug 15, 2014

Uh, that's really useful to know!

@mavam
Copy link
Collaborator Author

@mavam mavam commented Aug 15, 2014

Wait, the implementation looks like this:

  /**
   * Returns the value at @p as instance of @p T.
   */
  template <class T>
  inline const T& get_as(size_t p) const {
    CAF_REQUIRE(*(type_at(p)) == typeid(T));
    return *reinterpret_cast<const T*>(at(p));
  }

Where's the copy taking place?

@Neverlord
Copy link
Member

@Neverlord Neverlord commented Aug 15, 2014

In your example, you've used make_message which creates a new messages from the given values (by copying them in your case).

@mavam
Copy link
Collaborator Author

@mavam mavam commented Aug 15, 2014

Got it. I thought you implied that get_as<T> has a T instead of a T const& return value.

@Neverlord
Copy link
Member

@Neverlord Neverlord commented Feb 11, 2015

What about something like message::concat?

Here's an example snippet:

auto msg1 = make_message(1, 2, 3);
auto msg2 = make_message(4, 5, 6);
auto msg3 = message::concat(msg1.drop(1), msg2.take(2));
assert(msg3 == make_message(2, 3, 4, 5));
@mavam
Copy link
Collaborator Author

@mavam mavam commented Feb 11, 2015

Looks good to me. That's the final piece missing to make it possible slicing message values arbitrarily without copying.

Hamdor added a commit that referenced this issue Mar 4, 2015
Its now possible to concat messages, to do so you can use the
`message::concat(message other)` or the `+` operator.
Closes #173
Hamdor added a commit that referenced this issue Mar 6, 2015
Its now possible to concat messages, to do so you can use the
`message::concat(message other)` or the `+` operator.
Closes #173
Hamdor added a commit that referenced this issue Mar 6, 2015
Its now possible to concat messages, to do so you can use the
`message::concat(message other)` or the `+` operator.
Closes #173
@Hamdor Hamdor closed this in 53da870 Mar 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.