我试图弄清楚如何使用Boost.Preprocessor库http://www.boost.org/doc/libs/release/libs/preprocessor展开不同特定类型的“通用”类型。下面我将问一个简单的点类示例。给出:
struct Point##TYPE_SUFFIX_NAME
{
TYPE X;
TYPE Y;
// Other code
};
我想为不同的基本(POD)数据类型生成此类型,例如:
PointF32, PointF64, PointI32 etc.
其中PointF32将是:
struct PointF32
{
float X;
float Y;
};
即,基于类型列表:
short, int, long, float, double etc.
我想为这些“展开”上述类型。最好将“模板”定义放在单独的包含文件中,而不是宏,以便于调试。
注意:我对听说C ++模板不感兴趣。我知道如何使用模板。但这些对我来说没用。举个例子来说,这些类型将在C#中从.NET中使用,但是在C ++ / CLI中生成。所以请坚持这个问题。
当然,问题源于.NET中缺少模板支持,并且由于泛型不适合解决我的问题。
答案 0 :(得分:1)
旧的(预模板)版本的C ++编译器通常有<generic.h>
个标题用于此类事情。我会为它搜索旧版本的g ++。这是在我的时间之前,所以我不知道它是否适合你。
或者,类似
#define TYPE short
#define TYPES I16
#include "Point.def"
#undef TYPE
#undef TYPES
#define TYPE int
#define TYPES I32
#include "Point.def"
也可以帮到你。
或者显然是外部代码生成器(在awk,perl,C ++等等)。这可能是最好的解决方案。
答案 1 :(得分:1)
以下代码未经测试,但应该是一个良好的开端,以实现您想要的目标。
在my_structures.h中:
#ifndef __MYSTRUCTURES_H__
#define __MYSTRUCTURES_H__
#define MY_LIST_OF_TYPES (F32, (I32, (BOOST_PP_NIL)))
#define MY_LIST_OF_SUFFICES (float, (int, (BOOST_PP_NIL)))
#include <boost/preprocessor/iteration/iterate.hpp>
#include <boost/preprocessor/list/size.hpp>
#define BOOST_PP_ITERATION_LIMITS (0, BOOST_PP_LIST_SIZE(MY_LIST_OF_TYPES))
#define BOOST_PP_FILENAME_1 "create_my_structures.h"
#include BOOST_PP_ITERATE()
#undef MY_LIST_OF_TYPES
#undef MY_LIST_OF_SUFFICES
#endif
和create_my_structures.h
#include <boost/preprocessor/list/at.hpp>
#define n BOOST_PP_ITERATION()
struct Point ## BOOST_PP_LIST_AT(MY_LIST_OF_SUFFICES, n)
{
BOOST_PP_LIST_AT(MY_LIST_OF_TYPES, n) X;
BOOST_PP_LIST_AT(MY_LIST_OF_TYPES, n) Y;
};
#undef n
答案 2 :(得分:1)
根据Benoît的回答,我得出了以下答案。答案由三个文件组成:
MyPointTypes.h
MyPointTypeImpl.h
MyPointTypes.cpp
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:
#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:
#define BOOST_PP_FILENAME_1 "MyPointTypeImpl.h"
#include "MyPointTypes.h"
这将定义类型:
PointI8, PointI16, PointI32, PointI64,
PointUI8, PointUI16, PointUI32, PointUI64,
PointF32, PointF64
然后想象一下,而不是C ++结构的C ++ / CLI值类型,即:
public value class Point
然后我们有效地创建了在.NET中使用的所有基本数字类型的点类型,例如C#。
答案 3 :(得分:0)
这似乎是个老问题,但.... 我认为使用标准宏处理器(CPP)
更容易实现#define STRUCT_POINT( P_TYPE ) \
struct Point##_##P_TYPE \
{ \
P_TYPE X; \
P_TYPE Y; \
\
};
#define CREATE_STRUCT_POINTS \
STRUCT_POINT( short ) \
STRUCT_POINT( int ) \
STRUCT_POINT( unsigned ) \
STRUCT_POINT( float ) \
STRUCT_POINT( double )
CREATE_STRUCT_POINTS
#undef CREATE_STRUCT_POINTS
#undef STRUCT_POINT
或许这种变化(遵循'规格')
#define STRUCT_POINT( P_TYPE, P_TYPE_ALIAS ) \
struct Point##P_TYPE_ALIAS \
{ \
P_TYPE X; \
P_TYPE Y; \
\
};
#define CREATE_STRUCT_POINTS \
STRUCT_POINT( short , I16 ) \
STRUCT_POINT( int , I32 ) \
STRUCT_POINT( unsigned , U32 ) \
STRUCT_POINT( float , F32 ) \
STRUCT_POINT( double , F64 )
CREATE_STRUCT_POINTS
#undef CREATE_STRUCT_POINTS
#undef STRUCT_POINT