Note that there are some explanatory texts on larger screens.

plurals
  1. POTransparent aggregation of classes
    primarykey
    data
    text
    <p>I'm trying to embed some game classes using a scripting language. Suppose my main structure is:</p> <pre><code>class BaseGameObject { public: virtual ~BaseGameObject() { } }; class DerivedGameObject : public virtual BaseGameObject { }; </code></pre> <p>And then, I want to attach information about how I bind this class on the script engine. I want to keep the cohesion with them as minimal as possible, so I've ended with this template pattern:</p> <pre><code>class ScriptLanguageBinder { public: virtual void bind(VM* vm) = 0; }; template &lt; typename GameObject &gt; class GameObjectLanguageBinder : public GameObject, public ScriptLanguageBinder { virtual void bind(VM* vm) { vm-&gt;bind&lt;GameObject&gt;(static_cast&lt;GameObject*&gt;(this)); } }; template &lt; typename GameObject &gt; BaseGameObject* gameObjectBuilder() { return new GameObjectLanguageBinder&lt;GameObject&gt;(); } </code></pre> <p>So I have a factory that build the "wrapped" BaseGameObject and returns the GameObjectLanguageBinder in place. This way, the rest of the system uses the resulting class as a BaseGameObject*, the script language dependency is confined in a set of classes that do not harm the Game ecosystem, and only my Script engine need to know that in fact it is a GameObjectLanguageBinder. This is working very well, aside one point:</p> <p>To get back the ScriptLanguageBinder object from the BaseGameObject* I need to do this:</p> <pre><code>ScriptLanguageBinder* binder = dynamic_cast&lt;ScriptLanguageBinder*&gt;(my_game_object); </code></pre> <p>Which is undesirable as I want to squeeze all performance I can from the code execution (It's a mobile game engine, so yes, performance counts).</p> <p>So I was wondering if there is some non-obtrusive, transparent way to achieve this <strong>without</strong> using dynamic cast. I thought in using a Visitor pattern, but this pattern is a blocker when you want to extend the classes you visit, so it's a no go.</p> <p>ps: the factory is <em>ultra</em> over-simplified on this example.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload