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 SoMFName SoMFName.h Inventor/fields/SoMFName.h
35   \brief The SoMFName class is a container for SbName values.
36 
37   \ingroup fields
38 
39   This field is used where nodes, engines or other field containers
40   needs to store arrays of names.
41 
42   \sa SoSFName
43 */
44 
45 // *************************************************************************
46 
47 #include <Inventor/fields/SoMFName.h>
48 
49 #include <cassert>
50 
51 #include <Inventor/SoInput.h>
52 #include <Inventor/errors/SoDebugError.h>
53 
54 #include "fields/SoSubFieldP.h"
55 
56 // *************************************************************************
57 
58 SO_MFIELD_SOURCE(SoMFName, SbName, const SbName &);
59 
60 // *************************************************************************
61 
62 // Override from parent class.
63 void
initClass(void)64 SoMFName::initClass(void)
65 {
66   SO_MFIELD_INTERNAL_INIT_CLASS(SoMFName);
67 }
68 
69 // *************************************************************************
70 
71 // No need to document readValue() and writeValue() here, as the
72 // necessary information is provided by the documentation of the
73 // parent classes.
74 #ifndef DOXYGEN_SKIP_THIS
75 
76 // This is implemented in the SoSFName class.
77 extern void sosfname_write_value(SoOutput * out, const SbName & val);
78 
79 SbBool
read1Value(SoInput * in,int idx)80 SoMFName::read1Value(SoInput * in, int idx)
81 {
82   assert(idx < this->maxNum);
83 
84   // Reading as SbString instead of as SbName, because the semantics
85   // of SoInput::read(SbName&) is to read token identifiers, such as
86   // node or field names, and doesn't e.g. handle quotes as expected
87   // for a "free-form" string.
88   SbString s;
89   SbBool ok = in->read(s);
90   if (!ok) return FALSE;
91   this->values[idx] = s;
92   return TRUE;
93 }
94 
95 void
write1Value(SoOutput * out,int idx) const96 SoMFName::write1Value(SoOutput * out, int idx) const
97 {
98   sosfname_write_value(out, (*this)[idx]);
99 }
100 
101 #endif // DOXYGEN_SKIP_THIS
102 
103 // *************************************************************************
104 
105 /*!
106   Set \a num \a strings from index \a start in this multiple-value
107   field instance.
108 */
109 void
setValues(const int start,const int numarg,const char * strings[])110 SoMFName::setValues(const int start, const int numarg, const char * strings[])
111 {
112   if(start+numarg > this->maxNum) this->allocValues(start+numarg);
113   else if(start+numarg > this->num) this->num = start+numarg;
114 
115   for(int i=0; i < numarg; i++) this->values[i+start] = SbName(strings[i]);
116   this->valueChanged();
117 }
118 
119 /*!
120   Set this field to contain only a single name, given by \a str.
121 */
122 void
setValue(const char * str)123 SoMFName::setValue(const char * str)
124 {
125   this->setValue(SbName(str));
126 }
127 
128 // *************************************************************************
129 
130 #ifdef COIN_TEST_SUITE
131 
BOOST_AUTO_TEST_CASE(initialized)132 BOOST_AUTO_TEST_CASE(initialized)
133 {
134   SoMFName field;
135   BOOST_CHECK_MESSAGE(field.getTypeId() != SoType::badType(),
136                       "missing class initialization");
137   BOOST_CHECK_EQUAL(field.getNum(), 0);
138 }
139 
140 #endif // COIN_TEST_SUITE
141