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
int GetEnumValue( Months m )
{
return m;
}
You can also use maps
Rodolfo
"Udo Weik" <WeikE...@aol.com> wrote in message
news:3DA83922...@aol.com...
I'm reasonable new to BCB. Demonstrate a map too please.
Robert
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
#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...
#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
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