Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GetEnumName()/GetEnumValue() - Delphi->C++

610 views
Skip to first unread message

Udo Weik

unread,
Oct 12, 2002, 11:00:50 AM10/12/02
to
Hello,

could someone be so kind and "translate" the following Delphi-Functions
in C++ and add a small example for e. g. GetEnumName()?

function GetEnumName(TypeInfo: PTypeInfo; Value: Integer): string;
function GetEnumValue(TypeInfo: PTypeInfo; const Name: string): Integer;


Thank you.
Udo

Rodolfo Frino

unread,
Oct 12, 2002, 12:36:35 PM10/12/02
to
enum Months
{
January,
February,
March,
April,
May,
....
December
};

int GetEnumValue( Months m )
{
return m;
}

You can also use maps

Rodolfo

"Udo Weik" <WeikE...@aol.com> wrote in message
news:3DA83922...@aol.com...

Robert

unread,
Oct 12, 2002, 1:52:01 PM10/12/02
to

Rodolfo

I'm reasonable new to BCB. Demonstrate a map too please.

Robert

Udo Weik

unread,
Oct 12, 2002, 3:22:02 PM10/12/02
to
Hello Rodolfo,

thank you very much for your answer - a really good idea
to define an own function.
But I would be really happy to know how the "original"
functions defined in typinfo.pas could be used!


function GetEnumName(TypeInfo: PTypeInfo; Value: Integer): string;
function GetEnumValue(TypeInfo: PTypeInfo; const Name: string): Integer;

Thank you again.
Udo

Rodolfo Frino

unread,
Oct 13, 2002, 10:19:42 AM10/13/02
to
We use a map instead of a enum:

#include <map> //Include this header
TForm1 *Form1;

std::map<String, int> Months;
std::map<String, int>::iterator iter;

int GetValue( String S )
{
return Months[S];
}

String GetName( int value )
{
for ( iter = Months.begin(); iter != Months.end(); iter++ )
if ( value == iter->second )
return (String)iter->first;
return "Illegal value"; //Error Illegal input
}


void __fastcall TForm1::Button1Click(TObject *Sender)
{

Months["January"] = 1;
Months["February"] = 2;
Months["March" ] = 3;
Months["April" ] = 4;
Months["May" ] = 5;
Months["June" ] = 6;
Months["July" ] = 7;
Months["August" ] = 8;
Months["Setember" ] = 9;
Months["October"] = 10;
Months["November"] = 11;
Months["December"] = 12;

ShowMessage( String(GetValue( "October")) );
ShowMessage( GetName( 100 ) );
}

Rodolfo

"Robert" <ideat...@hotmail.com> wrote in message
news:3da86141$1...@newsgroups.borland.com...

Rodolfo Frino

unread,
Oct 13, 2002, 10:34:22 AM10/13/02
to
I should have used enum as well as maps, since the "conversion"
will be based on maps:

#include <map>
TForm1 *Form1;

enum
{
JANUARY = 1,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER
};

std::map<String, int> Months;
std::map<String, int>::iterator iter;

int GetValue( String S )
{
return Months[S];
}

String GetName( int value )
{
for ( iter = Months.begin(); iter != Months.end(); iter++ )
if ( value == iter->second )
return (String)iter->first;
return "Illegal value"; //Error Illegal input
}


//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Months["January"] = JANUARY;
Months["February"] = FEBRUARY;
Months["March" ] = MARCH;
Months["April" ] = APRIL;
Months["May" ] = MAY;
Months["June" ] = JUNE;
Months["July" ] = JULY;
Months["August" ] = AUGUST;
Months["Setember" ] = SEPTEMBER;
Months["October"] = OCTOBER;
Months["November"] = NOVEMBER;
Months["December"] = DECEMBER;
}
//--------------------------------------------------------------------------
-

void __fastcall TForm1::Button1Click(TObject *Sender)
{

ShowMessage( String(GetValue( "October")) );

ShowMessage( GetName( OCTOBER ) );
}

Rodolfo


Remy Lebeau (TeamB)

unread,
Oct 13, 2002, 6:10:07 PM10/13/02
to
In Delphi, enums have RTTI (runtime type information), thus functions like
GetEnumName() work fine for enum values directly. However, in C++, enums
don't have RTTI by default, so you have to do extra work in order to set
that up.

One way would be to wrap the enum into a class that exposes the enum via a
published properties. Published properties always have RTTI. Here is the
code I use myself for using GetEnumName() in C++:


--- header ---

#include <typinfo.hpp>
#include <assert.h>

enum TExtEditFlag { ... };

#define DECLARE_ENUM_TYPEINFO(e) \
\
class e##_HACKED_ENUM_TYPEINFO : public TObject \
{ \
private: \
e FTestEnum; \
__published: \
__property e eProp = \
{ read = FTestEnum, write = FTestEnum }; \
public: \
static PTypeInfo TypeInfo(void) \
{ \
PTypeInfo ClassTypeInfo; \
PPropInfo PropertyInfo; \
PTypeInfo PropertyTypeInfo; \
\
ClassTypeInfo = __typeinfo(e##_HACKED_ENUM_TYPEINFO); \
assert(ClassTypeInfo != 0); \
\
PropertyInfo = GetPropInfo(ClassTypeInfo, "eProp"); \
assert(PropertyInfo != 0); \
\
PropertyTypeInfo = *(PropertyInfo->PropType); \
assert(PropertyTypeInfo != 0); \
assert(PropertyTypeInfo->Kind == tkEnumeration); \
\
return PropertyTypeInfo; \
} \
\
static PTypeData TypeData(void) \
{ \
return GetTypeData(TypeInfo()); \
} \
};

#define GET_ENUM_TYPEINFO(e) \
\
e##_HACKED_ENUM_TYPEINFO::TypeInfo()

#define GET_ENUM_TYPEDATA(e) \
e##_HACKED_ENUM_TYPEINFO::TypeData()

DECLARE_ENUM_TYPEINFO(TExtEditFlag);


--- source ---

TExtEditFlags EditFlags = somevalue;
PTypeInfo pTypeInfo = GET_ENUM_TYPEINFO(TExtEditFlag);
PTypeData pTypeData = GET_ENUM_TYPEDATA(TExtEditFlag);
int MaxValue = (pTypeData->MaxValue/sizeof(pTypeData->NameList));

for(int x = pTypeData->MinValue; x <= MaxValue; x++)
{
if(EditFlags.Contains((TExtEditFlag)x))
TreeView2->Items->Add(NULL, GetEnumName(pTypeInfo, x));
}


Gambit

"Udo Weik" <WeikE...@aol.com> wrote in message
news:3DA83922...@aol.com...

> could someone be so kind and "translate" the following

0 new messages