WxExtLib - safecast_signedness.h
00001: // -*- c++ -*-
00002:
00003: //-------------------------------------------------------------------------
00004: // Author: Daniel Käps, created 2006-01-26.
00005: // This file is public domain.
00006: //-------------------------------------------------------------------------
00007:
00008: // no guards:
00009: // #ifndef _INCLUDED_safecast_signedness_h
00010: // #define _INCLUDED_safecast_signedness_h
00011:
00012: //-------------------------------------------------------------------------
00013: //
00014: // signed/unsigned/default-signed-ness conversions:
00015: // - those are less problematic compared to type-size changes, since,
00016: // at least, no data is lost by the cast operation
00017: // - however, wrong interpretation may cause incorrect behavior, still
00018: //
00019: // some cast difficulties with regard to signed-ness conversions:
00020: // - some compilers may interpret 'signed type' and '(unspecified
00021: // signedness) type' differently with regard to overload resolution
00022: // - the type of '(unspecified signedness) char' may differ from compiler
00023: // to compiler and may additionally depend on compiler options
00024: //
00025: //-------------------------------------------------------------------------
00026:
00027: // used defines:
00028: // #define M_Type ... // e.g. char, char *, int etc.
00029: // #define M_SafecastSignedness_IsAddConstOverloads 0/1
00030: // #define M_SafecastSignedness_IsUseDefaultSignedness 0/1
00031:
00032: //-------------------------------------------------------------------------
00033:
00034: // declared functions will be:
00035: // cast_signed()
00036: // cast_unsigned()
00037: // cast_defsigned()
00038: // additionally with const overloads if
00039: // M_SafecastSignedness_IsAddConstOverloads defined to 1
00040:
00041: //=========================================================================
00042: // functions to return M_Type as unsigned type
00043:
00044: inline unsigned M_Type cast_unsigned (signed M_Type TypeVar)
00045: {
00046: return (unsigned M_Type) TypeVar;
00047: }
00048:
00049: #if (M_SafecastSignedness_IsUseDefaultSignedness == 1)
00050: inline unsigned M_Type cast_unsigned (M_Type TypeVar)
00051: {
00052: return (unsigned M_Type) TypeVar;
00053: }
00054: #endif
00055:
00056: inline unsigned M_Type cast_unsigned (unsigned M_Type TypeVar)
00057: {
00058: return TypeVar;
00059: }
00060:
00061: //-------------------------------------------------------------------------
00062:
00063: #if (M_SafecastSignedness_IsAddConstOverloads == 1)
00064:
00065: inline const unsigned M_Type cast_unsigned (const signed M_Type TypeVar)
00066: {
00067: return (unsigned M_Type) TypeVar;
00068: }
00069:
00070: #if (M_SafecastSignedness_IsUseDefaultSignedness == 1)
00071: inline const unsigned M_Type cast_unsigned (const M_Type TypeVar)
00072: {
00073: return (unsigned M_Type) TypeVar;
00074: }
00075: #endif
00076:
00077: inline const unsigned M_Type cast_unsigned (const unsigned M_Type TypeVar)
00078: {
00079: return TypeVar;
00080: }
00081:
00082: #endif
00083:
00084: //=========================================================================
00085: // functions to return M_Type as signed type
00086:
00087: inline signed M_Type cast_signed (signed M_Type TypeVar)
00088: {
00089: return TypeVar;
00090: }
00091:
00092: #if (M_SafecastSignedness_IsUseDefaultSignedness == 1)
00093: inline signed M_Type cast_signed (M_Type TypeVar)
00094: {
00095: return (signed M_Type) TypeVar;
00096: }
00097: #endif
00098:
00099: inline signed M_Type cast_signed (unsigned M_Type TypeVar)
00100: {
00101: return (signed M_Type) TypeVar;
00102: }
00103:
00104: //-------------------------------------------------------------------------
00105:
00106: #if (M_SafecastSignedness_IsAddConstOverloads == 1)
00107:
00108: inline const signed M_Type cast_signed (const signed M_Type TypeVar)
00109: {
00110: return TypeVar;
00111: }
00112:
00113: #if (M_SafecastSignedness_IsUseDefaultSignedness == 1)
00114: inline const signed M_Type cast_signed (const M_Type TypeVar)
00115: {
00116: return (signed M_Type) TypeVar;
00117: }
00118: #endif
00119:
00120: inline const signed M_Type cast_signed (const unsigned M_Type TypeVar)
00121: {
00122: return (signed M_Type) TypeVar;
00123: }
00124:
00125: #endif
00126:
00127: //=========================================================================
00128: // functions to return M_Type with default-signedness (i.e.
00129: // no signedness declarator ('signed' or 'unsigned') is used)
00130:
00131: inline M_Type cast_defsigned (signed M_Type TypeVar)
00132: {
00133: return (M_Type) TypeVar;
00134: }
00135:
00136: #if (M_SafecastSignedness_IsUseDefaultSignedness == 1)
00137: inline M_Type cast_defsigned (M_Type TypeVar)
00138: {
00139: return TypeVar;
00140: }
00141: #endif
00142:
00143: inline M_Type cast_defsigned (unsigned M_Type TypeVar)
00144: {
00145: return (M_Type) TypeVar;
00146: }
00147:
00148: //-------------------------------------------------------------------------
00149:
00150: #if (M_SafecastSignedness_IsAddConstOverloads == 1)
00151:
00152: inline const M_Type cast_defsigned (const signed M_Type TypeVar)
00153: {
00154: return (M_Type) TypeVar;
00155: }
00156:
00157: #if (M_SafecastSignedness_IsUseDefaultSignedness == 1)
00158: inline const M_Type cast_defsigned (const M_Type TypeVar)
00159: {
00160: return TypeVar;
00161: }
00162: #endif
00163:
00164: inline const M_Type cast_defsigned (const unsigned M_Type TypeVar)
00165: {
00166: return (M_Type) TypeVar;
00167: }
00168:
00169: #endif
00170:
00171: //-------------------------------------------------------------------------
00172:
00173: // #endif
00174: