[OpenGL] C++ 封装(自动绑定/对象操作)

#include <iostream>

using namespace std;

template <typename BINDABLE>
struct Binder
{
	Binder(BINDABLE& obj)
	{
		*this = obj;
	}

	~Binder()
	{
		BINDABLE::binder::unbind();
	}

	Binder(const Binder&) = delete;
	Binder& operator=(const Binder&) = delete;

	Binder& operator=(BINDABLE& obj)
	{
		BINDABLE::binder::bind(obj);
		return *this;
	}
};

struct VAO;

class VaoId
{
	static int val;

	int* id;

public:
	
	using binder = VAO;

	VaoId()
	{
		id = new int(++val);
	}

	~VaoId()
	{
		delete id;
	}

	VaoId(const VaoId&) = delete;

	VaoId(VaoId&& that) noexcept :
		id(that.id)
	{
		that.id = nullptr;
	}

	friend struct VAO;
};

int VaoId::val = 0;

struct VAO final
{
	static int now;

	VAO() = delete;

	static void bind(VaoId& vao)
	{
		now = *vao.id;
	}

	static void unbind()
	{
		now = 0;
	}

	static void process()
	{
		cout << now << endl;
	}

	static VaoId create()
	{
		VaoId ret;
		Binder b = ret;
		VAO::process();
		return ret;
	}
};

int VAO::now = 0;

int main()
{
	VaoId id = VAO::create();// 1
	Binder a = id;
	VAO::process(); // 1
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据