1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 /*!
34   \class SoFloatElement Inventor/elements/SoFloatElement.h
35   \brief SoFloatElement is an abstract base class for elements that consists of a single float value.
36 
37   \ingroup elements
38 
39   This is the superclass of elements where the new element data \e
40   replaces the old data, and where the data the element stores is a
41   simple single precision floating point value.
42 
43   This element is like a convenient light-weight version of the
44   SoReplacedElement. It differs from the SoReplacedElement in that the
45   set() and get() methods are already implemented, since it is known
46   that subclasses will still contain just a single float value.
47 
48   \sa SoReplacedElement, SoInt32Element, SoAccumulatedElement
49 */
50 
51 #include "coindefs.h"
52 #include "SbBasicP.h"
53 
54 #include <Inventor/elements/SoFloatElement.h>
55 #include <cassert>
56 
57 SO_ELEMENT_ABSTRACT_SOURCE(SoFloatElement);
58 
59 /*!
60   \var float SoFloatElement::data
61   The element's value.
62 */
63 
64 // doc in super
65 void
initClass(void)66 SoFloatElement::initClass(void)
67 {
68   SO_ELEMENT_INIT_ABSTRACT_CLASS(SoFloatElement, inherited);
69 }
70 
71 /*!
72   Destructor.
73 */
~SoFloatElement(void)74 SoFloatElement::~SoFloatElement(void)
75 {
76 }
77 
78 // doc in super
79 SbBool
matches(const SoElement * element) const80 SoFloatElement::matches(const SoElement * element) const
81 {
82   assert(element);
83   if (getTypeId() != element->getTypeId()) { return FALSE; }
84   if (this->data != (coin_assert_cast<const SoFloatElement *>(element)->data)) {
85     return FALSE;
86   }
87   return TRUE;
88 }
89 
90 // doc in super
91 SoElement *
copyMatchInfo(void) const92 SoFloatElement::copyMatchInfo(void) const
93 {
94   // SoElement::copyMatchInfo is abstract
95   //    inherited::copyMatchInfo();
96   assert(getTypeId().canCreateInstance());
97   SoFloatElement * element = static_cast<SoFloatElement *>(getTypeId().createInstance());
98   element->data = this->data;
99   // DEPRECATED 980807 pederb. copyMatchInfo() should only copy
100   // information needed in matches(). An exact copy is not needed.
101   //    element->dataNode = this->dataNode;
102   return element;
103 }
104 
105 // doc in super
106 void
print(FILE * file) const107 SoFloatElement::print(FILE * file) const
108 {
109   (void)fprintf(file, "%s[%p]: data = %f\n",
110                 this->getTypeId().getName().getString(), this, this->data);
111 }
112 
113 /*!
114   Static method for setting the \a value of an element in the given \a
115   state at the given stack \a index.
116  */
117 void
set(const int index,SoState * const state,SoNode * const COIN_UNUSED_ARG (node),const float value)118 SoFloatElement::set(const int index,
119                     SoState * const state,
120                     SoNode * const COIN_UNUSED_ARG(node),
121                     const float value)
122 {
123   SoFloatElement * element =
124     coin_safe_cast<SoFloatElement *>
125     (
126      SoFloatElement::getElement(state, index)
127      );
128   if (element) {
129     element->setElt(value);
130   }
131 }
132 
133 /*!
134   Static method for setting the \a value of an element in the given \a
135   state at the given \a stackIndex.
136  */
137 void
set(const int stackIndex,SoState * const state,const float value)138 SoFloatElement::set(const int stackIndex, SoState * const state,
139                     const float value)
140 {
141   SoFloatElement::set(stackIndex, state, NULL, value);
142 }
143 
144 /*!
145   Static method to fetch the value of the element of this type from
146   the given \a state at the given stack \a index.
147  */
148 float
get(const int index,SoState * const state)149 SoFloatElement::get(const int index, SoState * const state)
150 {
151   const SoFloatElement * element = coin_safe_cast<const SoFloatElement *>
152     (
153      getConstElement(state, index)
154      ); //, NULL );
155   if (element) { return element->data; }
156   return 0.0f;
157 }
158 
159 /*!
160   Set element value.
161  */
162 void
setElt(float value)163 SoFloatElement::setElt(float value)
164 {
165   this->data = value;
166 }
167 
168 // doc in super
169 void
init(SoState * state)170 SoFloatElement::init(SoState * state)
171 {
172   inherited::init(state);
173   this->data = 0.0f;
174 }
175