Boost biblioteca preprocessore per generare un insieme di tipi basato su un elenco dei tipi fondamentali esempio PointI32, PointF32 ecc in C ++ / CLI
-
19-09-2019 - |
Domanda
Sto cercando di capire come utilizzare la libreria http : //www.boost.org/doc/libs/release/libs/preprocessor a svilupparsi un tipo "generico" per i diversi tipi specifici. Qui di seguito vi chiedere questo per un semplice esempio di classe punto. Dato:
struct Point##TYPE_SUFFIX_NAME
{
TYPE X;
TYPE Y;
// Other code
};
voglio generare questo tipo per i diversi tipi di dati (POD) di base per esempio:.
PointF32, PointF64, PointI32 etc.
dove PointF32 potrebbe essere:
struct PointF32
{
float X;
float Y;
};
Cioè, sulla base di un elenco dei tipi:
short, int, long, float, double etc.
Voglio "dispiegare" il tipo di cui sopra per questi. Preferibilmente con la definizione di "modello" in un separato file include e non come una macro, per consentire la più agevole il debug.
Nota: Io non sono interessato a sentir parlare di modelli C ++. So come utilizzare i modelli. Ma questi non sono utili nel mio caso. A titolo di esempio immaginate questi tipi stanno andando essere usati da NET in C #, ma vengono generati in C ++ / CLI. Quindi, per favore attenersi alla domanda.
Il problema, ovviamente, deriva dalla mancanza di sostegno modello in .NET e causa di farmaci generici non essere idoneo a risolvere il mio problema.
Soluzione 3
In base alla risposta di Benoît Sono venuto con la seguente risposta. La risposta è composto da tre file:
-
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"
In questo modo definire i tipi:
PointI8, PointI16, PointI32, PointI64,
PointUI8, PointUI16, PointUI32, PointUI64,
PointF32, PointF64
Immaginate allora invece di C ++ struct un C ++ / CLI tipo di valore cioè:.
public value class Point
Poi abbiamo fatto creato tipi di punti di tutti i tipi numerici di base per l'uso in NET esempio C #.
Altri suggerimenti
Vecchio (pre-modello) versioni dei compilatori C ++ ha avuto spesso un header <generic.h>
per tale genere di cose. Mi piacerebbe cerco vecchie versioni di g ++ per esso. E 'stato prima di me, quindi non so se sarebbe adatto voi oppure no.
In alternativa, qualcosa come
#define TYPE short
#define TYPES I16
#include "Point.def"
#undef TYPE
#undef TYPES
#define TYPE int
#define TYPES I32
#include "Point.def"
potrebbe anche aiutare.
In alternativa, ovviamente, un generatore di codice esterno (in awk, perl, C ++, a prescindere). Questo potrebbe essere la soluzione migliore.
Il seguente codice è non testati , ma dovrebbe essere un buon punto di partenza per fare ciò che si desidera accadere.
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
e 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
Questa sembra una vecchia questione, ma .... Credo che sia più facile farlo con la sola le macro standard (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
O forse questa variazione (a seguire le 'specificazioni')
#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