drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
DrClassMap.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <drogon/exports.h>
18#include <trantor/utils/Logger.h>
19#include <functional>
20#include <memory>
21#include <mutex>
22#include <thread>
23#include <unordered_map>
24#include <vector>
25#include <type_traits>
26#include <cstdlib>
27#ifndef _MSC_VER
28#include <cxxabi.h>
29#endif
30#include <stdio.h>
31
32namespace drogon
33{
34class DrObjectBase;
35using DrAllocFunc = std::function<DrObjectBase *()>;
36using DrSharedAllocFunc = std::function<std::shared_ptr<DrObjectBase>()>;
37
41class DROGON_EXPORT DrClassMap
42{
43 public:
50 static void registerClass(const std::string &className,
51 const DrAllocFunc &func,
52 const DrSharedAllocFunc &sharedFunc = nullptr);
53
60 static DrObjectBase *newObject(const std::string &className);
61
65 static std::shared_ptr<DrObjectBase> newSharedObject(
66 const std::string &className);
67
75 static const std::shared_ptr<DrObjectBase> &getSingleInstance(
76 const std::string &className);
77
85 template <typename T>
86 static std::shared_ptr<T> getSingleInstance()
87 {
88 static_assert(std::is_base_of<DrObjectBase, T>::value,
89 "T must be a sub-class of DrObjectBase");
90 static auto const singleton =
91 std::dynamic_pointer_cast<T>(getSingleInstance(T::classTypeName()));
92 assert(singleton);
93 return singleton;
94 }
95
101 static void setSingleInstance(const std::shared_ptr<DrObjectBase> &ins);
102
108 static std::vector<std::string> getAllClassName();
109
116 static std::string demangle(const char *mangled_name)
117 {
118#ifndef _MSC_VER
119 std::size_t len = 0;
120 int status = 0;
121 std::unique_ptr<char, decltype(&std::free)> ptr(
122 __cxxabiv1::__cxa_demangle(mangled_name, nullptr, &len, &status),
123 &std::free);
124 if (status == 0)
125 {
126 return std::string(ptr.get());
127 }
128 LOG_ERROR << "Demangle error!";
129 return "";
130#else
131 auto pos = strstr(mangled_name, " ");
132 if (pos == nullptr)
133 return std::string{mangled_name};
134 else
135 return std::string{pos + 1};
136#endif
137 }
138
139 protected:
140 static std::unordered_map<std::string,
141 std::pair<DrAllocFunc, DrSharedAllocFunc>> &
142 getMap();
143};
144} // namespace drogon
A map class which can create DrObjects from names.
Definition DrClassMap.h:42
static std::string demangle(const char *mangled_name)
demangle the type name which is returned by typeid(T).name().
Definition DrClassMap.h:116
static void registerClass(const std::string &className, const DrAllocFunc &func, const DrSharedAllocFunc &sharedFunc=nullptr)
Register a class into the map.
static DrObjectBase * newObject(const std::string &className)
Create a new instance of the class named by className.
static std::shared_ptr< T > getSingleInstance()
Get the singleton T type object.
Definition DrClassMap.h:86
static std::vector< std::string > getAllClassName()
Get all names of classes registered in the map.
static void setSingleInstance(const std::shared_ptr< DrObjectBase > &ins)
Set a singleton object into the map.
static std::shared_ptr< DrObjectBase > newSharedObject(const std::string &className)
Get the shared_ptr instance of the class named by className.
static const std::shared_ptr< DrObjectBase > & getSingleInstance(const std::string &className)
Get the singleton object of the class named by className.
The base class for all drogon reflection classes.
Definition DrObject.h:34
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23