Class DrObject

Synopsis

#include <lib/inc/drogon/DrObject.h>

template <typename T>
class DrObject : public virtual DrObjectBase

Description

a class template to implement the reflection function of creating the class object by class name

Inheritance

Ancestors: DrObjectBase

Decsendents: Plugin, HttpFilter, HttpController, WebSocketController, HttpSimpleController, DrTemplate

Methods

DrObject
classNameGet the class name.
classTypeName
isClassReturn true if the class name is 'class_name'.

Source

Lines 62-119 in lib/inc/drogon/DrObject.h.

template <typename T>
class DrObject : public virtual DrObjectBase
{
  public:
    virtual const std::string &className() const override
    {
        return alloc_.className();
    }
    static const std::string &classTypeName()
    {
        return alloc_.className();
    }

    virtual bool isClass(const std::string &class_name) const override
    {
        return (className() == class_name);
    }

  protected:
    // protect constructor to make this class only inheritable
    DrObject()
    {
    }

  private:
    class DrAllocator
    {
      public:
        DrAllocator()
        {
            registerClass<T>();
        }
        const std::string &className() const
        {
            static std::string className =
                DrClassMap::demangle(typeid(T).name());
            return className;
        }
        template <typename D>
        typename std::enable_if<std::is_default_constructible<D>::value,
                                void>::type
        registerClass()
        {
            DrClassMap::registerClass(className(),
                                      []() -> DrObjectBase * { return new T; });
        }
        template <typename D>
        typename std::enable_if<!std::is_default_constructible<D>::value,
                                void>::type
        registerClass()
        {
            LOG_ERROR << "Can't register classes without a default constructor";
        }
    };

    // use static val to register allocator function for class T;
    static DrAllocator alloc_;
};





Add Discussion as Guest

Log in to DocsForge