我正在创建一个轻量级跨平台插件框架,它在应用程序和插件之间使用 C 接口(interface)(通常,但不总是,用 C++ 编写)。
我在帮助 C++ 应用程序和插件编写者方面面临的挑战之一是找到一种简单的方法来跨 C 接口(interface)公开 C++ 对象功能。我目前的解决方案感觉很简单,并使用模板“构建”基于 this great stackoverflow question and answer 包装底层 C++ 成员函数的 C 签名函数。
template <typename Tc, typename F, F>
struct MemberFuncWrapper;
template <typename Tc, // C interface structure tag
typename T, // C++ class, derived from Tc
typename R, // C++ member function return type
typename ...Args, // C++ member function argument types
R (T::*f)(Args...) const> // C++ member function
struct MemberFuncWrapper<Tc, R (T::*)(Args...) const, f> {
static R call(const Tc * tc, Args... args) {
const T * t = static_cast<const T *>(tc);
return ((*t).*f)(args...);
}
};
此模板的实例在 linux (gcc) 和 mac (clang) 下编译和运行良好,但在 Visual Studio 2013 中编译失败:
error C2440: 'specialization' : cannot convert from 'overloaded-function' to 'void (__cdecl Greeter::* )(void) const'
error C2973: 'MemberFuncWrapper<Tc,R(__cdecl T::* )(Args...) const,f>' : invalid template argument 'overloaded-function'
下面的独立示例代码显示了 Visual Studio 失败的行(在 Greeter 类定义中)。我希望有人可以:
下面的独立代码演示了在相当冗长的 Hello world 应用程序中使用 C++ 类实现 C 接口(interface)的上下文中使用的模板代码:
#include <iostream>
#include <utility>
//
// C interface and function(s) typically defined elsewhere
//
#ifdef __cplusplus
extern "C" {
#endif
// The C interface implemented by a 'greeter'
struct greeter_c {
void(*greet_cb)(const struct greeter_c * greeter,
const char * recipient);
};
// Some C function that makes use of a greeter
void broadcast(const struct greeter_c * greeter) {
greeter->greet_cb(greeter, "world");
}
#ifdef __cplusplus
} // extern "C"
#endif
//
// Template magic that envelopes a C++ member
// function call in a C-signature function
//
template <typename Tc, typename F, F>
struct MemberFuncWrapper;
template <typename Tc, // C interface structure tag
typename T, // C++ class, derived from Tc
typename R, // C++ member function return type
typename ...Args, // C++ member function argument types
R (T::*f)(Args...) const> // C++ member function
struct MemberFuncWrapper<Tc, R (T::*)(Args...) const, f> {
static R call(const Tc * tc, Args... args) {
// Cast C structure to C++ object
const T * t = static_cast<const T *>(tc);
// Details such as catching/handling exceptions omitted.
// Call C++ member function
return ((*t).*f)(args...);
}
};
// Repeat of the above for non-const member functions omitted
//
// A C++ class that implements the C 'greeter' interface
//
class Greeter : public greeter_c {
public:
// Constructor
Greeter(const char * greeting) : m_greeting(greeting) {
// Set up C interface callback by wrapping member function
// !! The following line causes the Visual Studio compilation error !!
greet_cb = MemberFuncWrapper<greeter_c,
void (Greeter::*)(const char *) const,
&Greeter::greet>::call;
}
// C++ member function that 'does' the greeting
void greet(const char * recipient) const {
std::cout << m_greeting << " " << recipient << std::endl;
}
private:
const char * m_greeting;
};
// An application that greets using a Greeter's C interface
int main(int argc, char * argv[]) {
// Create C++ object that implements C interface
Greeter a("Hello");
// Greet using Greeter's C interface
broadcast(&a);
return 0;
}
技术细节:
最佳答案
前言: std::forward在这里没用,因为 Args...被明确指定。换句话说,实例化的 C 接口(interface)不再是模板。 std::forward在非模板代码中没有用。为此,std::forward未在以下解决方案中使用。
版本 1:
template <typename Base, typename Derived, typename R, typename... Args>
struct c_interface_gen {
template <R(Derived::*mem_fn)(Args...)> inline
static R invoke(Base* pb, Args... args) {
return (static_cast<Derived*>(pb)->*mem_fn)(args...);
}
template <R(Derived::*mem_fn)(Args...) const> inline
static R invoke(const Base* pb, Args... args) {
return (static_cast<const Derived*>(pb)->*mem_fn)(args...);
}
};
这个版本有效。但这绝不是优雅的。主要问题在于使用该工具的语法冗长且不直观。
版本 2:
template <typename Sig>
struct mem_fn_sig;
template <typename R, typename D, typename... Args>
struct mem_fn_sig<R(D::*)(Args...)> {
template <R(D::*mem_fn)(Args...)>
struct mem_fn_inst {
template <typename Base>
struct base {
inline static R invoke(Base* pb, Args... args) {
return (static_cast<D*>(pb)->*mem_fn)(args...);
}
};
};
};
template <typename R, typename D, typename... Args>
struct mem_fn_sig<R(D::*)(Args...) const> {
template <R(D::*mem_fn)(Args...) const>
struct mem_fn_inst {
template <typename Base>
struct base {
inline static R invoke(const Base* pb, Args... args) {
return (static_cast<const D*>(pb)->*mem_fn)(args...);
}
};
};
};
template <typename Sig, Sig inst, typename Base>
struct c_interface_gen:
mem_fn_sig<Sig>:: template mem_fn_inst<inst>:: template base<Base>
{};
很明显,这个版本的代码比上一个版本多。但好处是使用该工具的语法简单直观。事实上,语法类似于您的原始工具。我刚刚添加了一些代码来简化 MSVC 的编译过程。
通常,您将像这样使用该工具:
... = c_interface_gen<decltype(&Derived::f), &Derived::f, Base>::invoke;
如果Derived::f重载了,你必须像这样明确指定它的类型:
... = c_interface_gen<void(Derived::*)() const, &Derived::f, Base>::invoke;
请注意,无需指定 const Base这里是const成员函数。您只需指定基本类型。模板将自动计算出是否 const是否加修饰符。
以下是您使用第二个版本的示例代码:
#include <iostream>
template <typename Sig>
struct mem_fn_sig;
template <typename R, typename D, typename... Args>
struct mem_fn_sig<R(D::*)(Args...)> {
template <R(D::*mem_fn)(Args...)>
struct mem_fn_inst {
template <typename Base>
struct base {
inline static R invoke(Base* pb, Args... args) {
return (static_cast<D*>(pb)->*mem_fn)(args...);
}
};
};
};
template <typename R, typename D, typename... Args>
struct mem_fn_sig<R(D::*)(Args...) const> {
template <R(D::*mem_fn)(Args...) const>
struct mem_fn_inst {
template <typename Base>
struct base {
inline static R invoke(const Base* pb, Args... args) {
return (static_cast<const D*>(pb)->*mem_fn)(args...);
}
};
};
};
template <typename Sig, Sig inst, typename Base>
struct c_interface_gen:
mem_fn_sig<Sig>:: template mem_fn_inst<inst>:: template base<Base>
{};
//
// C interface and function(s) typically defined elsewhere
//
#ifdef __cplusplus
extern "C" {
#endif
// The C interface implemented by a 'greeter'
struct greeter_c {
void(*greet_cb)(const struct greeter_c * greeter,
const char * recipient);
};
// Some C function that makes use of a greeter
void broadcast(const struct greeter_c * greeter) {
greeter->greet_cb(greeter, "world");
}
#ifdef __cplusplus
} // extern "C"
#endif
//
// A C++ class that implements the C 'greeter' interface
//
class Greeter : public greeter_c {
public:
// Constructor
Greeter(const char * greeting) : m_greeting(greeting) {
// Set up C interface callback by wrapping member function
// !! The following line causes the Visual Studio compilation error !!
greet_cb = c_interface_gen<decltype(&Greeter::greet), &Greeter::greet, greeter_c>::invoke;
}
// C++ member function that 'does' the greeting
void greet(const char * recipient) const {
std::cout << m_greeting << " " << recipient << std::endl;
}
private:
const char * m_greeting;
};
// An application that greets using a Greeter's C interface
int main(int argc, char * argv[]) {
// Create C++ object that implements C interface
Greeter a("Hello");
// Greet using Greeter's C interface
broadcast(static_cast<const greeter_c *>(&a));
return 0;
}
关于c++ - 在 C 中包装 C++ 成员函数 - Visual Studio 2013 模板问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29919987/
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin