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