Passing string or wstring to macro in c++ - c++

How to pass string or wstring to macro and print string in macro
i am tried something below
#define JUMP_ON_FAILURE_MSG( ErrorCode , Text ) GET_ERROR( ErrorCode, #Text )
#define GET_ERROR( ErrorCode, Text) \
{ \
if (ErrorCode < 0) \
{ \
wprintf(L"Error :%s :[%d]", #Text , error_code); \
} \
}
Above macro does not print proper output i gets
Error :??????????? :[-5200]
text not gets printed
i am calling above code as
JUMP_ON_FAILURE_MSG(-5200,L"Print error msg");
or
JUMP_ON_FAILURE_MSG(-5200,"Print error msg");
Please help

As others already pointed out in the comments you have to write
#define JUMP_ON_FAILURE_MSG( ErrorCode , Text ) GET_ERROR( ErrorCode, Text )
for instance. Without the #.
For further reference look here.

Related

how to understand #define GET(name,type) type Get ## [duplicate]

This question already has answers here:
What does ## (double hash) do in a preprocessor directive?
(2 answers)
Closed last month.
While reading this code in .h file about tlm,something that confuses me comes up:
//
// Generators for Setters and Getters.
//
#define CHIATTR_PROP_GETSET_GEN(name, type) \
type Get ## name (void) const { return name ; } \
void Set ## name (type new_v) { name = new_v; }
#define CHIATTR_PROP_GETSET_GEN_FUNC_NAME(func_name, prop_name, type) \
type Get ## func_name (void) const { return prop_name ; } \
void Set ## func_name (type new_v) { prop_name = new_v; }
and it is used like this:
CHIATTR_PROP_GETSET_GEN(ReturnNID_StashNID, uint16_t)
CHIATTR_PROP_GETSET_GEN_FUNC_NAME(ReturnNID,
ReturnNID_StashNID,
uint16_t)
what happens in this sentence?
type Get ## name (void) const { return name ; } \
Eagerly awaiting the answer!
The ## operator takes two separate tokens and pastes them together to form a single token.
From your examples :
CHIATTR_PROP_GETSET_GEN(ReturnNID_StashNID, uint16_t)
Would be replaced at the preprocessor step by the following code :
uint16_t GetReturnNID_StashNID (void) const { return ReturnNID_StashNID; }
void SetReturnNID_StashNID (uint16_t new_v) { ReturnNID_StashNID = new_v; }

How to print line number and file name of calling function not the log.cpp?

I write a log utils with c++(log.cpp), but when I use it in my other class, I hope to print the line number and file name in calling functions, but unfortunately, it just print the line number and file name of log.cpp. That really confused me.
in Log function, I defined the prefixFormat like that, and add [%s][%d] format in order to print file name and line name, and set in strPrefix.Format
strPrefix.Format(prefixFormat, time.wHour, time.wMinute, time.wSecond, stamp, __FILE__, __LINE__, levelNames[loglevel]);
void CLog::Log(int loglevel, const char *format, ... )
{
pthread_mutex_lock(&m_log_mutex);
static const char* prefixFormat = "%02.2d:%02.2d:%02.2d T:%" PRIu64 " [%s][%d] %7s: ";
#if !(defined(_DEBUG) || defined(PROFILE))
if (m_logLevel > LOG_LEVEL_NORMAL ||
(m_logLevel > LOG_LEVEL_NONE && loglevel >= LOGNOTICE))
#endif
{
if (!m_file)
{
pthread_mutex_unlock(&m_log_mutex);
return;
}
struct timeval now;
gettimeofday(&now, NULL);
SYSTEMTIME time;
time.wHour=(now.tv_sec/3600) % 24;
time.wMinute=(now.tv_sec/60) % 60;
time.wSecond=now.tv_sec % 60;
uint64_t stamp = now.tv_usec + now.tv_sec * 1000000;
CStdString strPrefix, strData;
strData.reserve(16384);
va_list va;
va_start(va, format);
strData.FormatV(format,va);
va_end(va);
if (m_repeatLogLevel == loglevel && m_repeatLine == strData)
{
m_repeatCount++;
pthread_mutex_unlock(&m_log_mutex);
return;
}
else if (m_repeatCount)
{
CStdString strData2;
strPrefix.Format(prefixFormat, time.wHour, time.wMinute, time.wSecond, stamp, levelNames[m_repeatLogLevel]);
strData2.Format("Previous line repeats %d times." LINE_ENDING, m_repeatCount);
fputs(strPrefix.c_str(), m_file);
fputs(strData2.c_str(), m_file);
OutputDebugString(strData2);
m_repeatCount = 0;
}
m_repeatLine = strData;
m_repeatLogLevel = loglevel;
unsigned int length = 0;
while ( length != strData.length() )
{
length = strData.length();
strData.TrimRight(" ");
strData.TrimRight('\n');
strData.TrimRight("\r");
}
if (!length)
{
pthread_mutex_unlock(&m_log_mutex);
return;
}
OutputDebugString(strData);
/* fixup newline alignment, number of spaces should equal prefix length */
strData.Replace("\n", LINE_ENDING" ");
strData += LINE_ENDING;
strPrefix.Format(prefixFormat, time.wHour, time.wMinute, time.wSecond, stamp, __FILE__, __LINE__, levelNames[loglevel]);
fputs(strPrefix.c_str(), m_file);
fputs(strData.c_str(), m_file);
//fputs(strPrefix.c_str(), stdout);
//fputs(strData.c_str(), stdout);
fflush(m_file);
}
pthread_mutex_unlock(&m_log_mutex);
}
but when I call like this Clog::Log(DEBUG,"XXXXX") in other classes; I get the file with log.cpp and line number in log.cpp like that, that's really confused me.
05:04:49 T:18446744072485586921 [utils/log.cpp][129] DEBUG: Normal M:16829313 (A:23219955 V:0) P:0 A:6.39 V:-16.83/T:0.20 (0,0,1,1) A:99% V:0% (6.07,6.34)
05:04:49 T:18446744072485607233 [utils/log.cpp][129] DEBUG: Normal M:16849626 (A:23219955 V:0) P:0 A:6.37 V:-16.85/T:0.20 (0,0,1,1) A:99% V:0% (6.05,6.34)
__LINE__ and __FILE__ are preprocessor macros and they are not dependent on the code that calls them. A standard way to handle this is to create a wrapper macro around your logging function and let the caller use it instead of calling the logging func directly.
For example:
#define Logme(loglevel, format, ... ) Log(__LINE__, __FILE__, loglevel, format, __VA_ARGS__)
Inside the Log function you'll know that first 2 arguments are line number and file name so you can parse them. Rest of the arguments can be parsed as before.

Getting the address of a class member fuction

How do I get the memory address of a class member function, I'm using a library statically that was previously setup dynamically, as the platform I ported the game to does not support dynamic libraries. It works well except for one issue.
When restoring a saved game NPCs become static as they don't continue to run the functions that were active when the game was saved. It did this by looking up the function address to get the name in the global offset table and symbol table when saving, when restoring it got the address using the name from the GOT and ST.
As this will not work with static linking I'm trying to figure out how to get the address of the exported functions to store it in a std::map with the address and a unique name when the application first starts.
The following doesn't seems to work for me for some reason. Any help would be much appreciated :).
Storing the address for the save game file in the base class using the DEFINE_FIELD macro:
// Global Savedata for Delay
TYPEDESCRIPTION CBaseEntity::m_SaveData[] =
{
DEFINE_FIELD( CBaseEntity, m_pfnThink, FIELD_FUNCTION )
};
Derived class export from when it was using dynamic linking:
extern "C" _declspec( dllexport ) void CMultiManager( entvars_t *pev );
Derived class where ManagerThink() was exported previously when it used dynamic linking:
class CMultiManager : public CBaseEntity
{
public:
CMultiManager();
void /*EXPORT*/ ManagerThink ( void );
}
My new class constructor in the derived class to try and get the address of the member function (so I can store it in std:map with a name). Is it possible to this in the global scope rather than a constructor?
CMultiManager::CMultiManager()
{
//Try the ManagerThink function directly
void (CBaseEntity ::*ptrTemp)(void);
ptrTemp = static_cast <void (CBaseEntity::*)(void)> (ManagerThink);
void* iMemoryAddressFunc = &ptrTemp;
ALERT( at_error, "__TEST__ FUCNTION __ EXPORT ADDRESS: CMultiManager::ManagerThink (%08lx)\n", (unsigned long)iMemoryAddressFunc );
//-------------------------------------------------------------------------------
//Try the inherited m_pfnThink variable ManagerThink() is stored in as well.
void* iMemoryAddressVar = &m_pfnThink;
ALERT( at_error, "__TEST__ M_PFNTHINK __ EXPORT ADDRESS:
CMultiManager::ManagerThink (%08lx)\n", (unsigned long)iMemoryAddressVar );
}
Function check method called before restoring the game, it's using the function address stored in the save game data in the base class (function above).
void FunctionCheck( void *pFunction, char *name )
{
if (pFunction && !NAME_FOR_FUNCTION((unsigned long)(pFunction)) )
ALERT( at_error, "No EXPORT: %s:%s (%08lx)\n", STRING(pev->classname), name, (unsigned long)pFunction );
}
Debug log when running the game:
Obviously it's not going to find the export as I haven't setup anything yet as I need the address to store first, but why do the addresses not all match up? ManagerThink() has been assigned to m_pfnThink as well, so why are the first two in the log not at the same address at least?
From CMultiManager():
__TEST__ FUCNTION __ EXPORT ADDRESS: CMultiManager::ManagerThink (d008759c)
__TEST__ M_PFNTHINK __ EXPORT ADDRESS: CMultiManager::ManagerThink (01eb0e08)
From FunctionCheck();
No EXPORT: multi_manager:ManagerThink (00226c0a)
The ManagerThink() Function:
// Designers were using this to fire targets that may or may not exist --
// so I changed it to use the standard target fire code, made it a little simpler.
void CMultiManager :: ManagerThink ( void )
{
float time;
time = gpGlobals->time - m_startTime;
while ( m_index < m_cTargets && m_flTargetDelay[ m_index ] <= time )
{
FireTargets( STRING( m_iTargetName[ m_index ] ), m_hActivator, this, USE_TOGGLE, 0 );
m_index++;
}
if ( m_index >= m_cTargets )// have we fired all targets?
{
SetThink( NULL );
if ( IsClone() )
{
UTIL_Remove( this );
return;
}
SetUse ( ManagerUse );// allow manager re-use
}
else
pev->nextthink = m_startTime + m_flTargetDelay[ m_index ];
}
I'm also confused how it knew when setup dynamically which EXPORT ManagerThink ( void ); function to call for each instance of the object created, because it would have just stored the one EXPORT address in the GOT/symbol table right?
Any help/suggestions/advise would be great. Thank you :)
EDIT:
Thanks for the replies. I've managed to find a work around the issue.
I looked at a newer version of game where they updated the code to not use the GOT and ST as they needed the game on platforms that don't support dlls and were faced with the same issue. They solved it using the following macros to declare a structure and store defined pointers to member functions in the structure.
//-----------------------------------------------------------------------------
//
// Macros used to implement datadescs
//
#define DECLARE_SIMPLE_DATADESC() \
static datamap_t m_DataMap; \
static datamap_t *GetBaseMap(); \
template <typename T> friend void DataMapAccess(T *, datamap_t **p); \
template <typename T> friend datamap_t *DataMapInit(T *);
#define BEGIN_SIMPLE_DATADESC( className ) \
datamap_t className::m_DataMap = { 0, 0, #className, NULL }; \
datamap_t *className::GetBaseMap() { return NULL; } \
BEGIN_DATADESC_GUTS( className )
#define BEGIN_SIMPLE_DATADESC_( className, BaseClass ) \
datamap_t className::m_DataMap = { 0, 0, #className, NULL }; \
datamap_t *className::GetBaseMap() { datamap_t *pResult; DataMapAccess((BaseClass *)NULL, &pResult); return pResult; } \
BEGIN_DATADESC_GUTS( className )
#define DECLARE_DATADESC() \
DECLARE_SIMPLE_DATADESC() \
virtual datamap_t *GetDataDescMap( void );
#define BEGIN_DATADESC_NO_BASE( className ) \
datamap_t className::m_DataMap = { 0, 0, #className, NULL }; \
datamap_t *className::GetDataDescMap( void ) { return &m_DataMap; } \
datamap_t *className::GetBaseMap() { return NULL; } \
BEGIN_DATADESC_GUTS( className )
#define BEGIN_DATADESC( className ) \
datamap_t className::m_DataMap = { 0, 0, #className, NULL }; \
datamap_t *className::GetDataDescMap( void ) { return &m_DataMap; } \
datamap_t *className::GetBaseMap() { datamap_t *pResult; DataMapAccess((CBaseEntity *)NULL, &pResult); return pResult; } \
BEGIN_DATADESC_GUTS( className )
#define BEGIN_DATADESC_GUTS( className ) \
template <typename T> datamap_t *DataMapInit(T *); \
template <> datamap_t *DataMapInit<className>( className * ); \
namespace className##_DataDescInit \
{ \
datamap_t *g_DataMapHolder = DataMapInit( (className *)NULL ); /* This can/will be used for some clean up duties later */ \
} \
\
template <> datamap_t *DataMapInit<className>( className * ) \
{ \
typedef className classNameTypedef; \
static CDatadescGeneratedNameHolder nameHolder(#className); \
className::m_DataMap.baseMap = className::GetBaseMap(); \
static typedescription_t dataDesc[] = \
{ \
{ FIELD_VOID, 0, 0, 0, 0 }, /* so you can define "empty" tables */
#define END_DATADESC() \
}; \
\
if ( sizeof( dataDesc ) > sizeof( dataDesc[0] ) ) \
{ \
classNameTypedef::m_DataMap.dataNumFields = SIZE_OF_ARRAY( dataDesc ) - 1; \
classNameTypedef::m_DataMap.dataDesc = &dataDesc[1]; \
} \
else \
{ \
classNameTypedef::m_DataMap.dataNumFields = 1; \
classNameTypedef::m_DataMap.dataDesc = dataDesc; \
} \
return &classNameTypedef::m_DataMap; \
}
// replaces EXPORT table for portability and non-DLL based systems (xbox)
#define DEFINE_FUNCTION_RAW( function, func_type ) { FIELD_VOID, nameHolder.GenerateName(#function), /*{ NULL, NULL },*/ 1, FTYPEDESC_FUNCTIONTABLE, /*NULL, NULL,*/ (inputfunc_t)((func_type)(&classNameTypedef::function))},
//------------------------------------------------------------------------------

CppUnit expect exception with Assert Throw compiles with warning C4127 [duplicate]

This question already has answers here:
do while(false) pattern [duplicate]
(7 answers)
Closed 9 years ago.
Currently I am writing unit tests in C++ with CppUnit. Recently I needed to check that an exception is thrown in a specific case using CppUnits macro:
CPPUNIT_ASSERT_THROW(
boost::get<FooClassInBoostVariant>(m_boostVariantFooOrBar),
boost::bad_get);
the warning during the compilation of the test surprised me (on VS2010, but will be a warning on other compilers as well...):
warning C4127: conditional expression is constant
I looked into the macro definition of CppUnit and found the following:
do { \
bool cpputExceptionThrown_ = false; \
try { \
expression; \
} catch ( const ExceptionType & ) { \
cpputExceptionThrown_ = true; \
} \
\
if ( cpputExceptionThrown_ ) \
break; \
\
CPPUNIT_NS::Asserter::fail( \
"Expected exception: " #ExceptionType \
" not thrown.", \
CPPUNIT_SOURCELINE() ); \
} while ( false )
Well, I totally understand how this works, the do while loop is executed only once, because of the false, and the break is used to not execute the Asserter::fail() part. But why are they doing it like this? It - of course - triggers the compiler warning, as the break condition for the while loop is obviously always "false". But isn't there a more elegant way to do this? I usually adhere to the no-warning-compilation principle, so this really bugs me.
So my question really is, why didn't they implement it like this:
{ \
bool cpputExceptionThrown_ = false; \
try { \
expression; \
} catch ( const ExceptionType & ) { \
cpputExceptionThrown_ = true; \
} \
\
if ( !cpputExceptionThrown_ ) { \
CPPUNIT_NS::Asserter::fail( \
"Expected exception: " #ExceptionType \
" not thrown.", \
CPPUNIT_SOURCELINE() ); \
} \
}
Thanks in advance!
-Hannes
The reason is to make the assertion one statement. Consider these two uses of the macro:
CPPUNIT_ASSERT_THROW(foo(), MyException); // a
CPPUNIT_ASSERT_THROW(foo(), MyException) // b - without trailing `;`!
doSomething();
With their code, you'd get an error with //b, since the code expands to do { ... } while (false) doSomething(); - you'd be missing the ; after the condition.
With your code, //b would happily compile, but //a could give you an "empty statement" warning, since that line would expand to { ... };, with the superfluos ; after the block.
Why they force you to use //a I don't know - but I like //b way more because it's just consistent to have a ; after each line. One does not have to distinguish lines with assertions from normal statements.
PS:
I am not sure but there might be more differences between { ... } blocks and do {...} while(false) statements that will allow to put an assertion macro in places where simple blocks are not allowed.
Edit: with C++11, you could use a lambda (define and call it in one place):
#define CPPUNIT_ASSERT_THROW(expression, ExceptionType) \
[&]() -> void { \
bool cpputExceptionThrown_ = false; \
try { \
expression; \
} catch ( const ExceptionType & ) { \
cpputExceptionThrown_ = true; \
} \
\
if ( cpputExceptionThrown_ ) \
return; \
\
CPPUNIT_NS::Asserter::fail( \
"Expected exception: " #ExceptionType \
" not thrown.", \
CPPUNIT_SOURCELINE() ); \
}()
However, there might be caveats, e.g. due to the lambda capturing the variables you use in the expression.
OK, I guess I found the answer by myself:
http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ gives the explanation.
It is actually a common practice to wrap multi-line macros in do { } while (false);. This is a workaround to allow for using those macros, e.g., in un-braced if else.
if (condition_a)
MULTI_LINE_MACRO();
else
MULTI_LINE_MACRO_2();
The result would be that unexpectedly only the first line gets executed, which definitely leads to unexpected behaviour. So they were not entirely incompetent I guess...
http://kernelnewbies.org/FAQ/DoWhile0 also explains why my solution would not work. The MULTI_LINE_MACRO(); within the if would expand, e.g., to
if (condition_a)
{ /* macro stuff */ }
;
else // << never executed because of the ; above.
So I guess I have to disable the warning. GCC has a workaroud for this ( ({ MACRO }) ), called a Statement Expression, but I don't think this works on VS2010.

macro expansion in C/C++

I have this macro from someone's code:
#define Q_DEF_PROTOTYPE( Type, Name ) Type (*Name)
#define COPY_FP( pDest, pSrc ) (*((void**)(&(pDest)))) = ((void*)(pSrc))
#define LIB_QUERY(lib_handle, proc) dlsym(lib_handle, proc)
#define Q_DEF_PROTOTYPE( Type, Name ) \
COPY_FP( p->Name, LIB_QUERY( g_library, STRINGIZE(FUNC(Name)) ) ); \
void dummy_##Name
Not sure, what "void dummy_##Name" does? Thanks.
It replace the ##Name with the value of Name parameter as string.
Q_DEF_PROTOTYPE(myType, objectName) => void dummy_objectName