gpt4 book ai didi

c++ - 指向基于虚拟接口(interface)的可变参数模板类的指针

转载 作者:行者123 更新时间:2023-11-30 03:24:12 27 4
gpt4 key购买 nike

我有一个可变参数模板类,它实现了基类中定义的 4 个方法 iprocessing_component_manager .这 4 个方法管理组件的生命周期,例如初始化、准备和回收和/或处置。完全实现的接口(interface) tuple_processing_component_manager将组件类型作为可变参数。

组件管理器是执行组件序列的 Controller 的成员。

以下代码是我的 Controller 的基本框架,目前运行良好。它有一个可能的组件管理器实现,这里我只展示一个简单的实现来说明:

#include "utilities.h"
#include <cstdlib>
#include <iostream>
#include <tuple>

// https://stackoverflow.com/questions/42255534 (Yakk)
namespace notstd {
template<class T> struct tag_t { constexpr tag_t() {}; using type=T; };
template<class T> constexpr tag_t<T> tag{};
template<class Tag> using type_t = typename Tag::type;

template<class...Ts, class F>
void for_each_type(F&& f) {
using discard=int[];
(void)discard{ 0,(void(
f( tag<Ts> )
),0)...};
}
}

// A component
class icomponent {
public:
virtual std::string id() = 0;
virtual ~icomponent() = default;

virtual void init() = 0;
virtual void dispose() = 0;
};

class component_base : public icomponent
{
public:
virtual ~component_base() = default;

virtual void init()
{
// ... init context
}

virtual void dispose()
{
// ...
}

// ... more
};

// Sample components
class component_a : public component_base {
public:
virtual std::string id() override { return "component a"; }
};
class component_b : public component_base {
public:
virtual std::string id() override { return "component b"; }
};
class component_c : public component_base {
public:
virtual std::string id() override { return "component c"; }
};

// Interface component manager
class iprocessing_component_manager {
public:
virtual ~iprocessing_component_manager() = default;

virtual void init() = 0;
virtual icomponent* prepare() = 0;
virtual void recycle(icomponent* p) = 0;
virtual void dispose() = 0;
};

// Implementation component manager
template<typename T>
class type_processing_component_manager
: public iprocessing_component_manager {
public:
virtual ~type_processing_component_manager() = default;

virtual T* prepare() override
{
// Default create T or fetch from a object pool, etc ...
return new T;
}
};

// Implementation virt. methods component mgr
template<typename ... Ts>
class tuple_processing_component_manager
: public type_processing_component_manager<Ts>... {
public:
virtual ~tuple_processing_component_manager() = default;

virtual void init() override
{

}

template<typename T>
T* prepare()
{
return type_processing_component_manager<T>::prepare();
}

virtual void recycle(icomponent* p) override
{
// Delete pointer or return to an object pool, etc
delete p;
}

virtual void dispose() override
{

}
};

// The controller
template <typename ...Ts>
class controller {
std::unique_ptr<tuple_processing_component_manager<Ts...>> m_component_manager;
// iprocessing_component_manager* m_component_manager;

public:
controller()
: m_component_manager(std::make_unique<tuple_processing_component_manager<Ts...>>())
// : m_component_manager(new tuple_processing_component_manager<Ts...>())
{
}
~controller() = default;

// Do some initialization
void init()
{
m_component_manager->init();
}

// Process components
void process()
{
// A simple loop over components.
notstd::for_each_type<Ts...>([&](auto tag) {
using component_t = notstd::type_t<decltype(tag)>;

component_t* x = m_component_manager->template prepare<component_t>();
// Do some processing, here I just print the component id
std::cout << x->id() << "\n";

// Recycle.
m_component_manager->recycle(x);
});
}

// ... more stuff
};

执行 Controller 看起来像这样

int main(int argc, char** argv)
{
controller<component_a, component_c> c;
c.init();
c.process();

return 0;
}

我的问题出在controller类和 m_component_manager成员(member)。

如果我构造一个 Controller 并初始化 m_component_manager显然,以下工作没有任何问题:

std::unique_ptr<tuple_processing_component_manager<Ts...>> m_component_manager = std::make_unique<tuple_processing_component_manager<Ts...>>();

我将如何更改 m_component_manager 的定义?采取 component_manager 的任何可变实现和/或需要进行哪些调整才能实现这一目标?我尝试使用 std::unique_ptr<iprocessing_component_manager>但这肯定行不通。尽管如此,我还是尝试改变了一些东西,但得到了 multiple inheritance ambiguity错误或 no unique final overrider for iprocessing_component_manager::prepare() .

在后面的步骤中,我希望能够将组件管理器的实例作为参数传递到 Controller 构造函数中,只是为了更灵活一些。

所以我不确定这是否有可能实现我想要实现的目标。有什么方法可以解决这个问题吗?我对可变参数模板和元模板编程不是很有经验。所以我想知道是否有人可以告诉我如何做到这一点。

已经尝试搜索类似的问题,但找不到任何密切相关的内容。

非常感谢任何帮助。感谢您提前抽出时间!

最佳答案

我将试一试并与您一起学习。我一直在研究引擎盖下的 spirit x3 兔子洞。

因此,据我了解,您不必拥有继承权。您绝对不需要像使用派生对象的混合容器那样在堆上共享指针和对象。不同类型的真实对象可以存储在一个元组中。

我今天的工作即将完成,但这是我目前所掌握的内容。所有你的东西,但有一个元组。

controller::init 中,我迭代元组以调用方法 say。接下来我需要做的是弄清楚如何删除常量限定符,以便您可以自由修改对象。这是您要找的吗?

#include "stdafx.h"
//#include "utilities.h"
#include <cstdlib>
#include <iostream>
#include <tuple>

//https://stackoverflow.com/questions/26902633/how-to-iterate-over-a-stdtuple-in-c-11
template<class F, class...Ts, std::size_t...Is>
void for_each_in_tuple(const std::tuple<Ts...> & tuple, F func, std::index_sequence<Is...>) {
using expander = int[];
(void)expander {
0, ((void)func(std::get<Is>(tuple)), 0)...
};
}

template<class F, class...Ts>
void for_each_in_tuple( std::tuple<Ts...> & tuple, F func) {
for_each_in_tuple(tuple, func, std::make_index_sequence<sizeof...(Ts)>());
}


// https://stackoverflow.com/questions/42255534 (Yakk)
namespace notstd {
template<class T> struct tag_t { constexpr tag_t() {}; using type = T; };
template<class T> constexpr tag_t<T> tag{};
template<class Tag> using type_t = typename Tag::type;

template<class...Ts, class F>
void for_each_type(F&& f) {
using discard = int[];
(void)discard {
0, (void(
f(tag<Ts>)
), 0)...
};
}
}

// A component
class icomponent {
public:
virtual std::string id() = 0;
virtual ~icomponent() = default;

virtual void init() = 0;
virtual void dispose() = 0;

void say() const {
std::cout << "say: " << typeid(*this).name() << " ANDNAME: " << /*id( ).c_str( )*/"???" << std::endl;
}
};

class component_base : public icomponent
{
public:
virtual ~component_base() = default;

virtual void init()
{
// ... init context
}

virtual void dispose()
{
// ...
}

// ... more
};

// Sample components
class component_a : public component_base {
public:
virtual std::string id() override { return "component a"; }
};
class component_b : public component_base {
public:
virtual std::string id() override { return "component b"; }
};
class component_c : public component_base {
public:
virtual std::string id() override { return "component c"; }
};

// Interface component manager
class iprocessing_component_manager {
public:
virtual ~iprocessing_component_manager() = default;

virtual void init() = 0;
virtual icomponent* prepare() = 0;
virtual void recycle(icomponent* p) = 0;
virtual void dispose() = 0;
};

// Implementation component manager
template<typename T>
class type_processing_component_manager
: public iprocessing_component_manager {
public:
virtual ~type_processing_component_manager() = default;

virtual T* prepare() override
{
// Default create T or fetch from a object pool, etc ...
return new T;
}
};

// Implementation virt. methods component mgr
template<typename ... Ts>
class tuple_processing_component_manager
: public type_processing_component_manager<Ts>... {
public:
virtual ~tuple_processing_component_manager() = default;

virtual void init() override
{
std::cout << "init: " << typeid(*this).name() << std::endl;

}

template<typename T>
T* prepare()
{
return type_processing_component_manager<T>::prepare();
}

virtual void recycle(icomponent* p) override
{
// Delete pointer or return to an object pool, etc
delete p;
}

virtual void dispose() override
{

}
};

// The controller
template <typename... Ts>
class controller {

std::tuple< Ts...> tups;

public:
controller()
{
}
~controller() = default;

// Do some initialization
// https://stackoverflow.com/questions/16387354/template-tuple-calling-a-function-on-each-element
void init()
{
for_each_in_tuple(tups, [](auto &x) { x.say(); });
// m_component_manager->init();
}

// Process components
void process()
{
// A simple loop over components.

//but this does not get the stored objects!
//notstd::for_each_type<Ts...>([&](auto tag) {
// using component_t = notstd::type_t<decltype(tag)>;

}

// ... more stuff
};

int main(int argc, char** argv)
{
controller<component_a, component_c> c;
c.init();
c.process();

return 0;
}

关于c++ - 指向基于虚拟接口(interface)的可变参数模板类的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49858505/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com