Coin Logo Coin3D is Free Software,
published under the BSD 3-clause license.
https://bitbucket.org/Coin3D/
http://www.kongsberg.com/kogt/
SbBasic.h
1 #ifndef COIN_SBBASIC_H
2 #define COIN_SBBASIC_H
3 
4 /**************************************************************************\
5  * Copyright (c) Kongsberg Oil & Gas Technologies AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from
21  * this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 \**************************************************************************/
35 
36 #include <Inventor/C/basic.h>
37 #ifndef NDEBUG
38 #include <Inventor/C/errors/debugerror.h>
39 #endif // !NDEBUG
40 
41 /* ********************************************************************** */
42 /* Trap people trying to use Inventor headers while compiling C source code.
43  * (we get support mail about this from time to time)
44  */
45 #ifndef __cplusplus
46 #error You are not compiling C++ - maybe your source file is named <file>.c
47 #endif
48 
49 /* ********************************************************************** */
50 /* Include these for Open Inventor compatibility reasons (they are not
51  * actually used in Coin.)
52  */
53 #define SoEXTENDER
54 #define SoINTERNAL
55 
56 /* ********************************************************************** */
57 
58 /* Some useful inline template functions:
59  * SbAbs(Val) - returns absolute value
60  * SbMin(Val1, Val2) - returns minimum value
61  * SbMax(Val1, Val2) - returns maximum value
62  * SbClamp(Val, Min, Max) - returns clamped value
63  * SbSwap(Val1, Val2) - swaps the two values (no return value)
64  * SbSqr(val) - returns (val)²
65  */
66 
67 template <class Type>
68 inline Type SbAbs( Type Val ) {
69  return (Val < 0) ? 0 - Val : Val;
70 }
71 
72 template <class Type>
73 inline Type SbMax( const Type A, const Type B ) {
74  return (A < B) ? B : A;
75 }
76 
77 template <class Type>
78 inline Type SbMin( const Type A, const Type B ) {
79  return (A < B) ? A : B;
80 }
81 
82 template <class Type>
83 inline Type SbClamp( const Type Val, const Type Min, const Type Max ) {
84  return (Val < Min) ? Min : (Val > Max) ? Max : Val;
85 }
86 
87 template <class Type>
88 inline void SbSwap( Type & A, Type & B ) {
89  Type T; T = A; A = B; B = T;
90 }
91 
92 template <class Type>
93 inline Type SbSqr(const Type val) {
94  return val * val;
95 }
96 
97 /* *********************************************************************** */
98 
99 // SbDividerChk() - checks if divide-by-zero is attempted, and emits a
100 // warning if so for debug builds. inlined like this to not take much
101 // screenspace in inline functions.
102 
103 // Missing include for cc_debugerror_post() added here. The previous "trick"
104 // for not needing to resolve symbols in global namespace no longer works
105 // with newer compilers.
106 #ifndef NDEBUG
107 #include <Inventor/C/errors/debugerror.h>
108 #endif // !NDEBUG
109 
110 
111 #ifndef NDEBUG
112 template <typename Type>
113 inline void SbDividerChk(const char * funcname, Type divider) {
114  if (!(divider != static_cast<Type>(0)))
115  cc_debugerror_post(funcname, "divide by zero error.", divider);
116 }
117 #else
118 template <typename Type>
119 inline void SbDividerChk(const char *, Type) {}
120 #endif // !NDEBUG
121 
122 /* ********************************************************************** */
123 
124 /* COMPILER BUG WORKAROUND:
125 
126  We've had reports that Sun CC v4.0 is (likely) buggy, and doesn't
127  allow a statement like
128 
129  SoType SoNode::classTypeId = SoType::badType();
130 
131  As a hack we can however get around this by instead writing it as
132 
133  SoType SoNode::classTypeId;
134 
135  ..as the SoType variable will then be initialized to bitpattern
136  0x0000, which equals SoType::badType(). We can *however* not do
137  this for the Intel C/C++ compiler, as that does *not* init to the
138  0x0000 bitpattern (which may be a separate bug -- I haven't read
139  the C++ spec closely enough to decide whether that relied on
140  unspecified behavior or not).
141 
142  The latest version of the Intel compiler has been tested to still
143  have this problem, so I've decided to re-install the old code, but
144  in this form:
145 
146  SoType SoNode::classTypeId STATIC_SOTYPE_INIT;
147 
148  ..so it is easy to revert if somebody complains that the code
149  reversal breaks their old Sun CC 4.0 compiler -- see the #define of
150  STATIC_SOTYPE_INIT below.
151 
152  If that happens, we should work with the reporter, having access to
153  the buggy compiler, to make a configure check which sets the
154  SUN_CC_4_0_SOTYPE_INIT_BUG #define in include/Inventor/C/basic.h.in.
155 
156  (Note that the Sun CC compiler has moved on, and a later version
157  we've tested, 5.4, does not have the bug.)
158 
159  20050105 mortene.
160 */
161 
162 #define SUN_CC_4_0_SOTYPE_INIT_BUG 0 /* assume compiler is ok for now */
163 
164 #if SUN_CC_4_0_SOTYPE_INIT_BUG
165 #define STATIC_SOTYPE_INIT
166 #else
167 #define STATIC_SOTYPE_INIT = SoType::badType()
168 #endif
169 
170 /* ********************************************************************** */
171 
172 /*
173  Coin wraps many macros in do-while statements to make them usable
174  like a statement. At least the Microsoft compiler complains about
175  this construct with warning C4127: "conditional expression is constant".
176 */
177 
178 #ifdef _MSC_VER // Microsoft Visual C++
179 #define WHILE_0 \
180  __pragma(warning(push)) \
181  __pragma(warning(disable:4127)) \
182  while (0) \
183  __pragma(warning(pop))
184 #else
185 #define WHILE_0 while (0)
186 #endif
187 
188 #endif /* !COIN_SBBASIC_H */