fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FieldContainer / Attachments / OSGStringAttributeMap.cpp
blob60199dd8d47ef39ef340e9893c93e9a82636bdd8
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include "OSGAttachmentContainer.h"
49 #include "OSGStringAttributeMap.h"
51 OSG_USING_NAMESPACE
53 // Documentation for this class is emited in the
54 // OSGStringAttributeMapBase.cpp file.
55 // To modify it, please change the .fcd file (OSGStringAttributeMap.fcd) and
56 // regenerate the base file.
58 /***************************************************************************\
59 * Class variables *
60 \***************************************************************************/
62 /***************************************************************************\
63 * Class methods *
64 \***************************************************************************/
66 void StringAttributeMap::initMethod(InitPhase ePhase)
68 Inherited::initMethod(ePhase);
70 if(ePhase == TypeObject::SystemPost)
76 /***************************************************************************\
77 * Instance methods *
78 \***************************************************************************/
80 /*-------------------------------------------------------------------------*\
81 - private -
82 \*-------------------------------------------------------------------------*/
84 /*----------------------- constructors & destructors ----------------------*/
86 StringAttributeMap::StringAttributeMap(void) :
87 Inherited()
91 StringAttributeMap::StringAttributeMap(const StringAttributeMap &source) :
92 Inherited(source)
96 StringAttributeMap::~StringAttributeMap(void)
100 /*----------------------------- class specific ----------------------------*/
102 void StringAttributeMap::changed(ConstFieldMaskArg whichField,
103 UInt32 origin,
104 BitVector details)
106 Inherited::changed(whichField, origin, details);
109 void StringAttributeMap::dump( UInt32 ,
110 const BitVector ) const
112 SLOG << "Dump StringAttributeMap NI" << std::endl;
117 /*! Sets the value associated with the named key in this attribute map.
118 If this attribute map does not include the named key, then the key is
119 added and assigned the given value. Otherwise, the value previously
120 associated with the named key is changed to \p value.
122 Both fields of this container (Keys and Values) are edited by this method.
125 void StringAttributeMap::setAttribute(const std::string& key,
126 const std::string& value)
128 MFString *keys = this->StringAttributeMapBase::editMFKeys();
129 MFString *values = this->StringAttributeMapBase::editMFValues();
131 unsigned int index(0);
133 // Find the index of key in _mfKeys. This index will be the index of the
134 // value associated with key in _mfValues.
135 for ( MFString::iterator i = keys->begin(); i != keys->end(); ++i, ++index )
137 if ( *i == key )
139 (*values)[index] = value;
140 return;
144 // key was not found in _mfKeys, so we add key to _mfKeys and value to
145 // _mfValues.
146 keys->push_back(key);
147 values->push_back(value);
150 /*! Attempts to look up the value associated with the named key in this
151 attribute map. If this attribute map does not include \p key, then
152 false is returned. Otherwise, \p value is set to the value associated
153 with the named key, and true is returned.
156 bool StringAttributeMap::getAttribute(const std::string& key,
157 std::string& value)
158 const
160 if ( hasAttribute(key) )
162 const MFString *keys = this->StringAttributeMapBase::getMFKeys();
163 const MFString *values = this->StringAttributeMapBase::getMFValues();
165 // Find the index of key in _mfKeys. This index will be the index of
166 // the value associated with key in _mfValues.
167 unsigned int index(0);
168 MFString::const_iterator i;
169 for ( i = keys->begin(); i != keys->end(); ++i, ++index )
171 if ( *i == key )
173 // Assign the value associated with key.
174 value = (*values)[index];
175 return true;
180 // _mfKeys does not contain key.
181 return false;
184 /*-------------------------------------------------------------------------*/
185 /* String Attribute Map Attachment Utility Functions */
187 OSG_BEGIN_NAMESPACE
189 /*! Gets the string-to-string attribute map associated with the given
190 attachment container. If the container does not already have a
191 string-to-string attribute map attached, a new one is created and
192 attached as a side effect of this function. Otherwise, the existing
193 string-to-string attribute map is returned. If \p container is
194 NullFC, then NullFC is returned. Otherwise, the newly created and
195 attached OSG::StringAttributeMapPtr is returned.
198 StringAttributeMapTransitPtr stringAttributeMap(
199 AttachmentContainer *container)
201 if ( NULL == container )
203 FFATAL(("stringAttributeMap: no container?!?\n"));
204 return StringAttributeMapTransitPtr(NULL);
207 StringAttributeMapUnrecPtr attr_map = NULL;
208 Attachment *attach_ptr =
209 container->findAttachment(StringAttributeMap::getClassType().getGroupId());
211 if ( NULL == attach_ptr )
213 attr_map = StringAttributeMap::create();
214 container->addAttachment(attr_map);
216 else
218 attr_map = dynamic_cast<StringAttributeMap *>(attach_ptr);
220 if ( attr_map == NULL )
222 FFATAL(("StringAttributeMap: StringAttributeMap Attachment is "
223 "not castable to StringAttributeMap?!?\n"));
224 return StringAttributeMapTransitPtr(NULL);
228 return StringAttributeMapTransitPtr(attr_map);
231 OSG_END_NAMESPACE