Coin Logo http://www.sim.no/
http://www.coin3d.org/

SbBasic.h
00001 #ifndef COIN_SBBASIC_H
00002 #define COIN_SBBASIC_H
00003 
00004 /**************************************************************************\
00005  *
00006  *  This file is part of the Coin 3D visualization library.
00007  *  Copyright (C) by Kongsberg Oil & Gas Technologies.
00008  *
00009  *  This library is free software; you can redistribute it and/or
00010  *  modify it under the terms of the GNU General Public License
00011  *  ("GPL") version 2 as published by the Free Software Foundation.
00012  *  See the file LICENSE.GPL at the root directory of this source
00013  *  distribution for additional information about the GNU GPL.
00014  *
00015  *  For using Coin with software that can not be combined with the GNU
00016  *  GPL, and for taking advantage of the additional benefits of our
00017  *  support services, please contact Kongsberg Oil & Gas Technologies
00018  *  about acquiring a Coin Professional Edition License.
00019  *
00020  *  See http://www.coin3d.org/ for more information.
00021  *
00022  *  Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
00023  *  http://www.sim.no/  sales@sim.no  coin-support@coin3d.org
00024  *
00025 \**************************************************************************/
00026 
00027 #include <Inventor/C/basic.h>
00028 #ifndef NDEBUG
00029 #include <Inventor/C/errors/debugerror.h>
00030 #endif // !NDEBUG
00031 
00032 /* ********************************************************************** */
00033 /* Trap people trying to use Inventor headers while compiling C source code.
00034  * (we get support mail about this from time to time)
00035  */
00036 #ifndef __cplusplus
00037 #error You are not compiling C++ - maybe your source file is named <file>.c
00038 #endif
00039 
00040 /* ********************************************************************** */
00041 /* Include these for Open Inventor compatibility reasons (they are not
00042  * actually used in Coin.)
00043  */
00044 #define SoEXTENDER
00045 #define SoINTERNAL
00046 
00047 /* ********************************************************************** */
00048 
00049 /* Some useful inline template functions:
00050  *   SbAbs(Val)              - returns absolute value
00051  *   SbMin(Val1, Val2)       - returns minimum value
00052  *   SbMax(Val1, Val2)       - returns maximum value
00053  *   SbClamp(Val, Min, Max)  - returns clamped value
00054  *   SbSwap(Val1, Val2)      - swaps the two values (no return value)
00055  *   SbSqr(val)              - returns (val)²
00056  */
00057 
00058 template <class Type>
00059 inline Type SbAbs( Type Val ) {
00060   return (Val < 0) ? 0 - Val : Val;
00061 }
00062 
00063 template <class Type>
00064 inline Type SbMax( const Type A, const Type B ) {
00065   return (A < B) ? B : A;
00066 }
00067 
00068 template <class Type>
00069 inline Type SbMin( const Type A, const Type B ) {
00070   return (A < B) ? A : B;
00071 }
00072 
00073 template <class Type>
00074 inline Type SbClamp( const Type Val, const Type Min, const Type Max ) {
00075   return (Val < Min) ? Min : (Val > Max) ? Max : Val;
00076 }
00077 
00078 template <class Type>
00079 inline void SbSwap( Type & A, Type & B ) {
00080   Type T; T = A; A = B; B = T;
00081 }
00082 
00083 template <class Type>
00084 inline Type SbSqr(const Type val) {
00085   return val * val;
00086 }
00087 
00088 /* *********************************************************************** */
00089 
00090 // SbDividerChk() - checks if divide-by-zero is attempted, and emits a
00091 // warning if so for debug builds.  inlined like this to not take much
00092 // screenspace in inline functions.
00093 
00094 // cc_debugerror_post() is not attempted resolved before the template is
00095 // used, hence the missing Inventor/errors/SoDebugError.h #include. This
00096 // "trick" does only work *portably* for functions in the global namespace.
00097 
00098 template <typename Type>
00099 inline void SbDividerChk(const char * funcname, Type divider) {
00100 #ifndef NDEBUG
00101   if (!(divider != static_cast<Type>(0)))
00102     cc_debugerror_post(funcname, "divide by zero error.", divider);
00103 #endif // !NDEBUG
00104 }
00105 
00106 /* ********************************************************************** */
00107 
00108 /* COMPILER BUG WORKAROUND:
00109 
00110    We've had reports that Sun CC v4.0 is (likely) buggy, and doesn't
00111    allow a statement like
00112 
00113      SoType SoNode::classTypeId = SoType::badType();
00114 
00115    As a hack we can however get around this by instead writing it as
00116 
00117      SoType SoNode::classTypeId;
00118 
00119    ..as the SoType variable will then be initialized to bitpattern
00120    0x0000, which equals SoType::badType(). We can *however* not do
00121    this for the Intel C/C++ compiler, as that does *not* init to the
00122    0x0000 bitpattern (which may be a separate bug -- I haven't read
00123    the C++ spec closely enough to decide whether that relied on
00124    unspecified behavior or not).
00125 
00126    The latest version of the Intel compiler has been tested to still
00127    have this problem, so I've decided to re-install the old code, but
00128    in this form:
00129 
00130      SoType SoNode::classTypeId STATIC_SOTYPE_INIT;
00131 
00132    ..so it is easy to revert if somebody complains that the code
00133    reversal breaks their old Sun CC 4.0 compiler -- see the #define of
00134    STATIC_SOTYPE_INIT below.
00135 
00136    If that happens, we should work with the reporter, having access to
00137    the buggy compiler, to make a configure check which sets the
00138    SUN_CC_4_0_SOTYPE_INIT_BUG #define in include/Inventor/C/basic.h.in.
00139 
00140    (Note that the Sun CC compiler has moved on, and a later version
00141    we've tested, 5.4, does not have the bug.)
00142 
00143    20050105 mortene.
00144 */
00145 
00146 #define SUN_CC_4_0_SOTYPE_INIT_BUG 0 /* assume compiler is ok for now */
00147 
00148 #if SUN_CC_4_0_SOTYPE_INIT_BUG
00149 #define STATIC_SOTYPE_INIT
00150 #else
00151 #define STATIC_SOTYPE_INIT = SoType::badType()
00152 #endif
00153 
00154 /* ********************************************************************** */
00155 
00156 #endif /* !COIN_SBBASIC_H */

Copyright © 1998-2010 by Kongsberg Oil & Gas Technologies. All rights reserved.

Generated for Coin by Doxygen 1.7.5.1.