Coin: SbBasic.h Source File

| Coin3D is Free Software, published under the BSD 3-clause license. | https://coin3d.github.io https://www.kongsberg.com/en/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 squared value
65 */
66
67template <typename Type>
68inline Type SbAbs( Type Val ) {
69 return (Val < 0) ? 0 - Val : Val;
70}
71
72template <typename Type>
73inline Type SbMax( const Type A, const Type B ) {
74 return (A < B) ? B : A;
75}
76
77template <typename Type>
78inline Type SbMin( const Type A, const Type B ) {
79 return (A < B) ? A : B;
80}
81
82template <typename Type>
83inline Type SbClamp( const Type Val, const Type Min, const Type Max ) {
84 return (Val < Min) ? Min : (Val > Max) ? Max : Val;
85}
86
87template <typename Type>
88inline void SbSwap( Type & A, Type & B ) {
89 Type T; T = A; A = B; B = T;
90}
91
92template <typename Type>
93inline 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#ifndef NDEBUG
104template <typename Type>
105inline void SbDividerChk(const char * funcname, Type divider) {
106 if (!(divider != static_cast<Type>(0)))
107 cc_debugerror_post(funcname, "divide by zero error.", divider);
108}
109#else
110template <typename Type>
111inline void SbDividerChk(const char *, Type) {}
112#endif // !NDEBUG
113
114/* ********************************************************************** */
115
116/* COMPILER BUG WORKAROUND:
117
118 We've had reports that Sun CC v4.0 is (likely) buggy, and doesn't
119 allow a statement like
120
121 SoType SoNode::classTypeId = SoType::badType();
122
123 As a hack we can however get around this by instead writing it as
124
125 SoType SoNode::classTypeId;
126
127 ..as the SoType variable will then be initialized to bitpattern
128 0x0000, which equals SoType::badType(). We can *however* not do
129 this for the Intel C/C++ compiler, as that does *not* init to the
130 0x0000 bitpattern (which may be a separate bug -- I haven't read
131 the C++ spec closely enough to decide whether that relied on
132 unspecified behavior or not).
133
134 The latest version of the Intel compiler has been tested to still
135 have this problem, so I've decided to re-install the old code, but
136 in this form:
137
138 SoType SoNode::classTypeId STATIC_SOTYPE_INIT;
139
140 ..so it is easy to revert if somebody complains that the code
141 reversal breaks their old Sun CC 4.0 compiler -- see the #define of
142 STATIC_SOTYPE_INIT below.
143
144 If that happens, we should work with the reporter, having access to
145 the buggy compiler, to make a configure check which sets the
146 SUN_CC_4_0_SOTYPE_INIT_BUG #define in include/Inventor/C/basic.h.in.
147
148 (Note that the Sun CC compiler has moved on, and a later version
149 we've tested, 5.4, does not have the bug.)
150
151 20050105 mortene.
152*/
153
154#define SUN_CC_4_0_SOTYPE_INIT_BUG 0 /* assume compiler is ok for now */
155
156#if SUN_CC_4_0_SOTYPE_INIT_BUG
157#define STATIC_SOTYPE_INIT
158#else
159#define STATIC_SOTYPE_INIT = SoType::badType()
160#endif
161
162/* ********************************************************************** */
163
164/*
165 Coin wraps many macros in do-while statements to make them usable
166 like a statement. At least the Microsoft compiler complains about
167 this construct with warning C4127: "conditional expression is constant".
168*/
169
170#ifdef _MSC_VER // Microsoft Visual C++
171#define WHILE_0 \
172 __pragma(warning(push)) \
173 __pragma(warning(disable:4127)) \
174 while (0) \
175 __pragma(warning(pop))
176#else
177#define WHILE_0 while (0)
178#endif
179
180#endif /* !COIN_SBBASIC_H */