How can I use Enum::GetName in unmanaged C++ - c++

I am using managed extensions in VS 2008
I want to print the name of an en enum value
This code used to be fine VS 2003
Enum::GetName(__typeof(COMMAND_CODES),__box(iTmp))
but now I get a comile error
here is my enum
typedef enum { /* Command codes */
UMPC_NULL = 0,
} COMMAND_CODES
Any clues ?
;

As far as I know, that is not possible in plain C++ since it doesn't have reflection.
You can use macros in plain C++ to workaround it:
#define COMMAND_CODES \
ENUM_OR_STRING(CODE1), \
ENUM_OR_STRING(CODE1),
// Enum
#define ENUM_OR_STRING(x) x
enum CommandCodes
{
COMMAND_CODES
};
#undef ENUM_OR_STRING
// Names
#define ENUM_OR_STRING(x) #x
char *CommandCodeNames[] =
{
COMMAND_CODES
};
#undef ENUM_OR_STRING
Now the name of enum member is as easy to get as CommandCodeNames[(int)commandCode].

Can you use rtti typeid() and use the name() field?
Edit: From comment:
Enum::GetName(COMMAND_CODES::typeid,iTmp)

Related

How deep can I #define?

I need to use #define and using = ; as much as I can to replace possibly everything in C++ with emojis 😝😜😫😻😋.
Is it possible to #define preprocessors like #define 😎 #define or at least #define 😖 if, #define 🍉 ==, etc.? Maybe with 'using'?
I'd like to replace operators, core language instructions... Is it possible anyhow?
I know the aboves doesn't work, but maybe there is a way?... Please help me make something funny! :D
Yes, you can. You may need to think about the syntax. Easiest would be to use one emoji per keyword. However you may still need to write function- and variable names in clear text.
As per Romens comment I tried it and you can also replace method names with emojis.
Just as a proof of concept, the following code compiles in visual studio 2019 with platform toolset v142.
#include <iostream>
#define 😎 int
😎 🍉() {
std::cout << "I'm 🍉!";
return 1;
}
😎 main() {
🍉();
}
Or even more to include some of the comments:
#include <iostream>
#define 🙈 using
#define 🤷🏻‍ cout
#define 😎 int
namespace 🍏 = std;
🙈 🍏::🤷🏻‍;
😎 🍉() {
🤷🏻‍ << "I'm";
🍏::cout << "🍉!";
return 1;
}
😎 main() {
🍉();
}
Also using is something else than #define. You will only need the latter.
Is it possible to #define preprocessors like #define 😎 #define
No, it is not possible to define macros to replace preprocessor directives. (Also, macros cannot expand into directives either).
or at least #define 😖 if
This is potentially possible. It depends on the compiler what input character encoding it supports. Emojis are not listed in the basic source character set specified by the language standard, so they might not exist in the character encoding used by the compiler.
Maybe with 'using'?
Emojis are equally allowed for using as they are for macros.
Note that any identifier could be an emoji (assuming they are supported in the first place) including functions, types and variables. Example:
struct 🍏🍏 {};
struct 🍊🍊 {};
int main() {
🍏🍏{} == 🍊🍊{};
}

initialize a typedef struct variable in CPP

I'm trying to access a 3rd party library in which one of the header contains a struct as follows
#if defined(V1) || defined(V3)
typedef struct
{
int8 ErrorCode;
boolean isValid;
} validation, *validation_p;
#endif // #ifdef V1/V3
So if i would like to access validation/validation_p how can I access within my cpp, It's very clear that we need to define V1/V3 but where and how do I define those??
Sorry for the basic question on CPP
To define a macro in the C preprocessor, use the syntax #define MACRO VALUE, or, for an empty flag macro, just #define MACRO. So, in your case, your code should like like:
#define V3 // or V1
#include <thirdpartylib>

Variadic Macros

I came across this code that involved variadic Macros and I wanted to know what that meant
#define DECLARE_LEGACY_TYPES(...) //This all of the macro - I am not holding out on anything
Now There is this class as this
Header file: .h
namespace LG_Wrapper
{
template <LG_Thread Thread>
class EffectApplication : public ktApplication
{
public:
static EffectApplication<Thread>& GetInstance();
protected:
.....
.....
static boost::recursive_mutex mResource;
}
}
DECLARE_LEGACY_TYPES(EffectApplication); <---- What does this do ?
I wanted to know what effect the macro has ?
Update:
I have received numerous downvotes on this as this question gives of the impression that something is missing that I did not post the entire content of the macro. There is nothing more to the macro. I wish there was. This question is related to this which was closed. The macro literally just ends after (...)
#define DECLARE_LEGACY_TYPES(...)
but there isnt. That is one of the reason why I am here as I am not sure how to deal with this situation. Does this macro have not effect then ?
More Info:
This is what I have in another file
I am using the following defined in my project setting
LG_WRAPPER_EXPORTS
LG_THREAD_NAME=GAME
Following is the code
namespace LG_Wrapper
{
enum LG_Thread
{
GAME,
OTHER
};
/*
If the library itself is including this file
*/
#ifdef LG_WRAPPER_EXPORTS
#ifndef LG_THREAD_NAME
#error You must define LG_THREAD_NAME!
#endif
//Legacy types should not be used internally
#define DECLARE_LEGACY_TYPES(...)
#else // LG_WRAPPER_EXPORTS
//Legacy typenames are provided for convenience to the client
#define DECLARE_LEGACY_TYPES(ClassType) \
typedef LG_Wrapper::##ClassType##<LG_Wrapper::GAME> ClassType; \
#endif // LG_WRAPPER_EXPORTS
}
This is actually pretty common, but it depends on other code that wasn't mentioned in the other code you looked at:
#if USING_OLD_COMPILER //when using an older compiler, use this to declare legacy types
#define DECLARE_LEGACY_TYPES(...) STUFF(__VA_ARGS__)
#else //new compiler doesn't have to do anything special
#define DECLARE_LEGACY_TYPES(...)
#endif
//in older compilers we had to declare legacy types for this
//newer compilers don't need this step, so this does nothing at all in them.
DECLARE_LEGACY_TYPES(EffectApplication);
I don't actually know this macro, so I don't know it's actual purpose. But it's common to see macros without definitions for similar tricks as this.

Inhibit macro expansion

Is there any way to inhibit preprocessor macro expansion? I have an existing C header file that uses #define to define a set of integers and I would like to copy it to a C++ enum that has the same value names. For example (using C++11):
enum MyEnum {
VALUE,
// ...
};
#define VALUE 0
MyEnum convert(int x) {
if (x == VALUE) {
return MyEnum::VALUE;
}
// ...
}
The problem of course is that MyEnum::VALUE gets translated to MyEnum::0, which causes a syntax error. The best solution is to replace the macros with enums, but unfortunately that is not an option in my situation.
I tried to use concatenation, but that didn't help (the compiler gave the same error).
#define CONCAT(a,b) a##b
// ...
return MyEnum::CONCAT(VA,LUE); // still results in MyEnum::0
Is there another solution that allows me to have the same name for the macro and for the enum value?
You can undefine a macro:
#undef VALUE
after including the header.

Macro to return a unique string at compile-time

Is there a Macro that can return a unique-string at compile-time, so that it could be used as the name of an objective-c class?
I'm thinking of something like:
#define my_macro(params) \
#implementation my_macro_function_giving_unique_string_(MyTrickyRuntimeExtension) \
//Do stuff \
#end \
there is a macro __COUNTER__ predefined in Visual Studio that could help
I used iammilind suggestions
#define UNIQUE2(param) YourClassBaseNames##param
#define UNIQUE1(param) UNIQUE2(param)
#define UNIQUE UNIQUE1(__COUNTER__)
class UNIQUE
{
};
class UNIQUE
{
};
counter - returns new count ech time
reference: http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx
it is also available in GCC
In C++, it can be:
#define UNIQUE_NAME2(LINE) Class##LINE
#define UNIQUE_NAME1(LINE) UNIQUE_NAME2(LINE)
#define UNIQUE_NAME UNIQUE_NAME1(__LINE__)
And just use it as:
class UNIQUE_NAME {};
[Note: Declare 1 class per line and don't hope for forward declaring it anywhere :)]