Oracle® Fusion Middleware C++ API Reference for Oracle Coherence
12c (12.2.1.0.0)

E55667-01

coherence/lang/compatibility.hpp

00001 /*
00002 * compatibility.hpp
00003 *
00004 * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
00005 *
00006 * Oracle is a registered trademarks of Oracle Corporation and/or its
00007 * affiliates.
00008 *
00009 * This software is the confidential and proprietary information of Oracle
00010 * Corporation. You shall not disclose such confidential and proprietary
00011 * information and shall use it only in accordance with the terms of the
00012 * license agreement you entered into with Oracle.
00013 *
00014 * This notice may not be removed or altered.
00015 */
00016 #ifndef COH_COMPATIBILITY_HPP
00017 #define COH_COMPATIBILITY_HPP
00018 
00019 /// @cond EXCLUDE
00020 
00021 #include <typeinfo>
00022 
00023 // ----- identify operating system ------------------------------------------
00024 
00025 #if defined(_WIN32)
00026 #   define COH_OS_WINDOWS
00027 #   define COH_LIB_PREFIX ""
00028 #   define COH_LIB_SUFFIX ".dll"
00029 #   if defined(_WIN64)
00030 #      define COH_OS_WIN64
00031 #      define COH_PLATFORM Microsoft Windows x64
00032 #   else
00033 #      define COH_OS_WIN32
00034 #      define COH_PLATFORM Microsoft Windows x86
00035 #   endif
00036 #elif defined(__sun) || defined(sun)
00037 #   define COH_OS_SOLARIS // Oracle Solaris
00038 #   define COH_OS_UNIX
00039 #   define COH_LIB_PREFIX "lib"
00040 #   define COH_LIB_SUFFIX ".so"
00041 #   include <sys/types.h> // for _LP64 definition
00042 #   if defined(__sparc)
00043 #       if defined(_LP64)
00044 #           define COH_OS_SOLARIS64
00045 #           define COH_PLATFORM Oracle Solaris SPARC 64b
00046 #       else
00047 #           define COH_OS_SOLARIS32
00048 #           define COH_PLATFORM Oracle Solaris SPARC 32b
00049 #       endif
00050 #   elif defined(__x86)
00051 #       if defined(_LP64)
00052 #           define COH_OS_SOLARIS64
00053 #           define COH_PLATFORM Oracle Solaris x64
00054 #       else
00055 #           define COH_OS_SOLARIS32
00056 #           define COH_PLATFORM Oracle Solaris x86
00057 #       endif
00058 #   endif
00059 #elif defined(__linux__)
00060 #   define COH_OS_LINUX // Linux
00061 #   define COH_LIB_PREFIX "lib"
00062 #   define COH_LIB_SUFFIX ".so"
00063 #   define COH_OS_UNIX
00064 #   if defined(__x86_64__) || defined(__amd64__)
00065 #       define COH_OS_LINUX64
00066 #       define COH_PLATFORM Linux x64
00067 #   elif defined(__x86_32__) || defined(__i386__)
00068 #       define COH_OS_LINUX32
00069 #       define COH_PLATFORM Linux x86
00070 #   endif
00071 #elif defined(__APPLE__)
00072 #   define COH_OS_DARWIN // OS X
00073 #   define COH_OS_UNIX
00074 #   define COH_LIB_PREFIX "lib"
00075 #   define COH_LIB_SUFFIX ".dylib"
00076 #   if defined(__x86_64__) || defined(__amd64__)
00077 #       define COH_OS_DARWIN64
00078 #       define COH_PLATFORM Apple OS X x64
00079 #   elif defined(__x86_32__) || defined(__i386__)
00080 #       define COH_OS_DARWIN32
00081 #       define COH_PLATFORM Apple OS X x86
00082 #   endif
00083 #endif
00084 
00085 #ifndef COH_PLATFORM
00086 #   error "Coherence for C++ does not support this platform."
00087 #endif
00088 
00089 
00090 // ----- identify compiler --------------------------------------------------
00091 
00092 #if defined(_MSC_VER) && _MSC_VER >= 1600
00093 #   define COH_CC_MSVC // Microsoft Visual C/C++
00094 #   if _MSC_VER == 1900
00095 #       define COH_CC MSVC 2015 // differentiate VS 2015 build
00096 #   else
00097 #       define COH_CC MSVC
00098 #   endif
00099 #elif defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x590
00100 #   define COH_CC_SUN // Oracle Solaris Studio
00101 #   if defined (STLPORT)
00102 #       define COH_CC SunPro STLport
00103 #   else
00104 #       define COH_CC SunPro
00105 #   endif
00106 #elif defined(__GNUG__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
00107 #   define COH_CC_GNU // GNU C++
00108 #   define COH_CC GCC
00109 #else
00110 #   error "Coherence for C++ does not support this compiler or compiler version."
00111 #endif
00112 
00113 
00114 // ----- disable select warnings --------------------------------------------
00115 
00116 /**
00117 * Macro for disabling select warnings on MSVC. The warnings are automatically
00118 * disabled upon opening a coherence namespace (using the COH_OPEN_NAMESPACE
00119 * macros), and re-enabled when the namespace is exited via
00120 * COH_CLOSE_NAMESPACE.
00121 */
00122 #if defined(COH_CC_MSVC)
00123     #define COH_PRAGMA_PUSH \
00124     __pragma(warning(push)) \
00125     /* Allow multiple interface inheritance (inheritance via dominance) */ \
00126     __pragma(warning(disable : 4250)) \
00127     /* Allow non-exported DLL templates */ \
00128     __pragma(warning(disable : 4251)) \
00129     /* Exported class inheritance from non-exported class, needed for specs */ \
00130     __pragma(warning(disable : 4275)) \
00131     /* TypedHandle/Holder: multiple copy constructors */ \
00132     __pragma(warning(disable : 4521)) \
00133     /* Member/WeakHandle: assignment operators for const/non-const type */ \
00134     __pragma(warning(disable : 4522)) \
00135     /* Lack of return statements are being promoted to errors, when control \
00136        path results in throw */ \
00137     __pragma(warning(disable : 4715; disable : 4716))
00138 
00139     #define COH_PRAGMA_POP __pragma(warning(pop))
00140 #else
00141     #define COH_PRAGMA_PUSH
00142     #define COH_PRAGMA_POP
00143 #endif
00144 
00145 /**
00146 * Macro for temporarily disabling above warnings for the duration of a
00147 * single statement.  This is only needed for code which is not enclosed in a
00148 * COH_OPEN/CLOSE_NAMESPACE block.
00149 */
00150 #define COH_NO_WARN(STMT) COH_PRAGMA_PUSH STMT COH_PRAGMA_POP
00151 
00152 /**
00153 * These macros are used to indicate that a function will not return normally.
00154 *
00155 * Usage example:
00156 * @code
00157 * COH_NO_RETURN_PRE void doSomething() COH_NO_RETURN_POST
00158 *     {
00159 *     COH_NO_RETURN_STMT(doSomething2());
00160 *     }
00161 * @endcode
00162 */
00163 #if defined(COH_CC_MSVC)
00164     #define COH_NO_RETURN_PRE __declspec(noreturn)
00165     #define COH_NO_RETURN_POST
00166     #define COH_NO_RETURN_STMT(expr) expr
00167 #elif defined(COH_CC_GNU)
00168     #define COH_NO_RETURN_PRE
00169     #define COH_NO_RETURN_POST __attribute__((noreturn))
00170     #define COH_NO_RETURN_STMT(expr) expr
00171 #elif defined(COH_CC_SUN)
00172     #define COH_NO_RETURN_PRE
00173     #define COH_NO_RETURN_POST
00174     #define COH_NO_RETURN_STMT(expr)\
00175         do { expr; throw std::exception(); } while (0)
00176 #else
00177     #define COH_NO_RETURN_PRE
00178     #define COH_NO_RETURN_POST
00179     #define COH_NO_RETURN_STMT(expr) expr
00180 #endif
00181 
00182 
00183 // ----- namespace macros ---------------------------------------------------
00184 
00185 /**
00186 * Define the existence of the coherence::lang namespace
00187 */
00188 namespace coherence { namespace lang {}}
00189 
00190 #define COH_OPEN_NAMESPACE(ns) namespace ns { \
00191     COH_PRAGMA_PUSH \
00192     using namespace coherence::lang;
00193 
00194 #define COH_INNER_NAMESPACE(ns) namespace ns {
00195 
00196 #define COH_OPEN_NAMESPACE2(ns1, ns2)\
00197     COH_OPEN_NAMESPACE (ns1) COH_INNER_NAMESPACE (ns2)
00198 
00199 #define COH_OPEN_NAMESPACE3(ns1, ns2, ns3)\
00200     COH_OPEN_NAMESPACE2 (ns1, ns2) COH_INNER_NAMESPACE (ns3)
00201 
00202 #define COH_OPEN_NAMESPACE4(ns1, ns2, ns3, ns4)\
00203     COH_OPEN_NAMESPACE3 (ns1, ns2, ns3) COH_INNER_NAMESPACE (ns4)
00204 
00205 #define COH_OPEN_NAMESPACE5(ns1, ns2, ns3, ns4, ns5)\
00206     COH_OPEN_NAMESPACE4 (ns1, ns2, ns3, ns4) COH_INNER_NAMESPACE (ns5)
00207 
00208 #define COH_OPEN_NAMESPACE6(ns1, ns2, ns3, ns4, ns5, ns6)\
00209     COH_OPEN_NAMESPACE5 (ns1, ns2, ns3, ns4, ns5) COH_INNER_NAMESPACE (ns6)
00210 
00211 #define COH_OPEN_NAMESPACE7(ns1, ns2, ns3, ns4, ns5, ns6, ns7)\
00212     COH_OPEN_NAMESPACE6 (ns1, ns2, ns3, ns4, ns5, ns6) COH_INNER_NAMESPACE (ns7)
00213 
00214 #define COH_CLOSE_NAMESPACE COH_PRAGMA_POP }
00215 
00216 #define COH_CLOSE_NAMESPACE2 COH_PRAGMA_POP } }
00217 
00218 #define COH_CLOSE_NAMESPACE3 COH_PRAGMA_POP } } }
00219 
00220 #define COH_CLOSE_NAMESPACE4 COH_PRAGMA_POP } } } }
00221 
00222 #define COH_CLOSE_NAMESPACE5 COH_PRAGMA_POP } } } } }
00223 
00224 #define COH_CLOSE_NAMESPACE6 COH_PRAGMA_POP } } } } } }
00225 
00226 #define COH_CLOSE_NAMESPACE7 COH_PRAGMA_POP } } } } } } }
00227 
00228 
00229 // ----- general utility macros ---------------------------------------------
00230 
00231 /**
00232 * This macro "mangles" the specified @a Name, producing an identifier which
00233 * is unique in the current file.
00234 *
00235 * Note. This implementation of COH_UNIQUE_IDENTIFIER (as well as macros that
00236 * use it) won't work in MSVC 6.0 when -ZI is on! See Q199057.
00237 *
00238 * @param Name the name to produce a new identifier on its basis
00239 */
00240 #define COH_JOIN(X, Y) COH_DO_JOIN(X, Y)
00241 #define COH_DO_JOIN(X, Y) COH_DO_JOIN2(X, Y)
00242 #define COH_DO_JOIN2(X, Y) X##Y
00243 #ifdef COH_CC_MSVC_NET // MSVC 2002 and later
00244 #   define COH_UNIQUE_IDENTIFIER(Name) COH_JOIN(Name, __COUNTER__)
00245 #else
00246 #   define COH_UNIQUE_IDENTIFIER(Name) COH_JOIN(Name, __LINE__)
00247 #endif
00248 
00249 /**
00250 * This macro will ensure initialization of function local statics at library
00251 * load time. This should be used to force the initialization of statics
00252 * in a thread safe way.  The general form is:
00253 *
00254 * SomeType::Handle staticAccessorFunction()
00255 *     {
00256 *     static FinalHandle<SomeType> hStatic = SomeType::create();
00257 *     return hStatic;
00258 *     }
00259 * COH_STATIC_INIT(staticAccessorFunction());
00260 *
00261 * @param FUNC  The static function and parameters to call that requires
00262 *             initialization.
00263 */
00264 #ifdef COH_EAGER_INIT
00265 #   define COH_STATIC_INIT_EX(N, FUNC) \
00266     static const bool COH_UNIQUE_IDENTIFIER(coh_static_init_func##N##_) = (FUNC, true)
00267 #else
00268 // some platforms (Windows) have serious restrictions about what can be safely
00269 // performed during static initialization and so this variant of initializer
00270 // records the initializers in the order they would be loaded to allow for
00271 // execution after the library has been loaded. The execution can be triggered
00272 // by running System::loadLibrary(NULL)
00273 #    define COH_STATIC_INIT_EX(N, FUNC) \
00274          static void COH_UNIQUE_IDENTIFIER(coh_static_init_func##N##_)() {FUNC;} \
00275          static coherence::lang::coh_initializer \
00276                  COH_UNIQUE_IDENTIFIER(coh_static_init_reg##N##_) \
00277          (&COH_UNIQUE_IDENTIFIER(coh_static_init_func##N##_))
00278 #endif
00279 
00280 #define COH_STATIC_INIT(FUNC) COH_STATIC_INIT_EX(0, FUNC)
00281 
00282 COH_OPEN_NAMESPACE2(coherence,lang)
00283     template <bool x> struct STATIC_ASSERTION_FAILURE;
00284     template<> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
00285 COH_CLOSE_NAMESPACE2
00286 
00287 /**
00288 * This macro generates a compile time error message if the integral constant
00289 * expression @a B is not true. In other words, it is the compile time
00290 * equivalent of the @c assert macro. Note that if the condition is true, then
00291 * the macro will generate neither code nor data - and the macro can also be
00292 * used at either namespace, class or function scope. When used in a template,
00293 * the static assertion will be evaluated at the time the template is
00294 * instantiated; this is particularly useful for validating template
00295 * parameters.
00296 *
00297 * #COH_STATIC_ASSERT can be used at any place where a declaration can be
00298 * placed, that is at class, function or namespace scope.
00299 *
00300 * @param B an integral constant expression to check its trueness during
00301 *          translation phase
00302 */
00303 #define COH_STATIC_ASSERT(B)                               \
00304     enum { COH_UNIQUE_IDENTIFIER(coh_static_assert_enum_) =\
00305         sizeof(coherence::lang::STATIC_ASSERTION_FAILURE<(bool)(B)>) }
00306 
00307 /**
00308 * DLL import/export macros.
00309 */
00310 #if defined(COH_CC_MSVC)
00311     #ifdef COH_BUILD
00312         #define COH_EXPORT  __declspec(dllexport)
00313     #else
00314         #define COH_EXPORT  __declspec(dllimport)
00315     #endif
00316     #define COH_EXPORT_SPEC  __declspec(dllexport)
00317     #define COH_EXPORT_SPEC_MEMBER(DECL)
00318 #else
00319     #define COH_EXPORT
00320     #define COH_EXPORT_SPEC
00321     #define COH_EXPORT_SPEC_MEMBER(DECL) DECL;
00322 #endif
00323 
00324 /**
00325 * This macro will strongly encourage/force inlining of a method.
00326 */
00327 #if defined(COH_CC_MSVC)
00328     #define COH_INLINE __forceinline
00329 #elif defined(COH_CC_GNU)
00330     #define COH_INLINE __attribute__((always_inline)) inline
00331 #else
00332     #define COH_INLINE __attribute__((always_inline)) inline
00333 #endif
00334 
00335 /**
00336 * This macro expands to the name and signature of the current function.
00337 */
00338 #if defined(__GNUC__)
00339     #define COH_CURRENT_FUNCTION __PRETTY_FUNCTION__
00340 #elif defined(__FUNCSIG__)
00341     #define COH_CURRENT_FUNCTION __FUNCSIG__
00342 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
00343     #define COH_CURRENT_FUNCTION __func__
00344 #else
00345     #define COH_CURRENT_FUNCTION "(unknown function)"
00346 #endif
00347 
00348 /**
00349  * This macro controls alignment
00350  */
00351 #if defined(COH_CC_GNU)
00352     #define COH_ALIGN(B, TYPE, NAME) TYPE NAME __attribute__ ((aligned (B)))
00353 #elif defined(COH_CC_SUN)
00354     #define COH_SUNPRO_PRAGMA(S) _Pragma(S)
00355     #define COH_ALIGN(B, TYPE, NAME) COH_SUNPRO_PRAGMA(COH_SYMB_STRING(align B(NAME))) TYPE NAME
00356 #elif defined(COH_CC_MSVC)
00357     #define COH_ALIGN(B, TYPE, NAME) __declspec(align(B)) TYPE NAME
00358 #else
00359     #error "No Coherence alignment macro for this compiler"
00360 #endif
00361 
00362 /**
00363  * Macro to declare whether a function throws exceptions.
00364  */
00365 #if __cplusplus > 199711L ||\
00366     defined(_MSC_VER) && _MSC_VER >= 1900
00367 #       define COH_NOEXCEPT(expr) noexcept(expr)
00368 #else
00369 #       define COH_NOEXCEPT(expr)
00370 #endif
00371 
00372 // ----- fixed size types ---------------------------------------------------
00373 
00374 // We need to include an std lib header here in order to detect which library
00375 // is in use (__GLIBC__ may not be defined without this including). Use
00376 // <utility> as it's about the smallest of the std lib headers.
00377 #include <utility>
00378 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901 ||\
00379     defined(_POSIX_VERSION) && _POSIX_VERSION >= 200100 ||\
00380     defined(COH_OS_LINUX) &&\
00381         defined(__GLIBC__) &&\
00382         (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 1) &&\
00383         defined(__GNUC__) ||\
00384     defined(COH_OS_DARWIN) && __MACH__ && !defined(_MSL_USING_MSL_C)
00385 #       define COH_HAS_STDINT_H
00386 #endif
00387 
00388 #if defined(__GCCXML__) ||\
00389     defined(__GNUC__) ||\
00390     defined(_MSC_EXTENSIONS)
00391 #       define COH_HAS_LONG_LONG
00392 #endif
00393 
00394 #if (defined(__GLIBCPP__) || defined(__GLIBCXX__)) &&\
00395         !defined(_GLIBCPP_USE_LONG_LONG) &&          \
00396         !defined(_GLIBCXX_USE_LONG_LONG) &&          \
00397         defined(COH_HAS_LONG_LONG)
00398     // Coming here indicates that the GNU compiler supports long long type,
00399     // but the GNU C++ runtime library does not. Particularly, no global
00400     // operator<<(std::ostream&, long long) implementation is provided. Let us
00401     // provide it, at least with minimum incomplete functionality.
00402 #   include <ostream>
00403     namespace std
00404         {
00405         template<class E, class T>
00406         inline basic_ostream<E, T>& operator<<
00407                 (basic_ostream<E, T>& out, long long l)
00408             {
00409             return out << static_cast<long>(l);
00410             }
00411         template<class E, class T>
00412         inline basic_ostream<E, T>& operator<<
00413                 (basic_ostream<E, T>& out, unsigned long long l)
00414             {
00415             return out << static_cast<unsigned long>(l);
00416             }
00417         }
00418 #   undef COH_HAS_LONG_LONG
00419 #endif
00420 
00421 #if !defined(COH_HAS_LONG_LONG) && !defined(COH_CC_MSVC)
00422 #   include <limits.h>
00423 #   if defined(ULLONG_MAX) || defined(ULONG_LONG_MAX) ||\
00424             defined(ULONGLONG_MAX)
00425 #       define COH_HAS_LONG_LONG
00426 #   endif
00427 #endif
00428 
00429 #if defined(_MSC_VER)
00430 #       define COH_HAS_MS_INT64
00431 #endif
00432 
00433 /**
00434 * Fixed width primitive types.
00435 */
00436 #if defined(COH_HAS_STDINT_H)
00437 #   include <stdint.h>
00438 #elif defined(COH_CC_SUN)
00439 #   include <inttypes.h>
00440 #else
00441 //  This platform does not support the C99 stdint.h types. This code block
00442 //  defines them in the global namespace, alternatively you may define
00443 //  COH_NAMESPACED_FIXED_INTS, in which case these definitions will be within
00444 //  the coherence::lang namespace.
00445 #   include <climits>
00446 #   ifdef COH_NAMESPACED_FIXED_INTS
00447       COH_OPEN_NAMESPACE2(coherence,lang)
00448 #   endif
00449     COH_STATIC_ASSERT(UCHAR_MAX == 0xFF);
00450     typedef signed char   int8_t;
00451     typedef unsigned char uint8_t;
00452     COH_STATIC_ASSERT(USHRT_MAX == 0xFFFF);
00453     typedef short          int16_t;
00454     typedef unsigned short uint16_t;
00455 #   if UINT_MAX == 0xFFFFFFFF
00456         typedef int          int32_t;
00457         typedef unsigned int uint32_t;
00458 #   elif ULONG_MAX == 0xFFFFFFFF
00459         typedef long          int32_t;
00460         typedef unsigned long uint32_t;
00461 #   else
00462 #       error int size not correct
00463 #   endif
00464 #   if defined(COH_HAS_LONG_LONG) &&\
00465         !defined(COH_CC_MSVC) &&\
00466         (!defined(__GLIBCPP__) || defined(_GLIBCPP_USE_LONG_LONG)) && \
00467         (defined(ULLONG_MAX) ||\
00468             defined(ULONG_LONG_MAX) ||\
00469             defined(ULONGLONG_MAX))
00470 #               if defined(ULLONG_MAX)
00471                     COH_STATIC_ASSERT(ULLONG_MAX == 0xFFFFFFFFFFFFFFFFULL);
00472 #               elif defined(ULONG_LONG_MAX)
00473                     COH_STATIC_ASSERT
00474                         (ULONG_LONG_MAX == 0xFFFFFFFFFFFFFFFFULL);
00475 #               elif defined(ULONGLONG_MAX)
00476                     COH_STATIC_ASSERT
00477                         (ULONGLONG_MAX == 0xFFFFFFFFFFFFFFFFULL));
00478 #               else
00479 #                   error long long size not correct
00480 #               endif
00481                 typedef long long          int64_t;
00482                 typedef unsigned long long uint64_t;
00483 #   elif ULONG_MAX != 0xFFFFFFFF
00484         COH_STATIC_ASSERT(ULONG_MAX == 0xFFFFFFFFFFFFFFFFULL);
00485         typedef long          int64_t;
00486         typedef unsigned long uint64_t;
00487 #   elif defined(__GNUC__) && defined(COH_HAS_LONG_LONG)
00488         __extension__ typedef long long          int64_t;
00489         __extension__ typedef unsigned long long uint64_t;
00490 #   elif defined(COH_HAS_MS_INT64)
00491         typedef __int64          int64_t;
00492         typedef unsigned __int64 uint64_t;
00493 #   else
00494 #       error no 64-bit integer support
00495 #   endif
00496 #   ifdef COH_NAMESPACED_FIXED_INTS
00497         COH_CLOSE_NAMESPACE2
00498 #   endif
00499 #endif
00500 
00501 /**
00502 * Non-standard primitive type definitions.
00503 */
00504 COH_OPEN_NAMESPACE2(coherence,lang)
00505     typedef unsigned char octet_t;
00506     typedef uint16_t      wchar16_t;
00507     typedef uint32_t      size32_t;
00508     typedef uint64_t      size64_t;
00509     typedef float         float32_t; COH_STATIC_ASSERT(sizeof(float32_t) >= sizeof(int32_t));
00510     typedef double        float64_t; COH_STATIC_ASSERT(sizeof(float64_t) >= sizeof(int64_t));
00511 COH_CLOSE_NAMESPACE2
00512 
00513 /**
00514 * Produce a 64b value from two 32b parts.
00515 *
00516 * This is a bit-wise construction, and the supplied values should be the
00517 * bitwise unsigned representations.  For example:
00518 * COH_INT64(0x7FFFFFFFU, 0xFFFFFFFFU) == 0x7FFFFFFFFFFFFFFFLL
00519 */
00520 #define COH_INT64(HIGH, LOW) int64_t(uint64_t(HIGH) << 32 | uint64_t(LOW))
00521 
00522 // macros for turning compiler supplied define into a string
00523 #define COH_SYMB_STRING(SYMB) #SYMB
00524 #define COH_SYMB_TO_STRING(SYMB) COH_SYMB_STRING(SYMB)
00525 
00526 
00527 // ----- helpers ------------------------------------------------------------
00528 
00529 #include <typeinfo>
00530 COH_OPEN_NAMESPACE2(coherence,lang)
00531     class Class;
00532 
00533     typedef void (*coh_static_initializer)();
00534 
00535     /**
00536     * Register an initialization function with the runtime.
00537     */
00538     extern COH_EXPORT void coh_register_initializer(
00539             coh_static_initializer pInit, bool fAdd);
00540 
00541     /**
00542      * Helper class for registering and unregistering static initializers.
00543      */
00544     class coh_initializer
00545         {
00546         public:
00547 
00548         coh_initializer(coh_static_initializer pInit)
00549             : m_pInit(pInit)
00550             {
00551             coh_register_initializer(pInit, true);
00552             }
00553 
00554         ~coh_initializer()
00555             {
00556             coh_register_initializer(m_pInit, false);
00557             }
00558 
00559         private:
00560         coh_static_initializer m_pInit;
00561         };
00562 
00563     /**
00564     * Helper functions for throwing exceptions.
00565     */
00566     extern COH_EXPORT void coh_throw_npe(const std::type_info&);
00567     extern COH_EXPORT void coh_throw_class_cast(const std::type_info&,
00568             const std::type_info&);
00569     extern COH_EXPORT void coh_throw_const_cast(const std::type_info&,
00570             const std::type_info&);
00571     extern COH_EXPORT void coh_throw_illegal_state(const char* achMsg);
00572     extern COH_EXPORT void coh_throw_illegal_argument(const char* achMsg);
00573     extern COH_EXPORT void coh_throw_unsupported_operation(const char* achMsg);
00574     extern COH_EXPORT const Class* coh_loadClassByType(const std::type_info& ti);
00575 
00576     /**
00577     * Helper class used to test for type assignment compatibility at
00578     * compile time.
00579     *
00580     * This implementation is based on the Conversion example from
00581     * Andrei Alexandrescu's Modern C++ Design.
00582     */
00583     template<class A, class B>
00584     class assignment
00585         {
00586         protected:
00587             typedef char  PathA;
00588             class   Other {char unused[2];}; // sizeof(Other) != sizeof(PathA)
00589             static  PathA route(A*); // PathA taken only for compatible types
00590             static  Other route(...); // incompatible types go this route
00591             static  B*    test(); // "generate" a test object
00592 
00593         public:
00594             /**
00595             * Convert a derived A to an A.
00596             */
00597             static A* safe(A* a)
00598                 {
00599                 return a;
00600                 }
00601 
00602             /**
00603             * Dummy conversion (should never be called)
00604             */
00605             static A* safe(...)
00606                 {
00607                 coh_throw_illegal_state("unsafe cast");
00608                 return 0;
00609                 }
00610 
00611         public:
00612             /**
00613             * True iff A = B is allowed.
00614             */
00615             enum {allowed = (sizeof(route(test())) == sizeof(PathA))};
00616         };
00617 
00618     /**
00619     * Helper class used to test if a type has been declared as const.
00620     */
00621     template<class A>
00622     class constness
00623         {
00624         public:
00625             enum {applied = assignment<A, const A>::allowed};
00626         };
00627 /**
00628  * Coherence Class and Interface ID helpers.
00629  */
00630 #if defined(COH_CC_MSVC)
00631     // MSVC utilizes identical code folding ICF, which makes the alternate
00632     // very cheap form of id generation unusable and effectively assigns
00633     // all classes the same id, thus on MSVC we fallback on standard and
00634     // slower typeid
00635 
00636     /**
00637      * Coherence class id type.
00638      */
00639     typedef const std::type_info& coh_class_id;
00640 
00641     /**
00642      * Return the class id for a given managed class.
00643      */
00644     #define COH_CLASS_ID(CLASS) CLASS::_classId()
00645 
00646     /**
00647      * @internal
00648      *
00649      * Hook for COH_CLASS_ID macro, not for direct use
00650      */
00651     #define COH_GENERATE_CLASS_ID(T) \
00652         static coh_class_id _classId() \
00653             { \
00654             return typeid(T); \
00655             }
00656 #else
00657     // For most compilers we can use the address of a class specific static
00658     // function as a unique class identifier. This is cheaper then using
00659     // the standard typeid operator, which on some platforms adds contention
00660 
00661     /**
00662      * Coherence class id type.
00663      */
00664     typedef void (*coh_class_id)();
00665 
00666     /**
00667      * Return the class id for a given managed class.
00668      */
00669     #define COH_CLASS_ID(CLASS) &CLASS::_classId
00670 
00671     /**
00672      * @internal
00673      *
00674      * Hook for COH_CLASS_ID macro, not for direct use
00675      */
00676     #define COH_GENERATE_CLASS_ID(T) static void _classId() {}
00677 #endif
00678 
00679 COH_CLOSE_NAMESPACE2
00680 
00681 /// @endcond
00682 
00683 #endif // COH_COMPATIBILITY_HPP
Copyright © 2000, 2015, Oracle and/or its affiliates. All rights reserved.