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