Coin Logo http://www.sim.no/
http://www.coin3d.org/

SbBasic.h
1 #ifndef COIN_SBBASIC_H
2 #define COIN_SBBASIC_H
3 
4 /**************************************************************************\
5  *
6  * This file is part of the Coin 3D visualization library.
7  * Copyright (C) by Kongsberg Oil & Gas Technologies.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * ("GPL") version 2 as published by the Free Software Foundation.
12  * See the file LICENSE.GPL at the root directory of this source
13  * distribution for additional information about the GNU GPL.
14  *
15  * For using Coin with software that can not be combined with the GNU
16  * GPL, and for taking advantage of the additional benefits of our
17  * support services, please contact Kongsberg Oil & Gas Technologies
18  * about acquiring a Coin Professional Edition License.
19  *
20  * See http://www.coin3d.org/ for more information.
21  *
22  * Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
23  * http://www.sim.no/ sales@sim.no coin-support@coin3d.org
24  *
25 \**************************************************************************/
26 
27 #include <Inventor/C/basic.h>
28 #ifndef NDEBUG
29 #include <Inventor/C/errors/debugerror.h>
30 #endif // !NDEBUG
31 
32 /* ********************************************************************** */
33 /* Trap people trying to use Inventor headers while compiling C source code.
34  * (we get support mail about this from time to time)
35  */
36 #ifndef __cplusplus
37 #error You are not compiling C++ - maybe your source file is named <file>.c
38 #endif
39 
40 /* ********************************************************************** */
41 /* Include these for Open Inventor compatibility reasons (they are not
42  * actually used in Coin.)
43  */
44 #define SoEXTENDER
45 #define SoINTERNAL
46 
47 /* ********************************************************************** */
48 
49 /* Some useful inline template functions:
50  * SbAbs(Val) - returns absolute value
51  * SbMin(Val1, Val2) - returns minimum value
52  * SbMax(Val1, Val2) - returns maximum value
53  * SbClamp(Val, Min, Max) - returns clamped value
54  * SbSwap(Val1, Val2) - swaps the two values (no return value)
55  * SbSqr(val) - returns (val)²
56  */
57 
58 template <class Type>
59 inline Type SbAbs( Type Val ) {
60  return (Val < 0) ? 0 - Val : Val;
61 }
62 
63 template <class Type>
64 inline Type SbMax( const Type A, const Type B ) {
65  return (A < B) ? B : A;
66 }
67 
68 template <class Type>
69 inline Type SbMin( const Type A, const Type B ) {
70  return (A < B) ? A : B;
71 }
72 
73 template <class Type>
74 inline Type SbClamp( const Type Val, const Type Min, const Type Max ) {
75  return (Val < Min) ? Min : (Val > Max) ? Max : Val;
76 }
77 
78 template <class Type>
79 inline void SbSwap( Type & A, Type & B ) {
80  Type T; T = A; A = B; B = T;
81 }
82 
83 template <class Type>
84 inline Type SbSqr(const Type val) {
85  return val * val;
86 }
87 
88 /* *********************************************************************** */
89 
90 // SbDividerChk() - checks if divide-by-zero is attempted, and emits a
91 // warning if so for debug builds. inlined like this to not take much
92 // screenspace in inline functions.
93 
94 // cc_debugerror_post() is not attempted resolved before the template is
95 // used, hence the missing Inventor/errors/SoDebugError.h #include. This
96 // "trick" does only work *portably* for functions in the global namespace.
97 
98 template <typename Type>
99 inline void SbDividerChk(const char * funcname, Type divider) {
100 #ifndef NDEBUG
101  if (!(divider != static_cast<Type>(0)))
102  cc_debugerror_post(funcname, "divide by zero error.", divider);
103 #endif // !NDEBUG
104 }
105 
106 /* ********************************************************************** */
107 
108 /* COMPILER BUG WORKAROUND:
109 
110  We've had reports that Sun CC v4.0 is (likely) buggy, and doesn't
111  allow a statement like
112 
113  SoType SoNode::classTypeId = SoType::badType();
114 
115  As a hack we can however get around this by instead writing it as
116 
117  SoType SoNode::classTypeId;
118 
119  ..as the SoType variable will then be initialized to bitpattern
120  0x0000, which equals SoType::badType(). We can *however* not do
121  this for the Intel C/C++ compiler, as that does *not* init to the
122  0x0000 bitpattern (which may be a separate bug -- I haven't read
123  the C++ spec closely enough to decide whether that relied on
124  unspecified behavior or not).
125 
126  The latest version of the Intel compiler has been tested to still
127  have this problem, so I've decided to re-install the old code, but
128  in this form:
129 
130  SoType SoNode::classTypeId STATIC_SOTYPE_INIT;
131 
132  ..so it is easy to revert if somebody complains that the code
133  reversal breaks their old Sun CC 4.0 compiler -- see the #define of
134  STATIC_SOTYPE_INIT below.
135 
136  If that happens, we should work with the reporter, having access to
137  the buggy compiler, to make a configure check which sets the
138  SUN_CC_4_0_SOTYPE_INIT_BUG #define in include/Inventor/C/basic.h.in.
139 
140  (Note that the Sun CC compiler has moved on, and a later version
141  we've tested, 5.4, does not have the bug.)
142 
143  20050105 mortene.
144 */
145 
146 #define SUN_CC_4_0_SOTYPE_INIT_BUG 0 /* assume compiler is ok for now */
147 
148 #if SUN_CC_4_0_SOTYPE_INIT_BUG
149 #define STATIC_SOTYPE_INIT
150 #else
151 #define STATIC_SOTYPE_INIT = SoType::badType()
152 #endif
153 
154 /* ********************************************************************** */
155 
156 #endif /* !COIN_SBBASIC_H */

Copyright © 1998-2010 by Kongsberg Oil & Gas Technologies. All rights reserved.

Generated on Wed Feb 7 2018 for Coin by Doxygen 1.8.14.