对更好的标题有什么建议吗?
在 Qt 中,信号和槽有一个很好的特性。但是,它会告诉您特定信号是否只能在运行时 (afc) 期间连接到特定插槽。
打算:
从模板创建一个包含“信号签名”(作为模板参数的函数指针)的类,以允许将给定签名(传递参数的数量和类型)的“插槽”仅连接到具有相似签名的“已定义”信号;
必须简单易用。
现在的问题: 我在 ISignalSlotMap 类中遇到“使用声明”的编译错误。 template multiple variadic inheritance with variadic argument types - 在这里它编译得很好。
还有,有什么办法可以简化模板算法吗?
更新:第一个 block 可以在没有dll的情况下编译运行
这可以在不链接到 DLL 的情况下编译
#include <iostream>
#include <type_traits>
#include <forward_list>
#include <memory>
//template wrapper
template <typename...>
struct TW
{};
//template to get Class type from pointer
template <class ReturnType, class ... ArgTypes>
constexpr ReturnType ClassFromPointer(void(ReturnType::*)(ArgTypes...));
//template to get pack of arguments' types
template <class ReturnType, class ... ArgTypes>
constexpr TW<ArgTypes...> ArgTypesPackFromPointer(void(ReturnType::*)(ArgTypes...));
template <auto ptr>
using FuncClass = decltype(ClassFromPointer(ptr));
template <auto ptr>
using FuncPack = decltype(ArgTypesPackFromPointer(ptr));
template <class ... ArgTypes>
struct Invoker
{
virtual void Invoke(ArgTypes ... args) = 0;
};
template <class ClType, class ... ArgTypes>
class InvokerImpl : public Invoker<ArgTypes...>
{
ClType *ptr_;
void(ClType::*pFunc_)(ArgTypes...);
public:
InvokerImpl(ClType* pObj, void(ClType::*pFunc)(ArgTypes...))
: ptr_(pObj),
pFunc_(pFunc)
{}
virtual void Invoke(ArgTypes ... args)
{
(ptr_->*pFunc_)(args...);
}
};
template <class ClType, class ... ArgTypes>
Invoker<ArgTypes...>* CreateInvoker(ClType* pObj, void(ClType::*pFunc)(ArgTypes...))
{
return new InvokerImpl<ClType, ArgTypes...>(pObj, pFunc);
}
template <class Pack>
class SlotContainerTranslated;
template <template <class ...> class Pack, class ... ArgTypes>
class SlotContainerTranslated<Pack<ArgTypes...>>
{
typedef std::unique_ptr<Invoker<ArgTypes...>> pInvoker;
std::forward_list<pInvoker> slots_;
public:
void AddInvoker(Invoker<ArgTypes...>* pInv)
{
slots_.push_front(std::move(pInvoker(pInv)));
}
void DispatchSignal(ArgTypes ... args)
{
auto start = slots_.begin();
while (start != slots_.end())
{
(*start)->Invoke(args...);
++start;
}
}
};
template <auto memfuncptr>
class ISlotContainer : SlotContainerTranslated<FuncPack<memfuncptr>>
{
public:
using SlotContainerTranslated<FuncPack<memfuncptr>>::AddInvoker;
using SlotContainerTranslated<FuncPack<memfuncptr>>::DispatchSignal;
};
template <auto ... memfuncPtrs>
class ISignalSlotMap : SlotContainerTranslated<FuncPack<memfuncPtrs>>...
{
public:
// using SlotContainerTranslated<FuncPack<memfuncPtrs>>::AddInvoker...;
// using SlotContainerTranslated<FuncPack<memfuncPtrs>>::DispatchSignal...;
};
////////////////////////////////////////////////////////////////////////
struct AlienSignals
{
void MindControl() {};
void MindControlPrint(int a, double b, int c, int d, const char* str) {};
void MindControlAdvise(int i, bool b) {};
};
struct Alien
{
static Alien* Invade();
virtual ISlotContainer<&AlienSignals::MindControlAdvise>& AccessSignal() = 0;
/*//this is what usage is expected to be like
virtual ISignalSlotMap<&AlienSignals::MindControl,
&AlienSignals::MindControlAdvise,
&AlienSignals::MindControlPrint>& AccessSignalMap() = 0;
*/
virtual ~Alien() = default;
};
class AlienImpl : public Alien
{
std::unique_ptr<ISlotContainer<&AlienSignals::MindControlAdvise>> signalMindControlAdvise_
{ new ISlotContainer<&AlienSignals::MindControlAdvise> };
// Inherited via Alien
virtual ISlotContainer<&AlienSignals::MindControlAdvise>& AccessSignal() override
{
return *signalMindControlAdvise_;
}
virtual ~AlienImpl() = default;
};
Alien * Alien::Invade()
{
return new AlienImpl;
}
struct Human
{
int id = 0;
Human(int i)
: id(i)
{}
void Print()
{
std::cout << "Human: " << id << "! " << std::endl;
}
void mPrint(int a, double b, int c, int d, const char* str)
{
std::cout << "Human: " << id << "! " << a << " " << b << " " << c << " " << d << " " << str << std::endl;
}
void Advise(int i, bool b)
{
auto colour = b ? "red" : "blue";
std::cout << "Human: " << id << "! I will take " << i << " of " << colour << " pills" << std::endl;
}
};
template <auto memfuncptr>
constexpr auto GetType()
{
return memfuncptr;
}
template <auto memfunc>
using PtrType = decltype(GetType<memfunc>());
int main()
{
Human person1{ 1 }, person2{ 2 }, person3{ 3 };
std::unique_ptr<Alien>alien{ Alien::Invade() };
alien->AccessSignal().AddInvoker(CreateInvoker(&person1, &Human::Advise));
alien->AccessSignal().AddInvoker(CreateInvoker(&person2, &Human::Advise));
alien->AccessSignal().AddInvoker(CreateInvoker(&person3, &Human::Advise));
alien->AccessSignal().DispatchSignal(42, false);
return 0;
}
更新 2: 我发现问题出在扩展非类型模板参数包中,因此“使用”可以工作。我仍然无法克服这个问题。
c++ non-type parameter pack expansion一个类似的问题,但关于功能。我也找不到带继承的折叠表达式的任何用法。
有一个答案显示了一种有前途的方法:https://stackoverflow.com/a/53112843/9363996
但是也有很大的缺点。一种是使用模板函数来调用继承的函数。这个例子编译并工作,但是:
示例 2
#include <iostream>
template <class ...>
struct TW {};
template <class ClType, class ... ArgTypes>
constexpr ClType ClassType(void(ClType::*)(ArgTypes...));
template <class ClType, class ... ArgTypes>
constexpr TW<ArgTypes...> ArgsType(void(ClType::*)(ArgTypes...));
template <auto pFunc>
using class_trait = decltype(ClassType(pFunc));
template <auto pFunc>
using args_trait = decltype(ArgsType(pFunc));
template <class, class>
struct _func_trait;
template <class ClType, template <class...> class Pack, class ... ArgTypes>
struct _func_trait<ClType, Pack<ArgTypes...>>
{
typedef void(ClType::*FuncPtr)(ArgTypes...);
typedef ClType ClassType;
typedef Pack<ArgTypes...> Args;
};
template <auto pFunc>
struct func_traits : public _func_trait<class_trait<pFunc>, args_trait<pFunc>>
{};
template <auto L, class Pack>
struct ClassImpl;
template <auto L, template <class ...> class Pack, class ... ArgTypes>
struct ClassImpl<L, Pack<ArgTypes...>>
{
void invoke(ArgTypes ... args)
{
(std::cout << ... << args) << std::endl;
}
};
template <auto L, auto ...R>
class My_class;
template <auto L>
class My_class<L> : public ClassImpl <L, args_trait<L>>
{
};
template <auto L, auto ... R>
class My_class : public My_class<L>, public My_class<R...>
{
public:
template <auto T, class ... ArgTypes>
void Invoke(ArgTypes... args)
{
My_class<T>::invoke(args...);
return;
}
};
struct Signals
{
void func1(int a, double b) {}
void func2(const char*, const char*) {}
constexpr void func3(int a, double b, int c, bool d);
};
int main()
{
Signals s;
My_class<&Signals::func1, &Signals::func2, &Signals::func3> mSignls;
mSignls.Invoke<&Signals::func1>(4, 6.31);
mSignls.Invoke<&Signals::func2>("Invoking funcion:", "function 2");
return 0;
}
最佳答案
终于想到了解决方案,用法很简单,如我所愿。
这是我的工作示例!
#include <tuple>
#include <iostream>
template <class ...>
struct TW {};
template <class ClType, class ... ArgTypes>
constexpr ClType ClassType(void(ClType::*)(ArgTypes...));
template <class ClType, class ... ArgTypes>
constexpr TW<ArgTypes...> ArgsType(void(ClType::*)(ArgTypes...));
template <auto pFunc>
using class_trait = decltype(ClassType(pFunc));
template <auto pFunc>
using args_trait = decltype(ArgsType(pFunc));
template <class, class>
struct _func_trait;
template <class ClType, template <class...> class Pack, class ... ArgTypes>
struct _func_trait<ClType, Pack<ArgTypes...>>
{
typedef void(ClType::*FuncPtr)(ArgTypes...);
typedef ClType ClassType;
typedef Pack<ArgTypes...> Args;
};
template <auto pFunc>
struct func_traits : public _func_trait<class_trait<pFunc>, args_trait<pFunc>>
{};
template <auto L, class Pack = args_trait<L>>
struct ClassImpl;
template <auto L, template <class ...> class Pack, class ... ArgTypes>
struct ClassImpl<L, Pack<ArgTypes...>>
{
void invoke(decltype(L), ArgTypes ... args)
{
(std::cout << ... << args) << std::endl;
}
};
template <class ... Impls>
struct ISignalMap : protected Impls...
{
using Impls::invoke...;
};
template <auto ... L>
struct SignalsMap
{
//just to see the pointers' values
static constexpr std::tuple<decltype(L)...> t{ std::make_tuple(L...) };
ISignalMap<ClassImpl<L>...> Signals{};
};
struct Signals
{
void func1(int a, double b) {}
void func12(int a, double b) {}
void func2(double a, double b, int c) {}
constexpr void func3(const char*) {}
};
int main(void)
{
auto& ref = SignalsMap<&Signals::func1, &Signals::func2, &Signals::func3>::t;
//add SignalsMap as member to your class and pass the pointers to
//methods you need to be signals
SignalsMap<&Signals::func1, &Signals::func2, &Signals::func3> sm;
//first parameter is a pointer to a signal you want to invoke
sm.Signals.invoke(&Signals::func2, 4.8, 15.16, 23);
sm.Signals.invoke(&Signals::func1, 23, 42.108);
sm.Signals.invoke(&Signals::func12, 23, 42.108);
sm.Signals.invoke(&Signals::func3, "Eat this!");
return 0;
}
关于c++ - 使用 "using declaration"扩展非类型模板参数包(模板可变参数编译时 SignalSlot 实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54463956/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t