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

Author Topic: Aurora  (Read 27420 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Aurora
« on: March 19, 2012, 06:16:23 pm »
Aurora

Although this project isn't directly related to SFML, I thought some people may find it interesting. Formerly a part of Thor, Aurora is now an autonomous library that provides various C++ functionality which can be useful in different situations.

I know this sounds extremely abstract, so here is an example of a quite intelligent smart pointer:
#include <Aurora/SmartPtr/CopiedPtr.hpp>

namespace sf { class Drawable; } // Forward declaration is enough

struct GameObject
{
        aur::CopiedPtr<sf::Drawable> drawable;
};

#include <SFML/Graphics.hpp>

int main()
{
        GameObject a, b, c;
        a.drawable.reset(new sf::RectangleShape);
        b.drawable.reset(new sf::Sprite); // different derivates
        c = a; // performs a deep copy
} // deletes everything correctly
sf::Drawable is an abstract base class, but we can wrap it into a smart pointer. Like this, GameObject has normal value semantics, i.e. we can copy it as usual, while the derived type (sf::RectangleShape) is copied correctly through the hierarchy. This without a single copy constructor, assignment operator or a virtual clone function.

Another example are the dispatchers (also known as a way to emulate multimethods in C++). Their task is to choose the correct overload of a set of functions, of which the arguments are only known at runtime. This allows you to work with abstract base classes (like Object in the example), while interaction between two objects can still be implemented with the full type information. No need for manual case differentiations.
#include <Aurora/Dispatch.hpp>
#include <iostream>

// Example class hierarchy
class Object { public: virtual ~Object() {} };
class Asteroid : public Object {};
class Ship     : public Object {};

// Functions for collision with different argument types
void collision(Asteroid*, Asteroid*)    { std::cout << "Asteroid-Asteroid\n"; }
void collision(Asteroid*, Ship*)        { std::cout << "Asteroid-Ship\n";     }
void collision(Ship*,     Ship*)        { std::cout << "Ship-Ship\n";         }

int main()
{
    // Create dispatcher and register functions
    aur::DoubleDispatcher<Object*> dispatcher;
    dispatcher.add<Asteroid, Asteroid>(&collision);
    dispatcher.add<Asteroid, Ship>    (&collision);
    dispatcher.add<Ship,     Ship>    (&collision);

    // Base class pointers (no static type information about derived classes)
    Object* a = new Asteroid;
    Object* s = new Ship;

    // Invoke functions, let dispatcher choose the correct overload
    dispatcher.call(a, s); // Output: "Asteroid-Ship"
    dispatcher.call(a, a); // Output: "Asteroid-Asteroid"
    dispatcher.call(s, s); // Output: "Ship-Ship"
   
    delete a;
    delete s;
}

Aurora uses the zlib/libpng license. Since it is a header-only library, it requires no build process and can thus be very easily be used in your projects.

Links
Aurora project homepage
GitHub page
« Last Edit: March 25, 2012, 09:53:47 am by Laurent »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Silvah

  • Guest
Aurora
« Reply #1 on: March 19, 2012, 06:59:52 pm »
Could you change the namespace name? It's not like typing three characters less is going to save the world, and if it is, one can always use an alias. This library has nothing to do with AUR, but looking at the namespace one could think otherwise.

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Aurora
« Reply #2 on: March 20, 2012, 05:47:56 am »
Should've called it Odin or something, to tie into Thor. :D

I wonder If it'd be possible to modify the dispatcher to work with run time defined object times(scripted entities)?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Aurora
« Reply #3 on: March 20, 2012, 11:45:50 am »
Quote from: "Silvah"
This library has nothing to do with AUR, but looking at the namespace one could think otherwise.
Thanks for the hint, I wasn't aware of the ambiguity. But is it really an issue? Because 6 characters is really a bit long for something you always type, I mean Laurent even shortened sfml to sf ;)

Quote from: "thePyro_13"
Should've called it Odin or something, to tie into Thor.
Theoretically, I could still change the name, this would also solve the namespace problem :P But apart from that, "Aurora" actually pleases me (and since it's not directly related to Thor, it needn't be another Germanic god, Roman goddess is also fine ;))

Quote from: "thePyro_13"
I wonder If it'd be possible to modify the dispatcher to work with run time defined object times(scripted entities)?
Could you make an example? I think it might require bigger refactoring if the objects aren't represented by C++ classes...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Silvah

  • Guest
Aurora
« Reply #4 on: March 20, 2012, 04:49:01 pm »
Quote from: "Nexus"
Because 6 characters is really a bit long for something you always type, I mean Laurent even shortened sfml to sf ;)
boost::filesystem? As I said, one can always use an alias (e.g. "namespace foo = long_namespace_name"). Not to mention that modern IDEs have a nice feature called autocompletion ;)

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Aurora
« Reply #5 on: March 21, 2012, 02:57:43 am »
Quote from: "Nexus"
Quote from: "thePyro_13"
I wonder If it'd be possible to modify the dispatcher to work with run time defined object times(scripted entities)?
Could you make an example? I think it might require bigger refactoring if the objects aren't represented by C++ classes...
The objects would only have a generic C++ type. They differentiate themselves using identifiers provided from the script.

After actually looking at how dispatch works, it probably wouldn't do to try and shoehorn it for this situation. I'll have to make something more specific IMO.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Aurora
« Reply #6 on: March 21, 2012, 11:31:13 am »
Couldn't this be achieved relatively easy with a std::map<ObjectIdentifier, std::function<X>>?

My dispatchers essentially do the same thing, where ObjectIdentifier is the std::type_info of the object. In this respect, it might even be possible to generalize the dispatchers.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Orwel

  • Full Member
  • ***
  • Posts: 208
    • View Profile
Aurora
« Reply #7 on: March 21, 2012, 08:32:04 pm »
Somebody have test example. Dont work for me  :cry:

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Aurora
« Reply #8 on: March 25, 2012, 09:51:18 am »
Sorry, the Dispatcher example was incorrect. I reduced it from a bigger example and introduced some mistakes :-\

Should be okay now. The smart pointer example was already correct before.
« Last Edit: March 25, 2012, 12:17:20 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Orwel

  • Full Member
  • ***
  • Posts: 208
    • View Profile
Re: Aurora
« Reply #9 on: April 05, 2012, 11:19:57 pm »
Thank, now this example is perfect. You can add this line to show symetry is possible(cf doc):
Code: [Select]
dispatcher.call(s, a); // Output: "Asteroid-Ship"
I had tried to read code to understand how run dispatcher, but i didn't understand  :-[

You can make a short and simple explanation, please  ;D

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: Aurora
« Reply #10 on: April 06, 2012, 01:28:17 pm »
I especially like the dispatcher. But how's the smart pointer any different from the generic solutions in C++11 and Boost?

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: Aurora
« Reply #11 on: April 06, 2012, 05:20:04 pm »
I especially like the dispatcher. But how's the smart pointer any different from the generic solutions in C++11 and Boost?

As I understand it, it copies the value into a new heap allocated object. shared_ptr copies the reference and increments a counter, unique_ptr can't copy but can move the reference.
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

Silvah

  • Guest
Re: Aurora
« Reply #12 on: April 06, 2012, 06:38:08 pm »
Time to try again, I guess.

Quote from: Nexus
Because 6 characters is really a bit long for something you always type, I mean Laurent even shortened sfml to sf ;)
boost::filesystem? As I said, one can always use an alias (e.g. "namespace foo = long_namespace_name"). Not to mention that modern IDEs have a nice feature called autocompletion ;)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Aurora
« Reply #13 on: April 06, 2012, 06:59:58 pm »
I had tried to read code to understand how run dispatcher, but i didn't understand  :-[
Have you already read the documentation? What parts exactly don't you understand?

But how's the smart pointer any different from the generic solutions in C++11 and Boost?
The C++11 standard library and Boost don't provide any smart pointers with deep copy semantics at all. That is, you cannot copy the referenced object in general. The aur::CopiedPtr<T> class template is a wrapper around raw pointers with the intention to treat them like normal objects with value semantics. I've just uploaded a draft of the tutorial, there I explain some of the details in case you're interested.

Time to try again, I guess.
I have acknowledged it, I'm still reflecting whether I should change the namespace name or not. I'd also appreciate feedback from other users :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: Aurora
« Reply #14 on: April 08, 2012, 12:39:45 pm »
Yeah, but one can always do that via

std::shared_ptr<Foo> copy( new Foo( *original ) );

(works for other smart pointers in a similar/an equal way, of course) Even easier would be a free function like "clone()" that works with every smart pointer.

Imho it's getting too specialized when doing a separate type for copy semantics for heap-allocated objects. And copying has, in my understanding, not really something to do with the type of a pointer. Could be personal preference though. :)