ome-common  5.5.0
Wrapper.h
1 /*
2  * #%L
3  * OME-XERCES C++ library for working with Xerces C++.
4  * %%
5  * Copyright © 2006 - 2015 Open Microscopy Environment:
6  * - Massachusetts Institute of Technology
7  * - National Institutes of Health
8  * - University of Dundee
9  * - Board of Regents of the University of Wisconsin-Madison
10  * - Glencoe Software, Inc.
11  * %%
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * The views and conclusions contained in the software and documentation are
34  * those of the authors and should not be interpreted as representing official
35  * policies, either expressed or implied, of any organization.
36  * #L%
37  */
38 
39 #ifndef OME_COMMON_XML_DOM_WRAPPER_H
40 #define OME_COMMON_XML_DOM_WRAPPER_H
41 
42 #include <memory>
43 
44 #include <ome/common/config.h>
45 
46 #include <ome/common/xml/dom/Base.h>
47 
48 #include <string>
49 #include <stdexcept>
50 
51 namespace ome
52 {
53  namespace common
54  {
55  namespace xml
56  {
57  namespace dom
58  {
59 
71  template<typename WrappedType, typename Parent>
72  class Wrapper : public Parent
73  {
74  public:
76  typedef Parent parent_type;
82  typedef WrappedType element_type;
83 
85  Wrapper ():
86  parent_type(),
87  wrapped()
88  {
89  }
90 
96  Wrapper (const base_type& base):
97  parent_type(),
98  wrapped()
99  {
100  assign(base);
101  }
102 
109  template<typename Deleter>
110  explicit
112  Deleter del):
113  parent_type(),
114  wrapped()
115  {
116  std::shared_ptr<base_element_type> pbase;
117  if (base)
118  pbase = std::shared_ptr<base_element_type>(base, del);
119  else
120  pbase = std::shared_ptr<base_element_type>();
121  assign(pbase);
122  }
123 
129  explicit
131  parent_type(),
132  wrapped()
133  {
134  std::shared_ptr<base_element_type> pbase;
135  if (base)
136  pbase = std::shared_ptr<base_element_type>(base, &ome::common::xml::dom::detail::unmanaged<base_element_type>);
137  else
138  pbase = std::shared_ptr<base_element_type>();
139  assign(pbase);
140  }
141 
144  {
145  }
146 
154  element_type*
155  get()
156  {
157  return wrapped;
158  }
159 
167  const element_type*
168  get() const
169  {
170  return wrapped;
171  }
172 
173  protected:
179  virtual
180  void
181  assign(const base_type& wrapped)
182  {
183  this->wrapped = this->template assign_check<element_type>(const_cast<base_element_type *>(wrapped.get()));
184  parent_type::assign(wrapped);
185  }
186 
192  virtual
193  void
194  assign(std::shared_ptr<base_element_type>& wrapped)
195  {
196  this->wrapped = this->template assign_check<element_type>(wrapped.get());
197  parent_type::assign(wrapped);
198  }
199 
200  public:
207  Wrapper&
209  {
210  assign(wrapped);
211  return *this;
212  }
213 
219  element_type&
221  {
222  this->null_check();
223  return *wrapped;
224  }
225 
231  const element_type&
232  operator* () const
233  {
234  this->null_check();
235  return *wrapped;
236  }
237 
243  element_type *
245  {
246  this->null_check();
247  return wrapped;
248  }
249 
255  const element_type *
256  operator-> () const
257  {
258  this->null_check();
259  return wrapped;
260  }
261 
262  private:
264  element_type *wrapped;
265  };
266 
267  }
268  }
269  }
270 }
271 
272 #endif // OME_COMMON_XML_DOM_WRAPPER_H
273 
274 /*
275  * Local Variables:
276  * mode:C++
277  * End:
278  */
element_type * wrapped
The wrapped type.
Definition: Wrapper.h:264
~Wrapper()
Destructor.
Definition: Wrapper.h:143
Xerces DOM class wrapper.
Definition: Wrapper.h:72
Parent parent_type
Parent type.
Definition: Wrapper.h:76
virtual void assign(const base_type &wrapped)
Assign a new wrapped value.
Definition: Wrapper.h:181
WrappedType element_type
Wrapped Xerces element type.
Definition: Wrapper.h:82
parent_type::base_element_type base_element_type
Base element type.
Definition: Wrapper.h:80
Wrapper(typename parent_type::base_element_type *base)
Construct a Wrapper from a base_element_type * (unmanaged).
Definition: Wrapper.h:130
Open Microscopy Environment C++.
Definition: base64.h:48
base_element_type * get()
Get wrapped base_element_type *.
Definition: Base.h:129
Wrapper(typename parent_type::base_element_type *base, Deleter del)
Construct a Wrapper from a base_element_type * (managed).
Definition: Wrapper.h:111
Wrapper & operator=(const Wrapper &wrapped)
Assign a Wrapper.
Definition: Wrapper.h:208
element_type * operator->()
Dereference to element_type.
Definition: Wrapper.h:244
Base of the DOM wrapper hierarchy.
Definition: Base.h:76
virtual void assign(std::shared_ptr< base_element_type > &wrapped)
Assign a new wrapped value.
Definition: Wrapper.h:194
xercesc::DOMNode base_element_type
Base element type (root type of the wrapped type).
Definition: Base.h:82
element_type & operator*()
Dereference to element_type.
Definition: Wrapper.h:220
Wrapper()
Constructor.
Definition: Wrapper.h:85
parent_type::base_type base_type
Base type.
Definition: Wrapper.h:78
Wrapper(const base_type &base)
Copy construct a Wrapper.
Definition: Wrapper.h:96