Question

Is there a way to check what the type_info of a superclass is? In my system objects are identified by a set of bits. Which is Identified by the type_info hashcode. I would like some of these types to enable polymorphism in a sense that my system thinks they are under the same bit. type_info creates a unique hashcode for every class type. Instead I want the derived classes to take the bit set of their superclass like so:

Component > ISomeInterfaceComponent -> bit = 00001
              |
               \ ComponentContextA  -> bit = ISomeInterFaceComponent bit
              |
              \  ComponentContextB  -> bit = ISomeInterFaceComponent bit
              |
              \  ComponentContextC  -> bit = ISomeInterFaceComponent bit

This should add the object containing the components to one system for processing.

As of right now this is what happens:

Component > ISomeInterFaceComponent -> bit = 00001
              |
               \ ComponentContextA  -> bit = 00010
              |
              \  ComponentContextB  -> bit = 00100
              |
              \  ComponentContextC  -> bit = 01000

Which demands that I create different systems for all components.

If anyone could give me pointers on how I could achieve this that would be great.

Edit: To prevent confusion to get a bit set for a type it goes as follows: ComponentTypeManager::getBit();

So I'm not working with instances. And I would love to keep the current system in lock.

Was it helpful?

Solution

There isn't any automatic way that I'm aware of. However, if I'm understanding you, it can be done via little additional effort.

For each of your 'base' classes, add a typedef that refers to itself, and use that instead in the typeid(). Note, subclasses could choose to override it. If they do so, then they will get their own ID, and their children will use their value.

Note, another solution, if you don't want to use typeid() at all is to have a static member (or member function) on the base classes that returns the proper value for the base, and then invoke that directly in your getBit() function.

#include <iostream>
#include <iomanip>
#include <typeinfo>

struct Base1 
{
   typedef Base1 Base;

};

struct Base2
{
   typedef Base2 Base;
};

struct Derived1A : public Base1 { };   
struct Derived1B : public Base1 { };    
struct Derived2A : public Base2 { };    
struct Derived2B : public Base2 { };

template <typename T>
std::size_t getBit()
{
   // Do whatever you normally do here, but use T::Base instead of T
   return std::hash<std::string>()(typeid(typename T::Base).name());
}

int main()
{ 
   std::cout << std::boolalpha << getBit<Derived1A>() << " == " << getBit<Derived1B>() << " " << (getBit<Derived1A>() == getBit<Derived1B>()) << "\n";

   std::cout << std::boolalpha << getBit<Derived2A>() << " == " << getBit<Derived2B>() << " " <<(getBit<Derived2A>() == getBit<Derived2B>()) << "\n";

}

http://ideone.com/6ad08

OTHER TIPS

Why not just create a variable in the base class ISomeInterFaceComponent that is not modified by any derived classes (unless you specifically want it to be). It doesn't sound like you want polymorphism as you want the bit value to be the same regardless of derived class type, rather than to vary based on type. If in future you need to override if for a certain derived type, you can still do this specifically for that class.

Alternatively you could use a virtual function in the base class that returns an int (the bit value - or whatever type this actually is) and simply do not override it in the derived classes that you want to have the same bit value as the base.

For example

 class ISomeInterFaceComponent {
      virtual int GetBit() { return 1;};  // return bit value
 };


 class ComponentContextA : public ISomeInterFaceComponent {
      // Do not override GetBit() - let the base class function get called.
 };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top