kilpatds / pluginframework Goto Github PK
View Code? Open in Web Editor NEWA simple C++ framework for Plugins (interface + factory) loadable from shared libraries
License: Other
A simple C++ framework for Plugins (interface + factory) loadable from shared libraries
License: Other
#include <iostream>
#include <string>
#include <map>
class IPrint
{
public:
virtual ~IPrint(){}
virtual void print() = 0;
};
template <typename T, typename ...T1>
class TemplateClass:public T
{
public:
typedef T *(*class_t)(T1...);
typedef std::map<int, class_t> map_int_t;
virtual ~TemplateClass(){}
static int Set(int a, class_t b);
static T* Get(int a, T1... args);
static void dump();
private:
static map_int_t mMapIntContainer;
};
template <typename T, typename ...T1>
typename TemplateClass<T,T1...>::map_int_t TemplateClass<T,T1...>::mMapIntContainer;
template <typename T, typename ...T1>
int TemplateClass<T, T1...>::Set(int a, class_t b)
{
/*test creation*/
//T* test = b();
//delete test;
std::cout << "create:" << a << std::endl;
std::cout << "map_size:" << mMapIntContainer.size() << std::endl;
//mMapIntContainer.insert(std::make_pair(a, b)); // @FixMe: if insert anything will be crashed immediately.
return 0;
}
template <typename T, typename ... T1>
T* TemplateClass<T, T1...>::Get(int a, T1 ... args)
{
std::cout << "query:" << a << std::endl;
std::cout << "map_size:" << mMapIntContainer.size() << std::endl;
auto it = mMapIntContainer.find(a);
if(it!=mMapIntContainer.end())
{
std::cout << "Find*" << std::endl;
return (it->second)(args...);
}
return nullptr;
}
template <typename T, typename ...T1>
void TemplateClass<T, T1...>::dump()
{
for(auto s :mMapIntContainer)
{
std::cout << s.first << ", " << s.second << std::endl;
}
}
static int count = 0;
template <typename T, typename T1, typename ...T2>
class StaticReg
{
public:
static T* factory(T2 ... args)
{
std::cout << "sublcass creation" << std::endl;
return new T1(args...);
}
StaticReg(){
std::cout << "StaticReg::StaticReg" << std::endl;
std::cout << typeid(T).name() << std::endl;
TemplateClass<T, T2...>::Set(++count, factory);
std::cout << "count:" << count << std::endl;
}
};
template <typename T, typename T1, typename ...T2>
class RealTemplateClass: public TemplateClass<T,T2...>
{
public:
RealTemplateClass(){}
static StaticReg<T, T1, T2...> hold_obj;
};
template<typename T, typename T1, typename ...T2>
StaticReg<T, T1, T2...> RealTemplateClass<T, T1, T2...>::hold_obj;
class APrint:public RealTemplateClass<IPrint, APrint>
{
public:
APrint() { std::cout << "APrint:construct" << std::endl; }
virtual ~APrint(){}
void print()
{
std::cout << "APrint::print" << std::endl;
}
};
template class RealTemplateClass<IPrint, APrint>;
class BPrint:public TemplateClass<IPrint, BPrint>
{
public:
BPrint(){std::cout << "BPrint:construct" << std::endl;}
virtual ~BPrint(){}
void print()
{
std::cout << "BPrint:print" << std::endl;
}
};
template class RealTemplateClass<IPrint, BPrint>;
int main()
{
IPrint* aa_ptr = TemplateClass<IPrint>::Get(1); // return new APrint();
if(aa_ptr!=nullptr)
{
//APrint* a = dynamic_cast<APrint*>(aa_ptr);
aa_ptr->print();
delete aa_ptr;
aa_ptr = nullptr;
}
IPrint* bb_ptr = TemplateClass<IPrint>::Get(2); // return new BPrint();
if(bb_ptr!=nullptr)
{
//BPrint* b = dynamic_cast<BPrint*>(bb_ptr);
bb_ptr->print();
delete bb_ptr;
bb_ptr = nullptr;
}
return 0;
}
Exception thrown: read access violation.
std::_Tree<std::_Tmap_traits<int,IPrint * __ptr64 (__cdecl*)(void),std::less<int>,std::allocator<std::pair<int const ,IPrint * __ptr64 (__cdecl*)(void)> >,0> >::_Root(...) returned 0x8.
Hello!
How to load multiple modules?
Thanks!
If the plugin declares the wrong types (like to the constructor), the loader crashes in dlopen() as the init object attempts to add an entry to a set that doesn't exist. That shouldn't crash (the set should exist). Weak symbols?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.