生成一个基于基本类型,例如列表上的类型集合升压preprocessor库PointI32,PointF32等在C ++ / CLI [英] Boost Preprocessor library for generating a set of types based on a list of basic types e.g. PointI32, PointF32 etc. in C++/CLI

查看:160
本文介绍了生成一个基于基本类型,例如列表上的类型集合升压preprocessor库PointI32,PointF32等在C ++ / CLI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何使用升压。preprocessor库的 http://www.boost.org/doc/libs/release/libs/$p$pprocessor ​​展开一个通用的类型,不同的特定类型。下面我就问这个一个简单的点类的例子。鉴于:

I am trying to figure out how to use the Boost.Preprocessor library http://www.boost.org/doc/libs/release/libs/preprocessor to unfold a "generic" type for different specific types. Below I will ask this for a simple point class example. Given:

struct Point##TYPE_SUFFIX_NAME
{
    TYPE X;
    TYPE Y;

    // Other code
};

我要生成这种类型的不同的基本(POD)的数据类型,例如:

I want to generate this type for different basic (POD) data types e.g.:

PointF32, PointF64, PointI32 etc.

在这里PointF32是:

where PointF32 would be:

struct PointF32
{
     float X;
     float Y;
};

即,基于一个类型列表上:

That is, based on a list of types:

short, int, long, float, double etc. 

我要展开上述类型的这些。 preferably在单独的模板的定义包括文件,而不是作为一个宏,以便更容易调试。

I want to "unfold" the above type for these. Preferably with the "template" definition in a separate include file and not as a macro, to allow for easier debugging.

注:我没有兴趣听取关于C ++模板。我知道如何使用模板。但这些都不是我的情况非常有用。作为一个例子设想这些类型会从.NET在C#中使用,但正在在C / CLI中产生。所以,请坚持的问题。

NOTE: I am not interested in hearing about C++ templates. I know how to use templates. But these are not useful in my case. As an example imagine these types are going be used from .NET in C#, but are being generated in C++/CLI. So please stick to the question.

这个问题,当然,源于缺乏.NET模板支撑,并且由于仿制药并不适于解决我的问题。

The problem, of course, stems from the lack of template support in .NET and due to generics not being suitable to solve my problem.

推荐答案

根据由伯努瓦我想出了如下回答的答案。答案包括三个文件:

Based on the answer by Benoît I have come up with the following answer. The answer consists of three files:

  • MyPointTypes.h
  • MyPointTypeImpl.h
  • MyPointTypes.cpp
  • MyPointTypes.h
  • MyPointTypeImpl.h
  • MyPointTypes.cpp

MyPointTypes.h:

MyPointTypes.h:

#ifndef __MYSTRUCTURES_H__
#define __MYSTRUCTURES_H__

#include <boost/preprocessor/iteration/iterate.hpp>
#include <boost/preprocessor/seq/size.hpp>

typedef signed char int8;
typedef unsigned char uint8;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
typedef signed int int64;
typedef unsigned int uint64;

typedef float float32;
typedef double float64;

#define MY_SIGNED_INTEGER_SEQ    (int8)(int16)(int32)(int64)
#define MY_SIGNED_INTEGER_SUFFIX_SEQ    (I8)(I16)(I32)(I64)

#define MY_UNSIGNED_INTEGER_SEQ    (uint8)(uint16)(uint32)(uint64)
#define MY_UNSIGNED_INTEGER_SUFFIX_SEQ    (UI8)(UI16)(UI32)(UI64)

#define MY_SIGNED_UNSIGNED_INTEGER_SEQ    MY_SIGNED_INTEGER_SEQ MY_UNSIGNED_INTEGER_SEQ
#define MY_SIGNED_UNSIGNED_INTEGER_SUFFIX_SEQ    MY_SIGNED_INTEGER_SUFFIX_SEQ MY_UNSIGNED_INTEGER_SUFFIX_SEQ

#define MY_FLOAT_SEQ    (float32)(float64)
#define MY_FLOAT_SUFFIX_SEQ    (F32)(F64)

#define MY_BASIC_NUMERIC_TYPES_SEQ    MY_SIGNED_UNSIGNED_INTEGER_SEQ MY_FLOAT_SEQ
#define MY_BASIC_NUMERIC_TYPES_SUFFIX_SEQ    MY_SIGNED_UNSIGNED_INTEGER_SUFFIX_SEQ MY_FLOAT_SUFFIX_SEQ


#define MY_SEQ_OF_TYPES    MY_BASIC_NUMERIC_TYPES_SEQ
#define MY_SEQ_OF_SUFFICES    MY_BASIC_NUMERIC_TYPES_SUFFIX_SEQ

#define BOOST_PP_ITERATION_LIMITS (0, BOOST_PP_SEQ_SIZE(MY_SEQ_OF_TYPES) - 1)
#include BOOST_PP_ITERATE()

#undef MY_SEQ_OF_TYPES
#undef MY_SEQ_OF_SUFFICES

#endif

MyPointTypeImpl.h:

MyPointTypeImpl.h:

#include <boost/preprocessor/seq/elem.hpp>

#define n BOOST_PP_ITERATION()
#define PASTER(x,y) x ## y
#define EVALUATOR(x,y)  PASTER(x,y)
#define CONCATEVALUATED(x, y) EVALUATOR(x, y)

#define TYPE BOOST_PP_SEQ_ELEM(n, MY_SEQ_OF_TYPES)
#define SUFFIX BOOST_PP_SEQ_ELEM(n, MY_SEQ_OF_SUFFICES)

#define ADDSUFFIX(cls) CONCATEVALUATED(cls, SUFFIX)

struct ADDSUFFIX(Point)
{
  TYPE X;
  TYPE Y;
};

#undef n

MyPointTypes.cpp:

MyPointTypes.cpp:

#define BOOST_PP_FILENAME_1 "MyPointTypeImpl.h"
#include "MyPointTypes.h"

这将定义类型:

PointI8, PointI16, PointI32, PointI64, 
PointUI8, PointUI16, PointUI32, PointUI64, 
PointF32, PointF64

想象一下,而不是一个C ++结构体是C ++ / CLI值类型也就是那么:

Imagine then instead of a C++ struct a C++/CLI value type i.e.:

public value class Point 

然后,我们已经有效地创建了所有的基本数字类型的点类型在.NET中,例如使用C#。

Then we have effectively created point types of all basic numeric types for use in .NET e.g. C#.

这篇关于生成一个基于基本类型,例如列表上的类型集合升压preprocessor库PointI32,PointF32等在C ++ / CLI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆