Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Dispatcher with new meta-Function idiom  (Read 2961 times)

0 Members and 1 Guest are viewing this topic.

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Dispatcher with new meta-Function idiom
« on: April 19, 2016, 09:13:31 pm »
hello

i have an opportunity to get a proposal paper that might be included in C++ standard and i would like to share it with you. here a link to pdf file:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4436.pdf.
it is actually an idiom for meta-programming language. since we are mostly interesting in game development, this idiom might helpful for beginners like me to understand and implement it in any way that suits our need in game dev. an easy usage of this idiom would be dispatcher-like structures.

the dispatcher structure has a feature of registration facilities to attach member functions as listeners in easiest way possible. also, it has an ability to call right member function for any handler.

my interesting case to use this idiom in dispatcher-like structure is when i want the score to be updated for any  game's event that may happen.

here is a simple demo.
(click to show/hide)
« Last Edit: April 20, 2016, 11:56:45 pm by MORTAL »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Dispatcher with new meta-Function idiom
« Reply #1 on: April 20, 2016, 08:40:49 am »
Can everything you propose already be implemented with C++ right now?
Do you simply want to get that idiom code into the standard?

It seems something very specific and since it can be done with C++ already (I assume) with a few lines of code, what exactly would the advantage be, for including something like this into the C++ standard?

Is it thread-safe?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Dispatcher with new meta-Function idiom
« Reply #2 on: April 20, 2016, 12:09:39 pm »
Reminds me of my own Aurora Dispatchers and Thor Event Systems, but more taylored towards compile-time. Which obviously brings up the question in which cases it is an improvement over overload resolution?

Can you quickly outline the purpose? How does it differ from existing libraries such as Boost.Signals2 or Boost.Variant + Visitor Pattern?

How is the paper, which talks about the "detection idiom", related to this dispatcher? Would the former really considerably simplify the implementation?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Dispatcher with new meta-Function idiom
« Reply #3 on: April 20, 2016, 01:03:51 pm »
Can everything you propose already be implemented with C++ right now?
yes, C++ standard already has std::is_void which is defined in <type_traits> header file. if we want use it without detection idiom in demo example in main post the registerHandler() member function will be like this:
template<class C>
std::enable_if_t<std::is_void<decltype(std::declval<C>().receive(std::declval<const E&>()))>::value>
registerHandler(C& callback)
{
        const static auto Callable = static_cast<void(C::*)(const E&)>(&C::receive);
        handlerFunction = std::bind(Callable, callback, std::placeholders::_1);
        base.registerHandler(callback);
}
as you see just one line and straightforward implementation with SFINAE
Do you simply want to get that idiom code into the standard?
not necessary to be included in standard but it a tool for us to use it when we actually need it.
It seems something very specific and since it can be done with C++ already (I assume) with a few lines of code, what exactly would the advantage be, for including something like this into the C++ standard?
actually, i can't speak of behalf of idiom authors, but yeah, i'm sure this very first question they must answer it.
Is it thread-safe?
i can't answer this question due to my lack of understanding the thread-safe topic.


Reminds me of my own Aurora Dispatchers and Thor Event Systems, but more taylored towards compile-time. Which obviously brings up the question in which cases it is an improvement over overload resolution?
the main reason that lead me to delve into meta-programming is when i was want to know how does THOR library works internally and i hit deadlock when i see aurora at first time. at that time i have no idea what was template or meta-programming is. but yeah THOR library is much powerful and flexible.

i made this for self-teaching, i know out there is much robust and well-formatted libraries.

Can you quickly outline the purpose? How does it differ from existing libraries such as Boost.Signals2 or Boost.Variant + Visitor Pattern?

How is the paper, which talks about the "detection idiom", related to this dispatcher? Would the former really considerably simplify the implementation?
yeah, duo to my ignorance in boost library, i had conversation with some friends regarding this yesterday , i got exactly same question, it took me to check out the boost.Signals. it turns out, it is indeed does the job in this matters. that for sure a thread safe regarding eXpl0it3r's question
« Last Edit: April 20, 2016, 01:19:37 pm by MORTAL »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Dispatcher with new meta-Function idiom
« Reply #4 on: April 20, 2016, 03:32:38 pm »
I'm not sure if it makes sense to talk about thread-safety, as this construct may be too low-level. What exactly should be thread-safe? What are the shared resources? Where do race conditions occur?

Many of that depends on the actual functions passed to the dispatcher, and the variables they have access to. The dispatcher doesn't have any influence here... Regarding thread-safety of the public API (registerHandler() and call() methods), I'm not sure if it's worth if everyone pays the price of synchronization when it's rarely needed. So we should clarify what we refer to here.

MORTAL, you haven't answered my core question though: what is the purpose of your dispatcher? is it a more elegant solution of the Visitor Pattern?
« Last Edit: April 20, 2016, 03:36:30 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Dispatcher with new meta-Function idiom
« Reply #5 on: April 20, 2016, 05:56:09 pm »
i'm totally agree with you.