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;
}
目录
相关文章
|
2月前
|
存储 自然语言处理 JavaScript
Storage存储-正则表达式(coderwhy版本)(一)
Storage存储-正则表达式(coderwhy版本)
35 0
|
2月前
|
存储 JavaScript Java
Storage存储-正则表达式(coderwhy版本)(二)
Storage存储-正则表达式(coderwhy版本)
49 0
|
12月前
ES 布尔查询中 minimum_should_match 参数使用避坑
ES 布尔查询中 minimum_should_match 参数使用避坑
问题记录:node-exporter服务启动报错:Segmentation fault
问题记录:node-exporter服务启动报错:Segmentation fault
180 0
|
索引
问题复盘:Kibana did not load properly. Check the server output for more information
问题复盘:Kibana did not load properly. Check the server output for more information
273 0
问题复盘:Kibana did not load properly. Check the server output for more information
|
存储 索引
Elastic:data_hot,data_warm,data_cold角色有什么用
data_hot:热点数据节点,热点数据节点在进入es时存储时间序列数据,热层必须快速读取和写入,并且需要更多的硬件资源 data_warm;暖数据节点,存储不再定期更新但仍在查询的索引。查询两的频率通常低于索引出于热层时的频率,性能较低的硬件通常可用于此层中的节点 data_cold:冷数据节点,存储访问频率较低的只读索引,磁层使用性能较低的硬件,并且可以利用可搜索快照索引来最小化所需的资源
113 0
Elastic:data_hot,data_warm,data_cold角色有什么用
|
监控 关系型数据库 数据库
Greenplum csvlog(日志数据)检索、释义(gp_toolkit.gp_log*)
标签 PostgreSQL , Greenplum , csvlog , gp_toolkit 背景 由于GP为分布式数据库,当查看它的一些日志时,如果到服务器上查看,会非常的繁琐,而且不好排查问题。
2497 0
|
存储 测试技术 Apache
jMeter 里 CSV Data Set Config Sharing Mode 的含义详解
jMeter 里 CSV Data Set Config Sharing Mode 的含义详解
161 0
jMeter 里 CSV Data Set Config Sharing Mode 的含义详解
[HMR] Hot Module Replacement is disabled.
webpack配置 let path = require('path'); let htmlWebpackPlugin = require('html-webpack-plugin'); const webpack = require('webpack...
1908 0