1 #ifndef COIN_SCXMLCOMMONP_H
2 #define COIN_SCXMLCOMMONP_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 <cstring>
37 #include <vector>
38 #include <algorithm>
39 
40 #include "coindefs.h"
41 #include "SbBasicP.h"
42 
43 // *************************************************************************
44 
45 #define SCXML__SET_ATTRIBUTE_VALUE(_ptr_, _name_, _value_)              \
46   do {                                                                  \
47     if ((_ptr_ != NULL) &&                                              \
48         (_ptr_ != this->getXMLAttribute(_name_))) {                     \
49       delete [] _ptr_;                                                  \
50     }                                                                   \
51     _ptr_ = NULL;                                                       \
52     if (_value_ != NULL) {                                              \
53       if (_value_ == this->getXMLAttribute(_name_)) {                   \
54         _ptr_ = const_cast<char *>(_value_);                            \
55       } else {                                                          \
56         _ptr_ = new char [std::strlen(_value_) + 1];                    \
57         std::strcpy(_ptr_, _value_);                                    \
58       }                                                                 \
59     }                                                                   \
60   } while (FALSE)
61 
62 #define SCXML__CLEAR_STD_VECTOR(_listobj_, _elementtype_)               \
63   do {                                                                  \
64     std::vector<_elementtype_>::iterator it = _listobj_.begin();        \
65     while (it != _listobj_.end()) {                                     \
66       delete *it;                                                       \
67       ++it;                                                             \
68     }                                                                   \
69     _listobj_.clear();                                                  \
70   } while ( FALSE )
71 
72 
73 #define SCXML_SINGLE_OBJECT_API_IMPL(classname, objtype, pointer, singular) \
74 void                                                                    \
75 classname::SO__CONCAT(set,singular)(objtype * obj)                      \
76 {                                                                       \
77   pointer.reset(obj);                                                   \
78   obj->setContainer(this);                                              \
79 }                                                                       \
80                                                                         \
81 objtype *                                                               \
82 classname::SO__CONCAT(get,singular)(void) const                         \
83 {                                                                       \
84   return pointer.get();                                                 \
85 }
86 
87 #define SCXML_LIST_OBJECT_API_IMPL(classname, objtype, objlist, singular, plural) \
88                                                                         \
89 int                                                                     \
90 classname::SO__CONCAT(getNum,plural)(void) const                        \
91 {                                                                       \
92   return static_cast<int>(objlist.size());                              \
93 }                                                                       \
94                                                                         \
95 objtype *                                                               \
96 classname::SO__CONCAT(get,singular)(int idx) const                      \
97 {                                                                       \
98   assert(idx >= 0 && idx < static_cast<int>(objlist.size()));           \
99   return objlist.at(idx);                                               \
100 }                                                                       \
101                                                                         \
102 void                                                                    \
103 classname::SO__CONCAT(add,singular)(objtype * obj)                      \
104 {                                                                       \
105   objlist.push_back(obj);                                               \
106   obj->setContainer(this);                                              \
107 }                                                                       \
108                                                                         \
109 void                                                                    \
110 classname::SO__CONCAT(remove,singular)(objtype * obj)                   \
111 {                                                                       \
112   std::vector<objtype *>::iterator it =                                 \
113     std::find(objlist.begin(), objlist.end(), obj);                     \
114   assert(it != objlist.end());                                          \
115   objlist.erase(it);                                                    \
116   obj->setContainer(NULL);                                              \
117 }                                                                       \
118                                                                         \
119 void                                                                    \
120 classname::SO__CONCAT(clearAll,plural)(void)                            \
121 {                                                                       \
122   std::vector<objtype *>::iterator it = objlist.begin();                \
123   while (it != objlist.end()) {                                         \
124     (*it)->setContainer(NULL);                                          \
125     ++it;                                                               \
126   }                                                                     \
127   objlist.clear();                                                      \
128 }
129 
130 #endif // !COIN_SCXMLCOMMONP_H
131