Hot to use typelist of Loki??

简介: 最近调试的一段代码,请一起look look!#include #include #include using namespace std; namespace MCD { template struct Typelist{...
最近调试的一段代码,请一起look look!
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
namespace MCD
{
    template<class T,class U>
	struct Typelist{
	    typedef T Head;
		typedef U Tail;
	};

    class NullType;
#define TYPELIST_1(T1) Typelist<T1,NullType>
#define TYPELIST_2(T1,T2) Typelist<T1,TYPELIST_1(T2)>
#define TYPELIST_3(T1,T2,T3) Typelist<T1,TYPELIST_2(T2,T3)>
#define TYPELIST_4(T1,T2,T3,T4) Typelist<T1,TYPELIST_3(T2,T3,T4)>
	
	template<class Tlist> struct Length;
	template<> struct Length<NullType>
	{
	    enum{value = 0};
	};

	template<class T,class U>
	struct Length<Typelist<T,U> >
	{
		enum{value = 1 + Length<U>::value};
	};

	template<class Tlist,unsigned int index> struct TypeAt;
	template<class Head,class Tail>
	struct TypeAt<Typelist<Head,Tail>,0>
	{
	    typedef Head Result;	
	};

	template<class Head,class Tail,unsigned int i>
	struct TypeAt<Typelist<Head,Tail>,i>
	{
	    typedef typename TypeAt<Tail,i-1>::Result Result;
	};
} // End of MCD

namespace SolidMCP
{
	class FontTable
	{
	public:
		virtual void Read(){
			std::cout << "Reading Font Table" << std::endl;
		}
	};

	class CMAP_Table:public FontTable
	{
	public:
		CMAP_Table() {
			std::cout << "Initializing CMAP Table" << std::endl;
		}

		void Read() {
			std::cout << "Read CMAP Table" << std::endl;
		}

		static char* GetName() {
		    return "CMAP_Table";
		}
	};

	class OS2_Table:public FontTable
	{
	public:
		OS2_Table()
		{
			std::cout << "Initializing OS2 Table" << std::endl;
		}

		void Read()
		{
			std::cout << "Read OS2 Table" << std::endl;
		}

		static char* GetName()
		{
			return "OS2_Table";
		}
	};

    class HEAD_Table:public FontTable
	{
	public:
		HEAD_Table(){
			std::cout<< "Initializing HEAD Table" << std::endl;
		}

		void Read(){
			std::cout << "Read HEAD Table" << std::endl;
		}

		static char* GetName(){
			return "HEAD_Table";
		}
	};
	
	class HHEA_Table:public FontTable
	{
	public:
		HHEA_Table(){
			std::cout << "Initializing HHEA Table" << std::endl;
		}

		void Read() {
			std::cout << "Read HHEA Table" << std::endl;
		}

		static char* GetName()
		{
			return "HHEA_Table";
		}
	};
	
	class Unknown_Table:public FontTable
	{
	public:
	    Unknown_Table(){
			std::cout << "Initializing Unknown Table" << std::endl;
		}

		void Read() {
			std::cout << "Read Unknown Table" << std::endl;
		}

		static char* GetName()
		{
			return "Unknown_Table";
		}
	};	

	template<class Tlist> struct ReadTable;
	template<class T>
	struct ReadTable<MCD::Typelist<T,MCD::NullType> >
	{
		static bool Execute()
		{
			T table;
			table.Read();
			return true;
		}
	};

	template<class T,class U>
	struct ReadTable<MCD::Typelist<T,U> >
	{
		static bool Execute()
		{
			if(ReadTable<MCD::Typelist<typename U::Head,typename U::Tail> >::Execute())
			{
			    T table;
				table.Read();
			}
			return true;
		}
	};

	// ReadTaleLoop
	template<class T,int i> struct ReadTableLoop
	{
	    static void Execute()
		{
			// g递归ui用的很好 
			ReadTableLoop<T,i-1>::Execute();
			typename MCD::TypeAt<T,i-1>::Result table;
			table.Read();
		}
	};

	template<class T> struct ReadTableLoop<T,0>
	{
	    static void Execute(){}
	};

	// create table class Name
	template<class Tlist> struct CreateObject;

	template<class T,class U>
	struct CreateObject<MCD::Typelist<T,U> >
	{
	    static void* Execute(char* pName)
		{
			if(strcmp(T::GetName(),pName) == 0)
		    {
				return new T; 
			}else{
				return CreateObject<U>::Execute(pName);
			}
		}
	};

	template<>
	struct CreateObject<MCD::NullType>
	{
		static void* Execute(char* pName)
		{
			return NULL;
		}
	};	
}// END OF SoildMCP

// How to use
using namespace SolidMCP;
using namespace MCD;

int main()
{
	typedef TYPELIST_4(CMAP_Table,OS2_Table,HEAD_Table,HHEA_Table) FontTableList;
	std::cout << "--Length is " << Length<FontTableList>::value << std::endl;
	std::cout << "Read the 2nd Table" << std::endl;
	TypeAt<FontTableList,2>::Result table;
	table.Read();

	std::cout<<"Begin ReadTable" << std::endl;
	ReadTable<FontTableList>::Execute();


	std::cout << "Begin ReadTableLoop" << std::endl;
	ReadTableLoop<FontTableList,Length<FontTableList>::value>::Execute();

	std::cout << "Begin Create Object By Name" << std::endl;
	FontTable* pTable = (FontTable *)CreateObject<FontTableList>::Execute("HEAD_Table");
	pTable->Read();

	return 0;
}
目录
相关文章
|
6月前
|
自然语言处理 Go 索引
startOffset must be non-negative, and endOffset must be >= startOffset, and offsets must not go backwards startOffset=615,endOffset=617,lastStartOffset=616 for field 'convContent.content'
【7月更文挑战第4天】startOffset must be non-negative, and endOffset must be >= startOffset, and offsets must not go backwards startOffset=615,endOffset=617,lastStartOffset=616 for field 'convContent.content'
|
6月前
|
存储 传感器 SQL
influxdb 中得 fields 与 tag 区别总结
influxdb 中得 fields 与 tag 区别总结
539 1
ES中的Multi_match深入解读:best_fields、most_fields、cross_fields用法一览
ES中的Multi_match深入解读:best_fields、most_fields、cross_fields用法一览
ES中的Multi_match深入解读:best_fields、most_fields、cross_fields用法一览
PAT (Advanced Level) Practice - 1043 Is It a Binary Search Tree(25 分)
PAT (Advanced Level) Practice - 1043 Is It a Binary Search Tree(25 分)
132 0
|
存储 算法
PAT (Advanced Level) Practice - 1151 LCA in a Binary Tree(30 分)
PAT (Advanced Level) Practice - 1151 LCA in a Binary Tree(30 分)
130 0
|
前端开发 Go C++
hot load那点事
热加载,最初接触的时候是使用create-react-app的时候,创建一个项目出来,修改一点代码,页面自动刷新了,贫道当时就感叹,这是造福开发者的事情。 再后来编写静态页面的时候使用 VS Code 的插件 Liver Server, 也是及时刷新,平僧幸福感慢慢,什么单不单身,狗不狗的,都不重要了。
198 0
|
Kubernetes 容器
Kubernetes格式化输出:custom-columns
Kubernetes格式化输出:custom-columns
498 0
[HMR] Hot Module Replacement is disabled.
webpack配置 let path = require('path'); let htmlWebpackPlugin = require('html-webpack-plugin'); const webpack = require('webpack...
1957 0