我要实现的目的是跟踪我们创建的从基类继承的对象的类型。如果一个类是从基类继承的,但是没有在对象中实例化,那么我对跟踪它就不感兴趣(是否可以包含此条件,取决于实现是否容易)
虚拟示例:
template <typename T>
class Person
{
public:
Person() {
T* x;
container.push_back(x);
}
virtual ~Person() {}
private:
static heterogeneous_container container;
};
class Employee : public Person <Employee>
{
};
class Employee2 : public Employee
{
};
此外,我希望它可用于链式继承。当实例化一个Employee2时,基类Person是否有可能在容器中添加一个Employee2类型的指针?
对于异构容器,我认为可以使用link
答案 0 :(得分:3)
我认为您想要的更像是
class Person
{
public:
Person() {
objects.push_back(this);
}
virtual ~Person() {
objects.erase(this);
}
private:
static std::set<const Person*> objects;
};
class Employee : public Person
{
};
class Employee2 : public Employee
{
};
使用这种方法,您可以查询容器中每个指针所指向的最衍生对象的动态类型。
请注意,objects
集必须包含每个对象的指针,而不是type_info。问题在于,在Person
对象的Employee2
子对象的构造函数中,*this
的最派生类型将是Person
,而不是{{1} }(在执行程序进入Employee2
构造函数之前,它不会变成Employee2
。
答案 1 :(得分:1)
或多或少,我在某处工作:
#include <iostream>
#include <functional>
#include <vector>
struct ClassEntry {
size_t id = 0;
const char* label;
};
class BaseClass {
public:
protected:
static void RegisterType(size_t id, const char * label) {
ClassEntry entry;
entry.id = id;
entry.label = label;
mRegisteredTypes.emplace_back(entry);
std::cout << "Registered type " << id << " label " << label << std::endl;
}
static size_t createId() {
static size_t id = 0;
return id++;
}
static std::vector<ClassEntry> mRegisteredTypes;
};
std::vector<ClassEntry> BaseClass::mRegisteredTypes;
class OneTimeCall {
public:
OneTimeCall(std::function<void(void)>&& func) {
func();
}
virtual ~OneTimeCall() {
}
};
template<typename T>
class MyClass : public BaseClass {
public:
MyClass() {
static OneTimeCall one_time {
[this]{
BaseClass::RegisterType(GetId(), T::GetType());
}
};
}
private:
protected:
static size_t GetId() {
static size_t id = BaseClass::createId();
return id;
}
};
class A : public MyClass<A> {
public:
A() {
}
static const char *GetType() {
return "ClassA";
}
};
class B : public MyClass<B> {
public:
B() {
}
static const char *GetType() {
return "ClassB";
}
};
int main() {
A a;
B b;
A a2;
B b2;
return 0;
}
输出为:
Registered type 0 label ClassA
Registered type 1 label ClassB
主要思想是在构造过程中仅一次使用CRTP和静态初始化来注册每种类型。它在Linux中可以正常工作,在Windows编译器上,每个DLL上的静态BaseClass ID都是新的,因此您需要进行一些调整以在外部库中使用。
使用这种方法,您不需要任何外部库,并且无需rtti即可进行编译。
对于继承,您可以创建一个新类:
template<typename Current, typename Base>
class Mix : public MyClass<Current>, public Base {};
因此,如果您将“类型C”作为当前类型(CRTP)传递,并将类型A作为基类传递,则可以使用。
class C : public Mix<C, A> {
public:
C() {
}
static const char *GetType() {
return "ClassC";
}
};
使用这种方法,如果您先前已经注册了“ A”,它将不再被注册;如果您没有“ A”,它将被注册在“ C”之后。
答案 2 :(得分:0)
跟踪对象的一种方法是将它们存储在入侵列表中,并将链接节点嵌入到对象中。这为跟踪器操作提供了noexcept
的保证,并且在将元素插入到跟踪器容器中时不需要额外的内存分配,这是每个跟踪对象中嵌入式列表节点(两个指针)的价格:
#include <iostream>
#include <boost/intrusive/list.hpp>
namespace bi = boost::intrusive;
template<class T>
class Tracker : public bi::list_base_hook<bi::link_mode<bi::auto_unlink>>
{
protected:
static bi::list<Tracker, bi::constant_time_size<false>> objects_;
Tracker() noexcept { objects_.push_back(*this); }
Tracker(Tracker const&) noexcept { objects_.push_back(*this); }
public:
static auto count() noexcept { return objects_.size(); }
};
template<class T>
bi::list<Tracker<T>, bi::constant_time_size<false>> Tracker<T>::objects_;
struct Employee : Tracker<Employee> {};
struct Employee2 : Employee {};
int main() {
std::cout << Tracker<Employee>::count() << '\n';
{
Employee e0;
Employee2 e1;
std::cout << Tracker<Employee>::count() << '\n';
}
std::cout << Tracker<Employee>::count() << '\n';
}
输出:
0
2
0
Tracker
(不带Boost库)
struct AutoListNode {
AutoListNode *next_ = this, *prev_ = this;
AutoListNode() noexcept = default;
AutoListNode(AutoListNode const&) = delete;
AutoListNode& operator=(AutoListNode const&) = delete;
~AutoListNode() noexcept { this->erase(); }
void push_back(AutoListNode* node) noexcept {
auto prev = prev_;
node->prev_ = prev;
node->next_ = this;
prev->next_ = node;
prev_ = node;
}
void erase() noexcept {
auto next = next_;
auto prev = prev_;
prev->next_ = next;
next->prev_ = prev;
}
size_t size() const noexcept {
size_t count = 0;
for(auto node = next_; node != this; node = node->next_)
++count;
return count;
}
};
template<class T>
class Tracker : private AutoListNode
{
protected:
static AutoListNode objects_;
Tracker() noexcept { objects_.push_back(this); }
Tracker(Tracker const&) noexcept { objects_.push_back(this); }
public:
static auto count() noexcept { return objects_.size(); }
};
template<class T>
AutoListNode Tracker<T>::objects_;