Counting With Template Metaprogramming?

ⅰ亾dé卋堺 提交于 2019-11-30 13:06:16

No. This is a problem that comes up in practice quite a lot, and as far as I'm aware there are only two solutions:

  1. Manually assign IDs to each derived class.
  2. Dynamically and lazily generate IDs non-deterministically.

The way you do the second one is something like this:

class Base
{
    virtual int getId() const = 0;
};

// Returns 0, 1, 2 etc. on each successive call.
static int makeUniqueId()
{
    static int id = 0;
    return id++;
}

template <typename Derived>
class BaseWithId : public Base
{
    static int getStaticId()
    {
        static int id = makeUniqueId();
        return id;
    }

    int getId() const { return getStaticId(); }
};

class Derived1 : public BaseWithId<Derived1> { ... };
class Derived2 : public BaseWithId<Derived2> { ... };
class Derived3 : public BaseWithId<Derived3> { ... };

This gives you unique IDs for each class:

Derived1::getStaticId(); // 0
Derived2::getStaticId(); // 1
Derived3::getStaticId(); // 2

However, those IDs are assigned lazily, so the order you call getId() affects the ID's returned.

Derived3::getStaticId(); // 0
Derived2::getStaticId(); // 1
Derived1::getStaticId(); // 2

Whether or not this is OK for your application depends on your particular needs (e.g. would be no good for serialisation).

Is it possible to use template metaprogramming (or any other mechanism with the C++ language) to count the number of classes which are derived from some base class such that each derived class is given a unique, static class identifier?

No, there's no such mechanism. No matter what you do, you'll have to add "something" (most likely a macro) to every derived class manually to achieve something like that. See Qt 4 and Q_OBJECT macro. You could also make a macros for making derived classes, but this can't be done automatically.

You could however write your own C++ code preprocessor/analysis tool that scans source code you provided and then inserts necessary directives into source.

Also, RTTI provides name for every class. The problem is that this name is implementation-specific, so it isn't very useful.

I am posting this with my problem in mind. It will be long post. I am writing event system, and I want to register events only on one place.

-----Event.h-----

typedef int EventAddress;
typedef int EventId;
typedef int EventType;

static const EventAddress EVENT_FROM_ALL=-1;
static const EventAddress EVENT_TO_ALL=-1;

static const EventId EVENT_ID_INITIAL=-1;
static const EventType EVENT_TYPE_INITIAL=-1;

static const EventId EVENT_ID_ALL=0;
static const EventType EVENT_TYPE_ALL=0;

struct Event
{
    public:
        EventId eventId;
        EventType eventType;
        EventAddress from;

        Event(const EventId eventId, const EventType eventType):
            eventId(eventId),
            eventType(eventType)
        {
        }

        virtual ~Event()
        {
        }

        virtual std::string asString()=0;

    private:
        Event();
};

template <class T>
struct EventBase
        :public Event
{
    static int EVENT_ID;
    static int EVENT_TYPE;

    EventBase():
        Event(EVENT_ID,EVENT_TYPE)
    {
    }
};
template <class T>
int EventBase<T>::EVENT_ID=EVENT_ID_INITIAL;

template <class T>
int EventBase<T>::EVENT_TYPE=EVENT_TYPE_INITIAL;

/// Events All
struct EventAll:
        public Event
{
    static int EVENT_ID;
    static int EVENT_TYPE;

    EventAll():
        Event(EVENT_ID,EVENT_TYPE)
    {
    }

    virtual std::string asString()
    {
        return __PRETTY_FUNCTION__;
    }
};

-----Event.cpp-----

#include "Event.h"

int EventAll::EVENT_ID=EVENT_ID_ALL;
int EventAll::EVENT_TYPE=EVENT_TYPE_ALL;

------EventGenerator.h------

struct EventIdGenerator
{
    int generator;
    EventIdGenerator():
        generator(0)
    {

    }
};

template <class T, class Base>
struct UnitId:
        virtual public Base,
        public T
{
    UnitId()
    {
        ++Base::generator;
        T::EVENT_ID=Base::generator;
    }
};

struct EventTypeGenerator
{
    static int generator;
};

template <class T, class Base>
struct UnitType:
        virtual public Base,
        public T
{
    UnitType()
    {
        T::EVENT_TYPE=Base::generator;
    }
};

-----EventGenerator.cpp-----

#include "EventGenerator.h"

int EventTypeGenerator::generator=0;

And not the fun stuff...

-----EventsTank.h-----

#include <loki/Typelist.h>
#include <loki/HierarchyGenerators.h>

#include "Event.h"
#include "EventGenerator.h"

#define EVENT_CONTEXT__ Tank

#define EVENT_NAME__ EventTank1
struct EVENT_NAME__:
        public EventBase<EVENT_NAME__>
{
    std::string s;
    double b;
    void f()
    {
    }

    virtual std::string asString()
    {
        return __PRETTY_FUNCTION__;
    }
};
#undef EVENT_NAME__



#define EVENT_NAME__ EventTank2
struct EVENT_NAME__:
        public EventBase<EVENT_NAME__>
{
    std::string s;
    double b;
    void f()
    {
    }

    virtual std::string asString()
    {
        return __PRETTY_FUNCTION__;
    }
};
#undef EVENT_NAME__



#define EVENT_NAME__ EventTank3
struct EVENT_NAME__:
        public EventBase<EVENT_NAME__>
{
    std::string s;
    double b;
    void f()
    {
    }

    virtual std::string asString()
    {
        return __PRETTY_FUNCTION__;
    }
};
#undef EVENT_NAME__

#define TOKENPASTE(x, y, z) x ## y ## z
#define TOKENPASTE2(x, y, z) TOKENPASTE(x, y, z)

#define EVENTS_ALL__ TOKENPASTE2(Events,EVENT_CONTEXT__,All)


template <typename...Ts>
struct TYPELIST;

template <>
struct TYPELIST<>
{
    typedef Loki::NullType Result;
};

template <typename HEAD, typename...Ts>
struct TYPELIST<HEAD,Ts...>
{
    typedef Loki::Typelist<HEAD, typename TYPELIST<Ts...>::Result> Result;
};

typedef TYPELIST<
        EventTank1,
        EventTank2,
        EventTank3
    >::Result EVENTS_ALL__;

/// Do not change below---------------------------------------------------------------------

#define EVENT_CONTEXT_ALL__ TOKENPASTE2(Event,EVENT_CONTEXT__,All)
struct EVENT_CONTEXT_ALL__:
        public EventBase<EVENT_CONTEXT_ALL__>
{
    virtual std::string asString()
    {
        return __PRETTY_FUNCTION__;
    }
};

#define EVENT_ALL_REVERSED__ TOKENPASTE2(Event,EVENT_CONTEXT__,AllReversed)
typedef Loki::TL::Reverse<EVENTS_ALL__>::Result EVENT_ALL_REVERSED__;

#define EVENT_ALL_REVERSED_FIRST__ TOKENPASTE2(Event,EVENT_CONTEXT__,AllReversedFirst)
typedef Loki::TL::TypeAt<EVENTS_ALL__,0>::Result EVENT_ALL_REVERSED_FIRST__;

template <class Base>
struct UnitType<EVENT_ALL_REVERSED_FIRST__,Base>:
        virtual public Base,
        public EVENT_ALL_REVERSED_FIRST__
{
    typedef EVENT_ALL_REVERSED_FIRST__ T;
    UnitType()
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
        ++Base::generator;
        T::EVENT_TYPE=Base::generator;
        EVENT_CONTEXT_ALL__::EVENT_ID=EVENT_ID_ALL;
        EVENT_CONTEXT_ALL__::EVENT_TYPE=Base::generator;
    }
};

#define ALL_CONTEXT_EVENTS__ TOKENPASTE2(All,EVENT_CONTEXT__,Events)
typedef Loki::GenLinearHierarchy<EVENT_ALL_REVERSED__,UnitType,EventTypeGenerator> ALL_CONTEXT_EVENTS__;

#undef ALL_CONTEXT_EVENTS__
#undef EVENT_ALL_REVERSED__
#undef EVENT_ALL_REVERSED_FIRST__
#undef EVENT_NAME_ALL__
#undef EVENTS_ALL__

-----EventsTank.cpp-----

#include "EventsTank.h"

AllTankEvents allTankEvents;

-----EventRegisterer.cpp-----

#include <loki/Typelist.h>
#include <loki/HierarchyGenerators.h>

#include "../core/Event.h"

#include "EventsTank.h"

typedef Loki::GenLinearHierarchy<Loki::TL::Reverse<EventsTankAll>::Result,UnitId,EventIdGenerator> AllEvents;
AllEvents allEvents;

Since this is a lot of code, I will try to summarize. I have a base class EventBase which has two important members: EVENT_ID and EVENT_TYPE. What I am doing is to meta-compose two classes: AllTankEvents which upon instatiation initialize EVENT_TYPE for TankEvents, and AllEvents initialize EVENT_ID. What user of this piece of crap needs to do, is to add another Tank Event definition, and to add it to EVENTS_ALL__ typelist. You can dispatch events with code like if (event.EVENT_ID==EventTank1::EVENT_ID) and so on. Other code can watch out for EVENT_ID/EVENT_TYPE initialized with EVENT_ID_INITIAL/EVENT_TYPE_INITIAL and assert. Don't be afraid of C pre-processor macros. They a just the sugar, so I can automate some tasks. Take a look, I have to go now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!