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 
38 /* ********************************************************************** */
39 /* Trap people trying to use Inventor headers while compiling C source code.
40  * (we get support mail about this from time to time)
41  */
42 #ifndef __cplusplus
43 #error You are not compiling C++ - maybe your source file is named <file>.c
44 #endif
45 
46 /* ********************************************************************** */
47 /* Include these for Open Inventor compatibility reasons (they are not
48  * actually used in Coin.)
49  */
50 #define SoEXTENDER
51 #define SoINTERNAL
52 
53 /* ********************************************************************** */
54 
55 /* Some useful inline template functions:
56  *   SbAbs(Val)              - returns absolute value
57  *   SbMin(Val1, Val2)       - returns minimum value
58  *   SbMax(Val1, Val2)       - returns maximum value
59  *   SbClamp(Val, Min, Max)  - returns clamped value
60  *   SbSwap(Val1, Val2)      - swaps the two values (no return value)
61  *   SbSqr(val)              - returns (val)�
62  */
63 
64 template <class Type>
SbAbs(Type Val)65 inline Type SbAbs( Type Val ) {
66   return (Val < 0) ? 0 - Val : Val;
67 }
68 
69 template <class Type>
SbMax(const Type A,const Type B)70 inline Type SbMax( const Type A, const Type B ) {
71   return (A < B) ? B : A;
72 }
73 
74 template <class Type>
SbMin(const Type A,const Type B)75 inline Type SbMin( const Type A, const Type B ) {
76   return (A < B) ? A : B;
77 }
78 
79 template <class Type>
SbClamp(const Type Val,const Type Min,const Type Max)80 inline Type SbClamp( const Type Val, const Type Min, const Type Max ) {
81   return (Val < Min) ? Min : (Val > Max) ? Max : Val;
82 }
83 
84 template <class Type>
SbSwap(Type & A,Type & B)85 inline void SbSwap( Type & A, Type & B ) {
86   Type T; T = A; A = B; B = T;
87 }
88 
89 template <class Type>
SbSqr(const Type val)90 inline Type SbSqr(const Type val) {
91   return val * val;
92 }
93 
94 /* *********************************************************************** */
95 
96 // SbDividerChk() - checks if divide-by-zero is attempted, and emits a
97 // warning if so for debug builds.  inlined like this to not take much
98 // screenspace in inline functions.
99 
100 // Missing include for cc_debugerror_post() added here. The previous "trick"
101 // for not needing to resolve symbols in global namespace no longer works
102 // with newer compilers.
103 #ifndef NDEBUG
104 #include <Inventor/C/errors/debugerror.h>
105 #endif // !NDEBUG
106 
107 
108 #ifndef NDEBUG
109 template <typename Type>
SbDividerChk(const char * funcname,Type divider)110 inline void SbDividerChk(const char * funcname, Type divider) {
111   if (!(divider != static_cast<Type>(0)))
112     cc_debugerror_post(funcname, "divide by zero error.", divider);
113 }
114 #else
115 template <typename Type>
SbDividerChk(const char *,Type)116 inline void SbDividerChk(const char *, Type) {}
117 #endif // !NDEBUG
118 
119 /* ********************************************************************** */
120 
121 /* COMPILER BUG WORKAROUND:
122 
123    We've had reports that Sun CC v4.0 is (likely) buggy, and doesn't
124    allow a statement like
125 
126      SoType SoNode::classTypeId = SoType::badType();
127 
128    As a hack we can however get around this by instead writing it as
129 
130      SoType SoNode::classTypeId;
131 
132    ..as the SoType variable will then be initialized to bitpattern
133    0x0000, which equals SoType::badType(). We can *however* not do
134    this for the Intel C/C++ compiler, as that does *not* init to the
135    0x0000 bitpattern (which may be a separate bug -- I haven't read
136    the C++ spec closely enough to decide whether that relied on
137    unspecified behavior or not).
138 
139    The latest version of the Intel compiler has been tested to still
140    have this problem, so I've decided to re-install the old code, but
141    in this form:
142 
143      SoType SoNode::classTypeId STATIC_SOTYPE_INIT;
144 
145    ..so it is easy to revert if somebody complains that the code
146    reversal breaks their old Sun CC 4.0 compiler -- see the #define of
147    STATIC_SOTYPE_INIT below.
148 
149    If that happens, we should work with the reporter, having access to
150    the buggy compiler, to make a configure check which sets the
151    SUN_CC_4_0_SOTYPE_INIT_BUG #define in include/Inventor/C/basic.h.in.
152 
153    (Note that the Sun CC compiler has moved on, and a later version
154    we've tested, 5.4, does not have the bug.)
155 
156    20050105 mortene.
157 */
158 
159 #define SUN_CC_4_0_SOTYPE_INIT_BUG 0 /* assume compiler is ok for now */
160 
161 #if SUN_CC_4_0_SOTYPE_INIT_BUG
162 #define STATIC_SOTYPE_INIT
163 #else
164 #define STATIC_SOTYPE_INIT = SoType::badType()
165 #endif
166 
167 /* ********************************************************************** */
168 
169 /*
170 	Coin wraps many macros in do-while statements to make them usable
171 	like a statement. At least the Microsoft compiler complains about
172 	this construct with warning C4127: "conditional expression is constant".
173 */
174 
175 #ifdef _MSC_VER // Microsoft Visual C++
176 #define WHILE_0 \
177 	__pragma(warning(push)) \
178 	__pragma(warning(disable:4127)) \
179 		while (0) \
180 	__pragma(warning(pop))
181 #else
182 #define WHILE_0 while (0)
183 #endif
184 
185 #endif /* !COIN_SBBASIC_H */
186