贵阳网站建设天锐科技,金融集团网站模板,技能培训学校,网站被做301跳转了怎么办背景#xff1a;在项目开发过程中#xff0c;难免碰到这种情况#xff0c;当我们想要通过我们开发的库#xff0c;调用主程序中的一些变量或者函数的时候#xff0c;就会导致一些问题#xff0c;因为在项目构建过程中#xff0c;库都是不依赖于主程序编译的#xff0c;…背景在项目开发过程中难免碰到这种情况当我们想要通过我们开发的库调用主程序中的一些变量或者函数的时候就会导致一些问题因为在项目构建过程中库都是不依赖于主程序编译的但是在开发库函数的时候发现有时候我们不可避免的会和主程序之间的变量或者函数进行沟通在这种情况下如何在构建库的过程中能够调用到主程序的函数 同时又要避免链接问题库构建的过程中应该独立构建不依赖主程序简介在目前的项目中遇到了一个比较特殊的问题主程序中包括了各种各样的管理器其中就有一个线程池管理器这个管理器可以接收任务然后在后台运行。管理器是一个单例关于如何构建模版单例可以查看我这篇博客 -- 博客 我在库当中需要通过Worker::getInstance()-getRuntime()的方式获取Worker实例下的一个类型现在就是问题来了worker是在主程序处实现的我们在库当中只是单纯的包含了库的头文件这样在链接阶段就会提示找不到Worker相关的符号或者因这个原因导致的其他问题。所以有什么方式能够在写库的时候能通过某些方式调用到App里面的一些函数吗思考首先我们要想一下要实现这样的工作会遇到哪些问题 第一个如果直接在库当中调用App中的某些函数一定会遇到链接时找不到对应符号的实现。这是因为我们可以在App中调用库的函数完全是得益于target_link_library(App PRIVATE myLib)这样在编译期间就会通过链接myLib从而在其中找到App中想要用到的myLib中的函数实现。但是我们没有办法反过来这么指定呀target_link_library(myLib PUBLIC App)因为App是一个可执行文件是没办法被库链接的但是后来查到好像在某些平台可以把App所有的符号都Export出去但是这不是通用的解决方案。怎么解决呢 我想是否可以将我们想要用到的某个具体函数封装起来然后保存到myLib中的某个全局变量中这样在myLib中调用的话我们直接从这个全局变量中取出这个函数的封装然后自己拆解就行了。设计那接下来是要考虑如何设计这个结构了。我们想要这么一个结构可以将App中的一些函数封装起来然后存储到这个结构体中然后用到的时候我们可以把这个结构体转换成我们想用的东西。所以这个结构体可能会封装一个函数这个函数可以有参数或者无参数它也可以返回任意类型的参数否则的话这个结构体的通用性就不强。所以我设想的是在一个“池子”中保存某个结构体这个结构体可以通过名字取出来然后这个结构体可以转换成具体的格式。所以第一步我们使用std::map(std::string ,xxx ); 来做这个“池子”。其次我们要能够通过一种格式将我们的结构体转换成当时它实际的样子比如我一开始这个结构体存储了一个std::functionvoid() fun [](){std::coutHellostd::endl}; 那我用这个xxx结构体把这个函数封装完成之后我要用的时候,可以通过xxx.cast std::functionvoid() ()再把xxx转换成这个类型然后调用所以这个结构体一定是要可以抹除类型信息的我们称它为Any。实现好了具体的Any实现我直接放出来具体可以参考《深入理解c11 代码优化与企业级应用》。具体咱们不赘述了。#ifndef ANY_H
#define ANY_H#include memory
#include typeindex
#include exception
#include iostreamstruct Any
{Any(void) : m_tpIndex(std::type_index(typeid(void))) {}Any(const Any that) : m_ptr(that.Clone()), m_tpIndex(that.m_tpIndex) {}Any(Any that) : m_ptr(std::move(that.m_ptr)), m_tpIndex(that.m_tpIndex) {}//创建智能指针时对于一般的类型通过std::decay来移除引用和cv符从而获取原始类型templatetypename U, class typename std::enable_if!std::is_sametypename std::decayU::type, Any::value, U::typeAny(U value) : m_ptr(new Derived typename std::decayU::type(std::forwardU(value))),m_tpIndex(std::type_index(typeid(typename std::decayU::type))){}bool IsNull() const { return !bool(m_ptr); }templateclass U bool Is() const{return m_tpIndex std::type_index(typeid(U));}//将Any转换为实际的类型templateclass UU AnyCast(){if (!IsU()){std::cout can not cast typeid(U).name() to m_tpIndex.name() std::endl;throw std::logic_error{bad cast};}auto derived dynamic_castDerivedU* (m_ptr.get());return derived-m_value;}Any operator(const Any a){if (m_ptr a.m_ptr)return *this;m_ptr a.Clone();m_tpIndex a.m_tpIndex;return *this;}Any operator(Any a){if (m_ptr a.m_ptr)return *this;m_ptr std::move(a.m_ptr);m_tpIndex a.m_tpIndex;return *this;}private:struct Base;typedef std::unique_ptrBase BasePtr;struct Base{virtual ~Base() {}virtual BasePtr Clone() const 0;};templatetypename Tstruct Derived : Base{templatetypename UDerived(U value) : m_value(std::forwardU(value)) { }BasePtr Clone() const{return BasePtr(new DerivedT(m_value));}T m_value;};BasePtr Clone() const{if (m_ptr ! nullptr)return m_ptr-Clone();return nullptr;}BasePtr m_ptr nullptr;std::type_index m_tpIndex;
};#endif // ANY_H
然后接下来的“池子”就很简单了struct functionWrappers{static auto GetList() - std::mapstd::string,Any;static std::mapstd::string,Any::iterator getWrapper(std::string);template typename Tstatic T getFunc(std::string name){auto funcWarpper getWrapper(name);if( funcWarpper Qml::Register::functionWrappers::GetList().end() ){qCritical()Can not find Worker warpper in Qml::Register::functionWrapper::GetList().end()__PRETTY_FUNCTION__;return std::move(funcWarpper-second.AnyCastT());}auto func funcWarpper-second.AnyCastT();if(!func){qCritical()Can not convert Worker warpper function __PRETTY_FUNCTION__;return func;}return func;};static Any getAnyFunc(const std::string );
};
使用使用方式如下//Appquick::Qml::Register::funcType addTaskCount_f(addTaskCount,[](){return quick::App::Worker::getInstance()-addTaskCount();});//myLibQml::Register::functionWrappers::getFuncstd::functionvoid()(addTaskCount)();很简单不再赘述。通过结合我的另一篇文章如何静态化注册某些组件 -- 博客 我们可以实现程序启动之后即自动注册。有问题欢迎讨论。