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

SbBasic.h
1 #ifndef COIN_SBBASIC_H
2 #define COIN_SBBASIC_H
3 
4 /**************************************************************************\
5  *
6  * This file is part of the Coin 3D visualization library.
7  * Copyright (C) by Kongsberg Oil & Gas Technologies.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * ("GPL") version 2 as published by the Free Software Foundation.
12  * See the file LICENSE.GPL at the root directory of this source
13  * distribution for additional information about the GNU GPL.
14  *
15  * For using Coin with software that can not be combined with the GNU
16  * GPL, and for taking advantage of the additional benefits of our
17  * support services, please contact Kongsberg Oil & Gas Technologies
18  * about acquiring a Coin Professional Edition License.
19  *
20  * See http://www.coin3d.org/ for more information.
21  *
22  * Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
23  * http://www.sim.no/ sales@sim.no coin-support@coin3d.org
24  *
25 \**************************************************************************/
26 
27 #include <Inventor/C/basic.h>
28 
29 /* ********************************************************************** */
30 /* Trap people trying to use Inventor headers while compiling C source code.
31  * (we get support mail about this from time to time)
32  */
33 #ifndef __cplusplus
34 #error You are not compiling C++ - maybe your source file is named <file>.c
35 #endif
36 
37 /* ********************************************************************** */
38 /* Include these for Open Inventor compatibility reasons (they are not
39  * actually used in Coin.)
40  */
41 #define SoEXTENDER
42 #define SoINTERNAL
43 
44 /* ********************************************************************** */
45 
46 /* Some useful inline template functions:
47  * SbAbs(Val) - returns absolute value
48  * SbMin(Val1, Val2) - returns minimum value
49  * SbMax(Val1, Val2) - returns maximum value
50  * SbClamp(Val, Min, Max) - returns clamped value
51  * SbSwap(Val1, Val2) - swaps the two values (no return value)
52  * SbSqr(val) - returns (val)²
53  */
54 
55 template <class Type>
56 inline Type SbAbs( Type Val ) {
57  return (Val < 0) ? 0 - Val : Val;
58 }
59 
60 template <class Type>
61 inline Type SbMax( const Type A, const Type B ) {
62  return (A < B) ? B : A;
63 }
64 
65 template <class Type>
66 inline Type SbMin( const Type A, const Type B ) {
67  return (A < B) ? A : B;
68 }
69 
70 template <class Type>
71 inline Type SbClamp( const Type Val, const Type Min, const Type Max ) {
72  return (Val < Min) ? Min : (Val > Max) ? Max : Val;
73 }
74 
75 template <class Type>
76 inline void SbSwap( Type & A, Type & B ) {
77  Type T; T = A; A = B; B = T;
78 }
79 
80 template <class Type>
81 inline Type SbSqr(const Type val) {
82  return val * val;
83 }
84 
85 /* *********************************************************************** */
86 
87 // SbDividerChk() - checks if divide-by-zero is attempted, and emits a
88 // warning if so for debug builds. inlined like this to not take much
89 // screenspace in inline functions.
90 
91 // cc_debugerror_post() is not attempted resolved before the template is
92 // used, hence the missing Inventor/errors/SoDebugError.h #include. This
93 // "trick" does only work *portably* for functions in the global namespace.
94 
95 template <typename Type>
96 inline void SbDividerChk(const char * funcname, Type divider) {
97 #ifndef NDEBUG
98  if (!(divider != static_cast<Type>(0)))
99  cc_debugerror_post(funcname, "divide by zero error.", divider);
100 #endif // !NDEBUG
101 }
102 
103 /* ********************************************************************** */
104 
105 /* COMPILER BUG WORKAROUND:
106 
107  We've had reports that Sun CC v4.0 is (likely) buggy, and doesn't
108  allow a statement like
109 
110  SoType SoNode::classTypeId = SoType::badType();
111 
112  As a hack we can however get around this by instead writing it as
113 
114  SoType SoNode::classTypeId;
115 
116  ..as the SoType variable will then be initialized to bitpattern
117  0x0000, which equals SoType::badType(). We can *however* not do
118  this for the Intel C/C++ compiler, as that does *not* init to the
119  0x0000 bitpattern (which may be a separate bug -- I haven't read
120  the C++ spec closely enough to decide whether that relied on
121  unspecified behavior or not).
122 
123  The latest version of the Intel compiler has been tested to still
124  have this problem, so I've decided to re-install the old code, but
125  in this form:
126 
127  SoType SoNode::classTypeId STATIC_SOTYPE_INIT;
128 
129  ..so it is easy to revert if somebody complains that the code
130  reversal breaks their old Sun CC 4.0 compiler -- see the #define of
131  STATIC_SOTYPE_INIT below.
132 
133  If that happens, we should work with the reporter, having access to
134  the buggy compiler, to make a configure check which sets the
135  SUN_CC_4_0_SOTYPE_INIT_BUG #define in include/Inventor/C/basic.h.in.
136 
137  (Note that the Sun CC compiler has moved on, and a later version
138  we've tested, 5.4, does not have the bug.)
139 
140  20050105 mortene.
141 */
142 
143 #define SUN_CC_4_0_SOTYPE_INIT_BUG 0 /* assume compiler is ok for now */
144 
145 #if SUN_CC_4_0_SOTYPE_INIT_BUG
146 #define STATIC_SOTYPE_INIT
147 #else
148 #define STATIC_SOTYPE_INIT = SoType::badType()
149 #endif
150 
151 /* ********************************************************************** */
152 
153 #endif /* !COIN_SBBASIC_H */

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

Generated on Tue Dec 4 2012 01:50:14 for Coin by Doxygen 1.8.2.