More boostisms
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractXMLObject.h
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * @file xmltooling/AbstractXMLObject.h
23  *
24  * An abstract implementation of XMLObject.
25  */
26
27 #ifndef __xmltooling_abstractxmlobj_h__
28 #define __xmltooling_abstractxmlobj_h__
29
30 #include <xmltooling/logging.h>
31 #include <xmltooling/QName.h>
32 #include <xmltooling/XMLObject.h>
33
34 #if defined (_MSC_VER)
35     #pragma warning( push )
36     #pragma warning( disable : 4250 4251 )
37 #endif
38
39 namespace xmltooling {
40
41     class XMLTOOL_API DateTime;
42
43     /**
44      * An abstract implementation of XMLObject.
45      * This is the primary concrete base class, and supplies basic namespace,
46      * type, and parent handling. Most implementation classes should not
47      * directly inherit from this class, but rather from the various mixins
48      * that supply the rest of the XMLObject interface, as required.
49      */
50     class XMLTOOL_API AbstractXMLObject : public virtual XMLObject
51     {
52     public:
53         virtual ~AbstractXMLObject();
54
55         // Virtual function overrides.
56         void detach();
57         const QName& getElementQName() const;
58         const std::set<Namespace>& getNamespaces() const;
59         void addNamespace(const Namespace& ns) const;
60         void removeNamespace(const Namespace& ns);
61         const QName* getSchemaType() const;
62         const XMLCh* getXMLID() const;
63         xmlconstants::xmltooling_bool_t getNil() const;
64         void nil(xmlconstants::xmltooling_bool_t value);
65         bool hasParent() const;
66         XMLObject* getParent() const;
67         void setParent(XMLObject* parent);
68
69      protected:
70         /**
71          * Constructor
72          *
73          * @param nsURI         the namespace of the element
74          * @param localName     the local name of the XML element this Object represents
75          * @param prefix        the namespace prefix to use
76          * @param schemaType    the xsi:type to use
77          */
78         AbstractXMLObject(
79             const XMLCh* nsURI=nullptr, const XMLCh* localName=nullptr, const XMLCh* prefix=nullptr, const QName* schemaType=nullptr
80             );
81
82         /** Copy constructor. */
83         AbstractXMLObject(const AbstractXMLObject& src);
84
85         /**
86          * A helper function for derived classes, for assignment of strings.
87          *
88          * This 'normalizes' newString, and then if it is different from oldString,
89          * it invalidates the DOM, frees the old string, and returns the new.
90          * If not different, it frees the new string and just returns the old value.
91          *
92          * @param oldValue the current value
93          * @param newValue the new value
94          *
95          * @return the value that should be assigned
96          */
97         XMLCh* prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue);
98
99         /**
100          * A helper function for derived classes, for assignment of date/time data.
101          *
102          * It invalidates the DOM, frees the old object, and returns the new.
103          *
104          * @param oldValue the current value
105          * @param newValue the new value
106          *
107          * @return the value that should be assigned
108          */
109         DateTime* prepareForAssignment(DateTime* oldValue, const DateTime* newValue);
110
111         /**
112          * A helper function for derived classes, for assignment of date/time data.
113          *
114          * It invalidates the DOM, frees the old object, and returns the new.
115          *
116          * @param oldValue the current value
117          * @param newValue the epoch to assign as the new value
118          * @param duration true iff the value is a duration rather than an absolute timestamp
119          *
120          * @return the value that should be assigned
121          */
122         DateTime* prepareForAssignment(DateTime* oldValue, time_t newValue, bool duration=false);
123
124         /**
125          * A helper function for derived classes, for assignment of date/time data.
126          *
127          * It invalidates the DOM, frees the old object, and returns the new.
128          *
129          * @param oldValue the current value
130          * @param newValue the new value in string form
131          * @param duration true iff the value is a duration rather than an absolute timestamp
132          *
133          * @return the value that should be assigned
134          */
135         DateTime* prepareForAssignment(DateTime* oldValue, const XMLCh* newValue, bool duration=false);
136
137         /**
138          * A helper function for derived classes, for assignment of QName data.
139          *
140          * It invalidates the DOM, frees the old object, and returns the new.
141          *
142          * @param oldValue the current value
143          * @param newValue the new value
144          *
145          * @return the value that should be assigned
146          */
147         QName* prepareForAssignment(QName* oldValue, const QName* newValue);
148
149         /**
150          * A helper function for derived classes, for assignment of (singleton) XML objects.
151          *
152          * It is indifferent to whether either the old or the new version of the value is null.
153          * This method will do a safe compare of the objects and will also invalidate the DOM if appropriate.
154          * Note that since the new value (even if nullptr) is always returned, it may be more efficient
155          * to discard the return value and just assign independently if a dynamic cast would be involved.
156          *
157          * @param oldValue current value
158          * @param newValue proposed new value
159          * @return the new value
160          *
161          * @throws XMLObjectException if the new child already has a parent.
162          */
163         XMLObject* prepareForAssignment(XMLObject* oldValue, XMLObject* newValue);
164
165         /**
166          * Set of namespaces associated with the object.
167          */
168         mutable std::set<Namespace> m_namespaces;
169
170         /**
171          * Logging object.
172          */
173         logging::Category& m_log;
174
175         /**
176          * Stores off xsi:schemaLocation attribute.
177          */
178         XMLCh* m_schemaLocation;
179
180         /**
181          * Stores off xsi:noNamespaceSchemaLocation attribute.
182          */
183         XMLCh* m_noNamespaceSchemaLocation;
184
185         /**
186          * Stores off xsi:nil attribute.
187          */
188         xmlconstants::xmltooling_bool_t m_nil;
189
190     private:
191         XMLObject* m_parent;
192         QName m_elementQname;
193         std::auto_ptr<QName> m_typeQname;
194     };
195
196 };
197
198 #if defined (_MSC_VER)
199     #pragma warning( pop )
200 #endif
201
202 #endif /* __xmltooling_abstractxmlobj_h__ */