Aggregate initialization with Urho3D containers

Everything about the development of Urho3D.

Re: Aggregate initialization with Urho3D containers

PostPosted by sabotage3d » 27 Mar 2016, 21:37

If we can use C++11 I think Variadic templates are far more readable for functions than using initializer lists.
User avatar
sabotage3d
Have many posts
Have many posts
 
Posts: 515
Joined: 25 Oct 2014, 13:26

Re: Aggregate initialization with Urho3D containers

PostPosted by gawag » 28 Mar 2016, 09:40

sabotage3d wrote:If we can use C++11 I think Variadic templates are far more readable for functions than using initializer lists.

Yeah I would prefer them too.
Also initializer lists are completely broken when used together with a variadic template, they really screwed that up. I tend to avoid initializer lists due to that completely.

Though variadic templates are more expensive compile time wise and force function definitions into headers which initializer lists do not (I think).
Last edited by gawag on 28 Mar 2016, 19:03, edited 1 time in total.
Old Unofficial Urho3D Wiki: http://urho3d.wikia.com/
"Newer" Unofficial Urho3D Wiki: https://urho3d.miraheze.org/
My GitHub: https://github.com/damu (changed my name recently from gawag to damu there)
User avatar
gawag
Active user
Active user
 
Posts: 192
Joined: 12 Feb 2015, 03:48
Location: Germany

Re: Aggregate initialization with Urho3D containers

PostPosted by weitjong » 28 Mar 2016, 12:02

rku wrote:weitjong what do you think solution for const problem should be then?

I don't have the solution for that. Instead of creating another overload taking const VariantMap, probably it is user own responsibility to do the const_cast when calling the existing one. And perhaps if we really want to create an overload to make SendEvent() usage simpler then we should make a variadic template version of it, as already suggested by others. I believe we could implement a new one that internally call GetEventDataMap(), populate the map object with variadic arguments, and then call the existing SendEvent(). Thus, replacing the whole boilerplate code for sending event to just one line of code, similar to what you setup to do initially.

I think the aggregate initialization with Urho3D containers has its merit points in its own right.
YaoWT - Leave no window unbroken ♪ (^。^) 
User avatar
weitjong
Contributor
Contributor
 
Posts: 1094
Joined: 16 Jan 2014, 15:34
Location: Singapore, 新加坡

Re: Aggregate initialization with Urho3D containers

PostPosted by TheComet » 29 Mar 2016, 06:29

weitjong wrote:A few events actually expect the VariantMap to be non-const. The map can be used as input as well as output. i.e. the event handler can also put data into the map so that the event sender can read it back.


This seems very wrong to me. The very idea of an event is to transport data from A to B and not the other way around, simply because the event sender cannot know who's listening.

Would it be possible to extract those few cases where receivers put data back into some other mechanism?

[EDIT] A dirty hack would be to make the signature const, but have those few cases use const_cast. :D
I'm a non-binary non-cis sexually fluid cephalopod identifying genderqueer mocha frappé latte
User avatar
TheComet
Active user
Active user
 
Posts: 122
Joined: 29 Jan 2014, 14:07
Location: Germany

Re: Aggregate initialization with Urho3D containers

PostPosted by rku » 29 Mar 2016, 07:16

TheComet wrote:
weitjong wrote:A few events actually expect the VariantMap to be non-const. The map can be used as input as well as output. i.e. the event handler can also put data into the map so that the event sender can read it back.


This seems very wrong to me. The very idea of an event is to transport data from A to B and not the other way around, simply because the event sender cannot know who's listening.


I lean towards agreeing with you, but obviously thats not how Urho's events are designed. They are more like a wrapper over a function calls rather than events, because SendEvent() directly invokes event handlers. In turn we suffer from various issues like lack of control during which stage event is invoked or inability to send events from other threads too.
User avatar
rku
Active user
Active user
 
Posts: 103
Joined: 06 May 2015, 08:24

Re: Aggregate initialization with Urho3D containers

PostPosted by weitjong » 29 Mar 2016, 09:52

TheComet wrote:
weitjong wrote:A few events actually expect the VariantMap to be non-const. The map can be used as input as well as output. i.e. the event handler can also put data into the map so that the event sender can read it back.


This seems very wrong to me. The very idea of an event is to transport data from A to B and not the other way around, simply because the event sender cannot know who's listening.

Would it be possible to extract those few cases where receivers put data back into some other mechanism?

[EDIT] A dirty hack would be to make the signature const, but have those few cases use const_cast. :D


Those cases are really a few. I can think of two events as it was contributed by me not long ago. See NavigationEvents.h and DatabaseEvents.h. These events are not supposed to be subscribed by multiple listeners. But of course there is nothing prevented that and should they actually send conflicting data input. You are welcome to change it if there is better way. Unless the signature has been changed, I treat the map object as the container to communicate data between event sender and event subscribers in both directions (although mostly only one direction).

Regarding sending events in worker threads, I believe that topic has been discussed here in the forums too. This is the latest discussion I could find quickly using google. topic1753.html.
YaoWT - Leave no window unbroken ♪ (^。^) 
User avatar
weitjong
Contributor
Contributor
 
Posts: 1094
Joined: 16 Jan 2014, 15:34
Location: Singapore, 新加坡

Re: Aggregate initialization with Urho3D containers

PostPosted by cadaver » 29 Mar 2016, 10:30

Allowing the VariantMap to be modified is a feature, think of it as a return value. When doing that it's true that you've got to be careful if you have multiple subscribers.
User avatar
cadaver
Urho3D author
Urho3D author
 
Posts: 1802
Joined: 16 Jan 2014, 14:52
Location: Finland

Re: Aggregate initialization with Urho3D containers

PostPosted by weitjong » 11 Apr 2016, 08:16

The variadic template version of the SendEvent() method is just in.
YaoWT - Leave no window unbroken ♪ (^。^) 
User avatar
weitjong
Contributor
Contributor
 
Posts: 1094
Joined: 16 Jan 2014, 15:34
Location: Singapore, 新加坡

Re: Aggregate initialization with Urho3D containers

PostPosted by rku » 12 Apr 2016, 16:47

I think it is messed it up. How about simpler:
Code: Select all
void Object::SendEvent(StringHash eventType, const VariantMap& eventData)
{
    VariantMap& globalEventData = GetEventDataMap();
    for (auto it = eventData.Begin(); it != eventData.End(); it++)
        globalEventData.Insert(it);
    SendEvent(eventType, globalEventData);
}


Does exactly what variadic one does, however it can be used in a obvious way passing {{"a", b}, {"c", d}}. Also it does not need to be under c++11 so everyone can get it. As a nice bonus it does not bloat executable by making separate function for every N of pairs we pass.
User avatar
rku
Active user
Active user
 
Posts: 103
Joined: 06 May 2015, 08:24

Re: Aggregate initialization with Urho3D containers

PostPosted by weitjong » 12 Apr 2016, 22:36

In a nutshell what you proposed is similar to what I have just done, except that my approach requires less punctuation marks. The variadic template is type safe. The compiler would ensure you cannot enter garbage parameter type in or that the parameter list is not correctly paired up. As for the bonus, I would even say mine has the advantage. A good C++11 compiler should resolve the recursiveness of the variadic template during compile time and inline the call, while your loop may or may not be unrolled to achieve the same result.
YaoWT - Leave no window unbroken ♪ (^。^) 
User avatar
weitjong
Contributor
Contributor
 
Posts: 1094
Joined: 16 Jan 2014, 15:34
Location: Singapore, 新加坡

PreviousNext

Return to Developer Talk

Who is online

Users browsing this forum: No registered users and 0 guests