uLib
User mode C/C++ extended API library for Win32 programmers.
|
Type definitions, globals, macros, constants, and common functions.
#include <uLib/Common.h>
Macros | |
#define | NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0) |
#define | NT_ERROR(Status) (((ULONG)(Status) >> 30) == 3) |
#define | BEGIN_EXTERN_C extern "C" { |
#define | END_EXTERN_C } |
#define | BEGIN_EXTERN_CPP extern "C++" { |
#define | END_EXTERN_CPP } |
#define | BEGIN_NAMESPACE(name) namespace name { |
#define | END_NAMESPACE(name) } |
#define | BEGIN_ANONYMOUS namespace { |
#define | END_ANONYMOUS } |
#define | DEF_(x) = x |
#define | __assembly __declspec(naked) |
#define | DISABLE_BOOLCAST_WARNING __pragma( warning( disable: 4800 )) |
#define | REENABLE_BOOLCAST_WARNING __pragma( warning( default: 4800 )) |
#define | __bool_cast_defined |
#define | dimof(x) (sizeof(x)/sizeof(x[0])) |
#define | offsetof(stype, field) ((size_t)&reinterpret_cast<char const volatile&>((((stype*)0)->field))) |
#define | structof(faddr, stype, field) ((stype*)( (ULONG_PTR)(faddr) - (ULONG_PTR)offsetof(stype,field))) |
#define | fieldsize(stype, field) sizeof(((stype*)0)->field) |
#define | struct_subsize(stype, field) (offsetof( stype,field ) + fieldsize( stype,field )) |
#define | LITERAL(type, x) (*((type*)&(x))) |
#define | UNUSED(x) ((void)&(x)) |
#define | ZEROMEM(var) memset( &(var),0,sizeof(var) ) |
#define | YIELD() SleepEx( 0, true ) |
#define | SWAP_INPLACE(a, b) a ^= b ^= a ^= b |
#define | ROUND_UP(x, chunk) (((UINT_PTR(x) + (chunk) - 1) / (chunk)) * (chunk)) |
#define | ROUND_DOWN(x, chunk) ((UINT_PTR(x) / (chunk)) * (chunk)) |
#define | ROUND_UP2(x, chunk) ((UINT_PTR(x)+((chunk)-1)) & -(chunk)) |
#define | ROUND_DOWN2(x, chunk) (UINT_PTR(x) & -(chunk)) |
#define | ALIGN_VALUE(x, aln) (INT_PTR(x) + ((~INT_PTR(x)+1) & (aln-1))) |
#define | DELETE_ARRAY(A) if (A) { delete[] (A); (A) = NULL; } |
#define | DELETE_OBJ(Obj) if (Obj) { delete (Obj); (Obj) = NULL; } |
#define | RELEASE_OBJ(Ifc) if (Ifc) { (Ifc)->Release(); (Ifc) = NULL; } |
#define | COMTASK_FREE(Obj) if (Obj) { CoTaskMemFree( PVOID(Obj) ); (Obj) = NULL; } |
#define | _BAD_HANDLE INVALID_HANDLE_VALUE |
#define | BAD_HANDLE(hnd) ((hnd) == _BAD_HANDLE || !(hnd)) |
#define | GOOD_HANDLE(hnd) ((hnd) != _BAD_HANDLE && (hnd) != NULL) |
#define | CHK_HANDLE(hnd) ((hnd) == _BAD_HANDLE ? NULL : (hnd)) |
#define | _BAD_ATTRIBUTE INVALID_FILE_ATTRIBUTES |
#define | BITS_SET(bits, x) (((x) & (bits)) == (bits)) |
#define | BITS_RST(bits, x) (((x) & (bits)) == 0) |
#define | HAVE_BITS(var, mask) BITS_SET( (mask),(var) ) |
#define | ALL_BITS 0xFFFFFFFF |
#define | ALL_BITS64 0xFFFFFFFFFFFFFFFF |
#define | _BV(bitNr) (UINT_PTR(1) << (bitNr)) |
#define | PP_NSTR(x) PP_NSTR_(x) |
#define | PP_NSTR_(x) #x |
#define | PP_STR(x) _T(#x) |
#define | CCSTR_LENGTH(s) (dimof(s) - sizeof(*s)) |
#define | CCH_AVAIL(buf, pz) (dimof(buf) - (CSTR(pz) - CSTR(buf))) |
#define | _ODS(s) OutputDebugString( s ) |
#define | _ODSA(s) OutputDebugStringA( s ) |
#define | _ODSW(s) OutputDebugStringW( s ) |
#define | _CASSERT(id, expr) typedef char __cassert_##id[ (expr) ? +1 : -1 ] |
#define | OFFSETOFCLASS(base, derived) ((DWORD)(DWORD_PTR) (STATIC_CAST(base*)( (derived*)8 )) - 8) |
Functions | |
bool __forceinline | bool_cast (BOOL B52) |
#define NT_SUCCESS | ( | Status | ) | ((NTSTATUS)(Status) >= 0) |
#define NT_ERROR | ( | Status | ) | (((ULONG)(Status) >> 30) == 3) |
#define __assembly __declspec(naked) |
#define DISABLE_BOOLCAST_WARNING __pragma( warning( disable: 4800 )) |
#define REENABLE_BOOLCAST_WARNING __pragma( warning( default: 4800 )) |
#define dimof | ( | x | ) | (sizeof(x)/sizeof(x[0])) |
#define offsetof | ( | stype, | |
field | |||
) | ((size_t)&reinterpret_cast<char const volatile&>((((stype*)0)->field))) |
#define structof | ( | faddr, | |
stype, | |||
field | |||
) | ((stype*)( (ULONG_PTR)(faddr) - (ULONG_PTR)offsetof(stype,field))) |
#define fieldsize | ( | stype, | |
field | |||
) | sizeof(((stype*)0)->field) |
#define LITERAL | ( | type, | |
x | |||
) | (*((type*)&(x))) |
#define UNUSED | ( | x | ) | ((void)&(x)) |
#define ZEROMEM | ( | var | ) | memset( &(var),0,sizeof(var) ) |
#define YIELD | ( | ) | SleepEx( 0, true ) |
#define SWAP_INPLACE | ( | a, | |
b | |||
) | a ^= b ^= a ^= b |
#define ROUND_UP | ( | x, | |
chunk | |||
) | (((UINT_PTR(x) + (chunk) - 1) / (chunk)) * (chunk)) |
Note: ROUND_UP( 0,N ), ROUND_UP2( 0,N ), and ALIGN_VALUE( 0,N ) return 0, not N!
Unsigned integers, can be odd as well.
#define ROUND_DOWN | ( | x, | |
chunk | |||
) | ((UINT_PTR(x) / (chunk)) * (chunk)) |
#define ROUND_UP2 | ( | x, | |
chunk | |||
) | ((UINT_PTR(x)+((chunk)-1)) & -(chunk)) |
#define ALIGN_VALUE | ( | x, | |
aln | |||
) | (INT_PTR(x) + ((~INT_PTR(x)+1) & (aln-1))) |
#define DELETE_ARRAY | ( | A | ) | if (A) { delete[] (A); (A) = NULL; } |
#define DELETE_OBJ | ( | Obj | ) | if (Obj) { delete (Obj); (Obj) = NULL; } |
#define RELEASE_OBJ | ( | Ifc | ) | if (Ifc) { (Ifc)->Release(); (Ifc) = NULL; } |
#define COMTASK_FREE | ( | Obj | ) | if (Obj) { CoTaskMemFree( PVOID(Obj) ); (Obj) = NULL; } |
#define _BAD_HANDLE INVALID_HANDLE_VALUE |
#define BAD_HANDLE | ( | hnd | ) | ((hnd) == _BAD_HANDLE || !(hnd)) |
Caveat Emptor: Don't use a function call like CreateFile() as an argument
to the handle-check macros GOOD_HANDLE(), BAD_HANDLE(), and CHK_HANDLE().
The argument /must/ be a variable, else the macro will fail due to calling the function twice!
#define GOOD_HANDLE | ( | hnd | ) | ((hnd) != _BAD_HANDLE && (hnd) != NULL) |
#define CHK_HANDLE | ( | hnd | ) | ((hnd) == _BAD_HANDLE ? NULL : (hnd)) |
Note: It's recommended to use CheckHandle() instead of CHK_HANDLE().
#define _BAD_ATTRIBUTE INVALID_FILE_ATTRIBUTES |
#define BITS_SET | ( | bits, | |
x | |||
) | (((x) & (bits)) == (bits)) |
#define BITS_RST | ( | bits, | |
x | |||
) | (((x) & (bits)) == 0) |
#define HAVE_BITS | ( | var, | |
mask | |||
) | BITS_SET( (mask),(var) ) |
#define ALL_BITS64 0xFFFFFFFFFFFFFFFF |
#define _BV | ( | bitNr | ) | (UINT_PTR(1) << (bitNr)) |
#define PP_NSTR | ( | x | ) | PP_NSTR_(x) |
#define CCSTR_LENGTH | ( | s | ) | (dimof(s) - sizeof(*s)) |
#define _ODS | ( | s | ) | OutputDebugString( s ) |
#define _CASSERT | ( | id, | |
expr | |||
) | typedef char __cassert_##id[ (expr) ? +1 : -1 ] |
_CASSERT checks boolean/integral conditions at compile time.
It is purely a compile-time static mechanism that generates no code.
On fail it prints something like "Error: Negative subscript".
Some compilers neatly include the __cassert_Id in the error message.
#define OFFSETOFCLASS | ( | base, | |
derived | |||
) | ((DWORD)(DWORD_PTR) (STATIC_CAST(base*)( (derived*)8 )) - 8) |
OFFSETOFCLASS is used to help C++ resolve multiple COM interface inheritance.
Example (class CSample : public IPersistFolder)..
See also QITABENT, QITABENTMULTI, which can make the table less elaborate.
[REF] ShLwApi.h : Line 2024++
bool __forceinline bool_cast | ( | BOOL | B52 | ) |
A BOOL has (2^32)-1 'true' values, and I prefer one, to stay stringent.
The cast is forced inline, and should ideally not generate extra code,
just coax the compiler to treat the BOOL as a bool without whining.
(In practice, it generates two machine code instructions.)