Boost 预处理器库,用于基于 C++/CLI 中的基本类型列表(例如 PointI32、PointF32 等)生成一组类型

发布于 2024-08-21 05:40:58 字数 805 浏览 5 评论 0原文

我试图弄清楚如何使用 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 中缺乏模板支持以及泛型不适合解决我的问题。

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
};

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

PointF32, PointF64, PointI32 etc.

where PointF32 would be:

struct PointF32
{
     float X;
     float Y;
};

That is, based on a list of types:

short, int, long, float, double etc. 

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

动次打次papapa 2024-08-28 05:40:58

根据 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"

This将定义类型:

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

想象一下,然后用 C++/CLI 值类型代替 C++ 结构,即:

public value class Point 

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

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:

#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"

This will define the types:

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

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

public value class Point 

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

ま昔日黯然 2024-08-28 05:40:58

(模板前)版本的 C++ 编译器通常具有用于此类内容的 标头。我会搜索旧版本的 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++ 等)。这可能是最好的解决方案。

Old (pre-template) versions of C++ compilers had often a <generic.h> headers for such kind of thing. I'd search old versions of g++ for it. It was before my time, so I don't know if it would suit you or not.

Alternatively, something like

#define TYPE short
#define TYPES I16
#include "Point.def"
#undef TYPE
#undef TYPES
#define TYPE int
#define TYPES I32
#include "Point.def"

could also help you.

Or obviously an external code generator (in awk, perl, C++, whatever). That could be the best solution.

灯下孤影 2024-08-28 05:40:58

以下代码未经测试,但应该是实现您想要的结果的良好开端。

在 my_structs.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_structs.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

The following code is untested but should be a good start to make what you want happen.

In 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

and in 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
尐偏执 2024-08-28 05:40:58

这似乎是一个老问题但是......
更容易做到这一点

#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

我认为仅使用标准宏处理器(CPP)或者这种变体(遵循“规范”)

#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

This seems an old question but....
I think is easier to do it just with the standard macroprocessor (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

Or maybe this variation (to follow the 'specifications')

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文