Giter Site home page Giter Site logo

pluginframework's People

Contributors

kilpatds avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pluginframework's Issues

Does my code have some problems?

Hi, there.

I'm use your Plugin Framework code to study c++ skill.

Below is my test code: main.cpp

#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;
}

When i debug this code in VS 2015 x64.

The debugger tell me :

Line 43:mMapIntContainer.insert(std::make_pair(a, b));

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.

Perhaps it is the std::map container is not initialize?

Can you help me fix this problem? Thanks.

Crash when requested type wrong, instead of exception

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.