I'm creating a library and I want to both hide and protect (make unwritable) a structure type (can only edit using library functions). I'm using:
typedef _STRINGLIB_STRING_CONST_ struct
{
#if defined(_STRINGLIB_OVERLOADING_WAIT_) || defined(_STRINGLIB_STRING_ACCESSIBLE_)
//actual string
char *getString;
//string size in bytes
size_t getSize;
//string size in characters (=size in bytes-1)
size_t getSizeChars;
//should both point to _STRINGLIB_ALLOCATION_TRUE_ when string is allocated
void *stringAllocation;
void *stringSignature;
#else
struct
{
size_t _block_a;
void *_block_b;
void *_block_c;
void *_block_d;
size_t _block_e;
}_block;
#endif // _STRINGLIB_OVERLOADING_WAIT_
}String;
where _STRINGLIB_STRING_CONST_
is always const unless the header is called from source file or the user declared _STRINGLIB_STRING_ACCESSIBLE_
before including the library; _STRINGLIB_OVERLOADING_WAIT_
is declared by the source file (and undeclared right after).
It looks like it works fine (i'm using GCC 4.9.2) but I want to be sure whether this is always OK or undefined behaviour.
.
.
ADDITIONAL INFORMATION: forgot to mention i have these definitions on top of the stringlib.h file:
#ifdef _STRINGLIB_OVERLOADING_WAIT_
#undef _STRINGLIB_STRING_CONST_
#define _STRINGLIB_STRING_CONST_
#elif !defined(_STRINGLIB_STRING_ACCESSIBLE_)
#undef _STRINGLIB_STRING_CONST_
#define _STRINGLIB_STRING_CONST_ const
#else
#undef _STRINGLIB_STRING_CONST_
#define _STRINGLIB_STRING_CONST_
#endif // _STRINGLIB_OVERLOADING_WAIT_
#ifndef _STRINGLIB_H_
#define _STRINGLIB_H_
//<...>