Re: Using C++ enums like C# enums
I found out that only the Microsoft allows you to forward declare enums, but standard compliant compilers do not allow it since its size is not yet known.
I could use an int rather than the enum variable to get around the problem, but I prefer to be able to see what the enum is set to, so here is the macro to make the code work in non-Microsoft compilers.
#define ATHENA_START_CSHARP_ENUM( x ) \
struct x \
{ \
public: \
enum EnumType
#define ATHENA_END_CSHARP_ENUM( x ) \
; \
\
inline x() \
{ \
} \
\
template<typename T> \
inline x( const T & inValue ) \
: \
value( (EnumType)inValue ) \
{ \
} \
\
inline x( const EnumType inValue ) \
: \
value( inValue ) \
{ \
} \
\
inline void operator = ( const EnumType inValue ) \
{ \
value = inValue; \
} \
\
inline EnumType operator ~ () const \
{ \
return (EnumType)~value; \
} \
\
inline EnumType operator & ( const EnumType inValue ) const \
{ \
return (EnumType)( value & inValue ); \
} \
\
inline operator EnumType () const \
{ \
return value; \
} \
\
private: \
EnumType value; \
}
POSIX Thread: Setting Priority and Stack Size
The POSIX Thread library, also known as pthread, has always annoyed me with trying to set thread priority.
Some documentation for pthread_attr_setschedparam() say use PTHREAD_MIN_PRIORITY and PTHREAD_MAX_PRIORITY, however they never seem to exist.
Some sites say use 0 and 31 for them respectively for min and max.
Instead I found a function that will retrieve the min and max priority for a given scheduling policy, allowing you to set the thread priority before it is created.
void SetAttributeMaxPriority( pthread_attr_t & inThreadAttr )
{
const int kPolicy = SCHED_RR;
int minPriority = sched_get_priority_min( kPolicy );
int maxPriority = sched_get_priority_max( kPolicy );
//
sched_param schedParam;
schedParam.sched_priority = maxPriority;
pthread_attr_setschedparam( &inThreadAttr, &schedParam );
//
pthread_attr_setschedpolicy( &inThreadAttr, kPolicy );
}
The stack size also needs to be a multiple of the page size, while this is not really an issue unless you go really low or don't use a power of 2 number, I added some code to reinforce it.
void SetStackSize( pthread_attr_t & inThreadAttr, const int inStackSize )
{
//
int stackSize = ( inStackSize < PTHREAD_STACK_MIN ) ? PTHREAD_STACK_MIN : inStackSize;
//
int pageSize = sysconf( _SC_PAGESIZE );
if( pageSize == -1 )
{
pageSize = PTHREAD_STACK_MIN;
}
int multipleRemainder = mParams.stackSize % pageSize;
if( multipleRemainder != 0 )
{
stackSize += ( pageSize - multipleRemainder );
}
//
pthread_attr_setstacksize( &inThreadAttr, stackSize );
}
I wasn't sure if _SC_PAGESIZE would work on every system, so just incase I made page size equal PTHREAD_STACK_MIN if an error was returned.
C/C++ Operator Synonyms
I just discovered that there are defines as part of the standard that can be used instead of some operators. For example instead of this:
//
if( something && somethingelse )
{
}You can do this:
//
if( something and somethingelse )
{
}There also exists or, xor, and bitwise versions, bitand, bitor.
For more, read the article on the Wikipedia.
iOS Semaphore Problems
Originally for my semaphore I used an Apple specific one that used variable type semaphore_t.
But recently I decided to use a more generic unix one sem_t but started having trouble since it failed to create one.
After a bit of changing the code, I found the call to sem_init() always failed, returning an error code on iOS.
The only way I could get it to work was to use sem_open() with the O_CREAT flag.
//
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <assert.h>
//
sem_t * CreateSemaphore( const char * inName, const int inStartingCount )
{
sem_t * semaphore = sem_open( inName, O_CREAT, 0644, inStartingCount );
//
if( semaphore == SEM_FAILED )
{
switch( errno )
{
case EEXIST:
printf( "Semaphore with name '%s' already exists.\n", inName );
break;
default:
printf( "Unhandled error: %d.\n", errno );
break;
}
//
assert(false);
return SEM_FAILED;
}
//
return semaphore;
}
This code however is flawed in that if the semaphore already exists it will use that one instead. So I tried to BitOR the flag O_EXCL to O_CREAT so it asserted if the semaphore existed already.
That worked, however now whenever the app didn't shutdown correctly, the semaphore would not be destroyed and running again would fail since it already existed. I have not yet found a way around this.
Finally to destroy the semaphore, you need to close it with sem_close() instead of sem_destroy().
//
bool DestroySemaphore( sem_t * inSemaphore )
{
int retErr = sem_close( inSemaphore );
//
if( retErr == -1 )
{
//
switch( errno )
{
case EINVAL:
printf( "inSemaphore is not a valid sem_t object." );
break;
default:
printf( "Unhandled error: %d.\n", errno );
break;
}
//
assert(false);
return false;
}
//
return true;
}
//
void SignalSemaphore( sem_t * inSemaphore )
{
sem_post( inSemaphore );
}
//
void WaitSemaphore( sem_t * inSemaphore )
{
sem_wait( inSemaphore );
}
//
bool TryWaitSemaphore( sem_t * inSemaphore )
{
int retErr = sem_trywait( inSemaphore );
//
if( retErr == -1 )
{
//
if( errno != EAGAIN )
{
printf( "Unhandled error: %d\n", errno );
assert( false );
}
//
return false;
}
//
return true;
}
Forward Declare a typedef
Recently I came by a problem where a forward declare was needed for an external library, but the variable type was declared as a typedef of a structure with no name.
To resolve it, I forward declared a structure name of my own choosing, and then in the cpp file made the structure inherit off the typedef.
//
struct MyForwardDeclare;
//
void SomeFunc();
//
void SomeFunc( const MyForwardDeclare & inVariable );//
//
#include "SomeFile.h"
//
//
typedef struct
{
int x;
} SomeStruct;
//
//
struct MyForwardDeclare : SomeStruct {};
//
//
void SomeFunc()
{
MyForwardDeclare var;
var.x = 1;
SomeFunc( var );
}
//
//
void SomeFunc( const MyForwardDeclare & inVariable )
{
printf( "%d", inVariable.x );
}Not the best example since the code was inside a class, but thats the general idea.
Using C++ enums like C# enums
There is an update to this post: Re: Using C++ enums like C# enums
I've been playing around with idea for a while in my head, a way to have enums work similar to how they work in C#, where they are part of an object.
Usually I would do this.
enum BufferFlags
{
kBufferFlags_Read = 1 << 0,
kBufferFlags_Write = 1 << 1
};
Since I prefix all my enums with kBufferFlags_, so to access the read enum I'd write kBufferFlags_Read. I can reduce out this bit of code in C# to be an enum object called BufferFlags, and have enums Read and Write. Accessing them with BufferFlags.Read.
So how do I do it in C++? By putting the enum within a struct, I can access them with BufferFlags::Read. Not quite the same, but better. However the structure needs to operate like an enum, so I overloaded the cast operator to return the enum, and the assignment and copy constructors to take the enum too.
I choose to keep the internal value hidden, but it can all be public.
#define ATHENA_START_CSHARP_ENUM( x ) struct x \
{ \
public: \
enum xEnum; \
\
inline x() \
{ \
} \
\
inline x( const xEnum inValue ) \
: \
value( inValue ) \
{ \
} \
\
inline void operator = ( const xEnum inValue ) \
{ \
value = inValue; \
} \
\
inline operator xEnum () const \
{ \
return value; \
} \
\
private: \
xEnum value; \
\
public: \
enum xEnum
#define ATHENA_END_CSHARP_ENUM() ; \
};
Originally, the end macro use to take in the name of the enum as well. The reason for this was that the variable value used its type so when viewed in the debugger I could see its value as an enum name instead of a number.
So I changed the enum to be forward declared. To use this macro you now declare an enum like this.
ATHENA_START_CSHARP_ENUM( BufferFlags )
{
Read = 1 << 0,
Write = 1 << 1
}
ATHENA_END_CSHARP_ENUM()
And an example of it in use.
int main()
{
BufferFlags flags = BufferFlags::Read;
//
switch( flags )
{
case BufferFlags::Read:
break;
case BufferFlags::Write:
break;
default:
break;
}
//
return 0;
}
Its a bit more hassle to use it, but it does look nicer to look at when used.


