Aggregate initialization with Urho3D containers

Everything about the development of Urho3D.

Aggregate initialization with Urho3D containers

PostPosted by sabotage3d » 20 Mar 2016, 00:57

Is there any way to do aggregate initialization with Urho3D containers? Similar to std::initializer_list and the nice functionality of Boost's Assignment library:http://www.boost.org/doc/libs/master/libs/assign/doc/index.html .
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 rku » 26 Mar 2016, 12:24

Does not look like it. However it is easy to add:
Code: Select all
#include <initializer_list>
#include <utility>

    /// Aggregate initialization constructor
    HashMap(std::initializer_list<std::pair<T, U>> list)
    {
        for (auto it = list.begin(); it != list.end(); it++)
        {
            Insert(Pair<T, U>(it->first, it->second));
        }
    }


Note that this uses c++11 features therefore urho needs to be built with -DURHO3D_C++11=ON

EDIT:
Although i noticed we cant still do SendEvent("Some event", {{"key", value}}); due to fact that SendEvent() does not take const VariantMap. This is very unfortunate. Sure we could add overload with const and cast constness away but IMO it would be better to just make VariantMap const. But it triggers ugly cascade of changes in source code. Bummer.
User avatar
rku
Active user
Active user
 
Posts: 103
Joined: 06 May 2015, 08:24

Re: Aggregate initialization with Urho3D containers

PostPosted by weitjong » 26 Mar 2016, 14:26

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.
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 » 26 Mar 2016, 14:46

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.



Well maybe it is not that big of a problem, since events are executed right on SendEvent() call. Until SendEvent() completes that const VariantMap is guaranteed to be alive anyway and there is little harm done in modifying that variant map in calls like SendEvent("...", {{"a", 1}}). However it could be a problem if someone makes wrong assumption and passes his own instance of const VariantMap and it gets modified while user expects it to not be touched. So either risk that or give up "SendEvent("...", {{"a", 1}})" in favour of uglier "VariantMap args = {{"a", 1}}; SendEvent("...", args)". I personally hate writing two lines where one does suffice and is even more readable. Another solution would be boost way which looks like "VariantMap()(key, value)(key2, value2)" but i dont like it. It is a hack for pre-c++11 times. IMHO we should use built-in language stuff first if it is available. What do you think right way would be to go about this?

P.S. I already implemented these constructors for urho's containers + SendEvent() with casting constness away. Ill make a PR when we figure out this const thing.
User avatar
rku
Active user
Active user
 
Posts: 103
Joined: 06 May 2015, 08:24

Re: Aggregate initialization with Urho3D containers

PostPosted by yushli » 26 Mar 2016, 16:03

I would like to see this feature in main branch soon. It helps keep code more concise.
User avatar
yushli
Some active
Some active
 
Posts: 94
Joined: 18 Oct 2014, 05:42

Re: Aggregate initialization with Urho3D containers

PostPosted by rku » 27 Mar 2016, 09:08

Meanwhile you can use it as a patch if you want: https://github.com/r-ku/Urho3D/commit/1 ... 82facbefd1
User avatar
rku
Active user
Active user
 
Posts: 103
Joined: 06 May 2015, 08:24

Re: Aggregate initialization with Urho3D containers

PostPosted by sabotage3d » 27 Mar 2016, 10:43

I always thought we have to conform to C++98 for PRs. Is that a new flag URHO3D_CXX11?

At the moment I have settled for this syntax.

Code: Select all
Vector<float> vec;
vec += 1.0, 2.0, 3.0, 4.0;


Using this template without C++11.

Code: Select all
template<typename S, typename T>
Urho3D::Vector<S>& operator += (Urho3D::Vector<S>& v, const T & item)
{
    v.Push(item);  return v;
}
template<typename S, typename T>
Urho3D::Vector<S>& operator,(Urho3D::Vector<S>& v, const T & item)
{
    v.Push(item);  return v;
}
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 rku » 27 Mar 2016, 13:11

Yes that flag seems new. Aside from passing relevant compiler param (like -std=c++11) and adding preprocessor define its not used anywhere else. Although IMO we should take advantage of these new features. Would be especially handy if we could use lambdas in place of URHO3D_HANDLER(). Ill definitely add that for myself.
User avatar
rku
Active user
Active user
 
Posts: 103
Joined: 06 May 2015, 08:24

Re: Aggregate initialization with Urho3D containers

PostPosted by weitjong » 27 Mar 2016, 14:46

I think you guys meant compiler define. Yes, that "URHO3D_CXX11" is defined exactly for this kind of usage. The idea is to ease the transition to c++11 standard. Naturally since we could not (yet) set the c++11 standard as the default now, nothing should break when the new standard is not enabled. If you can ensure that then I think there is a good chance the patch such as this one will be accepted when you submit it as PR. However, having both const and non-const VariantMap version of SendEvent() may not be a good idea. I recall we have already address the constant reallocation issue of event data map by reusing the map object. See the Context::GetEventDataMap().
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 » 27 Mar 2016, 17:34

weitjong what do you think solution for const problem should be then? I mean requiring to write
Code: Select all
VariantMap args = {{"a", 1}};
SendEvent("event", args);

instead of
Code: Select all
SendEvent("event", {{"a", 1}});

is solving aggregate initializer problem only half-way. Everyone would really appreciate ability to use them in-place on one line.
User avatar
rku
Active user
Active user
 
Posts: 103
Joined: 06 May 2015, 08:24

Next

Return to Developer Talk

Who is online

Users browsing this forum: No registered users and 1 guest

cron