c++模板和c的混用

        工作中虽然用的都是 cpp文件,但里面大部分还是c接口,即全局接口。今天就遇到了一个事情,使用c++的模板更容易些,如果用c接口实现的话可能要写很多相似的接口了,代码如下:

头文件:

typedef unsigned int euint32;
typedef int eint32;

#define MAX_OUT_SEGMENT 8

enum OutIfcType 
{
    OutIfcType_Lif,
    OutIfcType_Lsp,
    OutIfcType_SrLsp,
    OutIfcType_SrTeTunnel,
    OutIfcType_SrAdjSid,
    OutIfcType__MAX
};

//sr
struct config_SrLsp_segout_segoutInfo
{
    euint32 OutLifOidx;
    euint32 OutLabel;
};

struct config_SrLsp_segout
{
    struct config_SrLsp_segout_segoutInfo outSegment;
    enum OutIfcType IfcType;
	bool isSecondary;
};

struct config_SrLsp_segout_rep
{
    euint32 _max;
    euint32 _n;
    struct config_SrLsp_segout *_entry;
};

struct config_SrLsp_SegOuts
{
    struct config_SrLsp_segout_rep segout;
};

//sradj
struct config_SrAdjLsp_segout_segoutInfo
{
    euint32 OutLifOidx;
    euint32 OutLabel;
};

struct config_SrAdjLsp_segout
{
    struct config_SrAdjLsp_segout_segoutInfo outSegment;
    enum OutIfcType IfcType;
	bool isSecondary;
};

struct config_SrAdjLsp_segout_rep
{
    euint32 _max;
    euint32 _n;
    struct config_SrAdjLsp_segout *_entry;
};

struct config_SrAdjLsp_SegOuts
{
    struct config_SrAdjLsp_segout_rep segout;
};

//ldp
struct config_LdpLsp_segout_segoutInfo
{
    euint32 OutLifOidx;
    euint32 OutLabel;
};

struct config_LdpLsp_segout
{
    struct config_LdpLsp_segout_segoutInfo outSegment;
    enum OutIfcType IfcType;
	bool isSecondary;
};

struct config_LdpLsp_segout_rep
{
    euint32 _max;
    euint32 _n;
    struct config_LdpLsp_segout *_entry;
};

struct config_LdpLsp_SegOuts
{
    struct config_LdpLsp_segout_rep segout;
};

typedef struct
{
    OutIfcType outlif_type;
    euint32    outlif_oidx;
    euint32    out_label;

}outseg_info;

typedef struct
{
    outseg_info outseg_cfg;

}single_outseg_cfg;

typedef struct
{
    single_outseg_cfg pri_cfg[MAX_OUT_SEGMENT];
	single_outseg_cfg sec_cfg[MAX_OUT_SEGMENT];
}general_outseg_cfg;

定义了很多的结构体,然后最终的目的是要把各个结构体里的 OutLifOidx 和 OutLabel 取出来。 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "c_cpp_template.h"

template<class type_in_outseg, class type_out_outseg>
eint32 outseg_get_template(type_in_outseg in_cfg, type_out_outseg out_cfg)
{	
	single_outseg_cfg *single_cfg = NULL;
    for(euint32 i = 0; i < in_cfg->segout._n; i++)
    {
		single_cfg = (in_cfg->segout._entry[i].isSecondary) ? out_cfg->sec_cfg : out_cfg->pri_cfg;
		for(euint32 j = 0; j < 1; j++)
		{
			single_cfg[j].outseg_cfg.outlif_type = (OutIfcType)in_cfg->segout._entry[i].IfcType;
			single_cfg[j].outseg_cfg.outlif_oidx = in_cfg->segout._entry[i].outSegment.OutLifOidx;
			single_cfg[j].outseg_cfg.out_label = in_cfg->segout._entry[i].outSegment.OutLabel;			
		}
    }

    return 0;
}

#ifdef __cplusplus
extern "C" {
	
eint32 outseg_get_by_type(euint32 outlif_type, void *lsp_cfg, general_outseg_cfg *outseg_cfg)
{
	switch(outlif_type)
	{
		case OutIfcType_SrAdjSid:
			{
				config_SrAdjLsp_SegOuts *SrAdjCfg = (config_SrAdjLsp_SegOuts *)lsp_cfg;
				outseg_get_template(SrAdjCfg, outseg_cfg);
			}
			break;
		case OutIfcType_SrLsp:
			{
				config_SrLsp_SegOuts *SrCfg = (config_SrLsp_SegOuts *)lsp_cfg;
				outseg_get_template(SrCfg, outseg_cfg);
			}
			break;
		default:
			{
				config_LdpLsp_SegOuts *LdpCfg = (config_LdpLsp_SegOuts *)lsp_cfg;
				outseg_get_template(LdpCfg, outseg_cfg);
			}
	}
	
	return 0;
}	

}
#endif

eint32 outseg_print(general_outseg_cfg *cfg)
{
	printf("primary:\n");
	for(eint32 i = 0; i < MAX_OUT_SEGMENT; i++)
	{
		printf("outlif_type = %d, outlif_oidx = %u, out_label = %u\n", cfg->pri_cfg[i].outseg_cfg.outlif_type, cfg->pri_cfg[i].outseg_cfg.outlif_oidx, cfg->pri_cfg[i].outseg_cfg.out_label);
	}
	
	printf("secondary:\n");
	for(eint32 i = 0; i < MAX_OUT_SEGMENT; i++)
	{
		printf("outlif_type = %d, outlif_oidx = %u, out_label = %u\n", cfg->sec_cfg[i].outseg_cfg.outlif_type, cfg->sec_cfg[i].outseg_cfg.outlif_oidx, cfg->sec_cfg[i].outseg_cfg.out_label);
	}
	
	return 0;
}
int main()
{
	config_LdpLsp_SegOuts ldpCfg;
	ldpCfg.segout._n = 2;
	ldpCfg.segout._entry = (struct config_LdpLsp_segout *)malloc(sizeof(config_LdpLsp_segout) * 2);
	if(NULL == ldpCfg.segout._entry)
	{
		printf("out of memory\n");
		return 1;
	}
	config_LdpLsp_segout segOutArray[2] = {
		{{1234, 5555}, OutIfcType_Lif, 0}, 
		{{1357, 6666}, OutIfcType_Lsp, 1}
		};
		
	memcpy(ldpCfg.segout._entry, &segOutArray, sizeof(config_LdpLsp_segout) * 2);
	
	general_outseg_cfg outsegCfg;
	memset(&outsegCfg, 0, sizeof(outsegCfg));
	outseg_get_by_type(OutIfcType_Lif, (void*)&ldpCfg, &outsegCfg);
	outseg_print(&outsegCfg);
	
	return 0;
}

注意这个模板的实现需要在 extern "C" 外面,否则编译会出错的。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值