bioformats  5.1.8
Base.h
1 /*
2  * #%L
3  * OME-XERCES C++ library for working with Xerces C++.
4  * %%
5  * Copyright © 2006 - 2016 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_BASE_H
40 #define OME_COMMON_XML_DOM_BASE_H
41 
42 #include <ome/common/config.h>
43 
44 #include <ome/compat/memory.h>
45 
46 #include <iostream>
47 #include <stdexcept>
48 
49 namespace ome
50 {
51  namespace common
52  {
53  namespace xml
54  {
55  namespace dom
56  {
57 
58  namespace detail
59  {
60 
61  template<typename T>
62  inline
63  void
64  unmanaged(T *)
65  {}
66 
67  }
68 
75  template<typename T>
76  class Base
77  {
78  public:
80  typedef Base base_type;
82  typedef T base_element_type;
83 
85  Base():
86  base()
87  {}
88 
95  template<typename Deleter>
96  explicit
97  Base(base_element_type *wrapped,
98  Deleter del):
99  base(wrapped ?
100  ome::compat::shared_ptr<base_element_type>(wrapped, del) :
101  ome::compat::shared_ptr<base_element_type>())
102  {}
103 
109  explicit
110  Base(base_element_type *wrapped):
111  base(wrapped ?
112  ome::compat::shared_ptr<base_element_type>(wrapped, &ome::common::xml::dom::detail::unmanaged<base_element_type>) :
113  ome::compat::shared_ptr<base_element_type>())
114  {}
115 
117  virtual
119  {}
120 
128  base_element_type*
129  get()
130  {
131  return base.get();
132  }
133 
141  const base_element_type*
142  get() const
143  {
144  return base.get();
145  }
146 
152  operator bool () const
153  {
154  return get() != 0;
155  }
156 
162  void
164  {
165  base.reset();
166  ome::compat::shared_ptr<base_element_type> n;
167  assign(n);
168  }
169 
170  protected:
176  virtual
177  void
178  null_check() const
179  {
180  if (!base.get())
181  throw std::logic_error("Accessing null wrapped DOM type");
182  }
183 
189  virtual
190  void
191  assign(const base_type& wrapped)
192  {
193  // Assign base directly to refcount already wrapped objects.
194  base = wrapped.base;
195  }
196 
202  virtual
203  void
204  assign(ome::compat::shared_ptr<base_element_type>& wrapped)
205  {
206  base = wrapped;
207  }
208 
219  template<typename D>
220  D *
221  assign_check(base_element_type *newbase)
222  {
223  D *newderived = dynamic_cast<D *>(newbase);
224  if (newbase && !newderived)
225  throw std::logic_error("Failed to assign incompatible wrapped DOM type");
226  return newderived;
227  }
228 
229  private:
231  ome::compat::shared_ptr<base_element_type> base;
232  };
233 
234  }
235  }
236  }
237 }
238 
239 #endif // OME_COMMON_XML_DOM_BASE_H
240 
241 /*
242  * Local Variables:
243  * mode:C++
244  * End:
245  */
Base()
Constructor.
Definition: Base.h:85
virtual void assign(ome::compat::shared_ptr< base_element_type > &wrapped)
Assign a new wrapped value.
Definition: Base.h:204
Memory type substitution.
virtual ~Base()
Destructor.
Definition: Base.h:118
void reset()
Free the managed resource.
Definition: Base.h:163
Open Microscopy Environment C++ implementation.
Definition: CoreMetadata.cpp:40
Base(base_element_type *wrapped, Deleter del)
Construct with initial wrapped value (managed).
Definition: Base.h:97
Base of the DOM wrapper hierarchy.
Definition: Base.h:76
T base_element_type
Base element type (root type of the wrapped type).
Definition: Base.h:82
D * assign_check(base_element_type *newbase)
Check that a new wrapped value is of the correct derived type.
Definition: Base.h:221
Base base_type
Base type.
Definition: Base.h:80
virtual void assign(const base_type &wrapped)
Assign a new wrapped value.
Definition: Base.h:191
ome::compat::shared_ptr< base_element_type > base
Wrapped reference.
Definition: Base.h:231
Base(base_element_type *wrapped)
Construct with initial wrapped value (unmanaged).
Definition: Base.h:110
Xerces-C modern C++ wrapper.
Definition: Base.h:53
virtual void null_check() const
Check if the wrapped type is NULL.
Definition: Base.h:178