ome-common  5.5.0
Base.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_BASE_H
40 #define OME_COMMON_XML_DOM_BASE_H
41 
42 #include <memory>
43 
44 #include <ome/common/config.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  std::shared_ptr<base_element_type>(wrapped, del) :
101  std::shared_ptr<base_element_type>())
102  {}
103 
109  explicit
110  Base(base_element_type *wrapped):
111  base(wrapped ?
112  std::shared_ptr<base_element_type>(wrapped, &ome::common::xml::dom::detail::unmanaged<base_element_type>) :
113  std::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() != nullptr;
155  }
156 
162  bool
163  operator == (std::nullptr_t) const
164  {
165  return get() == nullptr;
166  }
167 
173  bool
174  operator != (std::nullptr_t) const
175  {
176  return get() != nullptr;
177  }
178 
184  void
186  {
187  base.reset();
188  std::shared_ptr<base_element_type> n;
189  assign(n);
190  }
191 
192  protected:
198  virtual
199  void
200  null_check() const
201  {
202  if (!base.get())
203  throw std::logic_error("Accessing null wrapped DOM type");
204  }
205 
211  virtual
212  void
213  assign(const base_type& wrapped)
214  {
215  // Assign base directly to refcount already wrapped objects.
216  base = wrapped.base;
217  }
218 
224  virtual
225  void
226  assign(std::shared_ptr<base_element_type>& wrapped)
227  {
228  base = wrapped;
229  }
230 
241  template<typename D>
242  D *
243  assign_check(base_element_type *newbase)
244  {
245  D *newderived = dynamic_cast<D *>(newbase);
246  if (newbase && !newderived)
247  throw std::logic_error("Failed to assign incompatible wrapped DOM type");
248  return newderived;
249  }
250 
251  private:
253  std::shared_ptr<base_element_type> base;
254  };
255 
256  }
257  }
258  }
259 }
260 
261 #endif // OME_COMMON_XML_DOM_BASE_H
262 
263 /*
264  * Local Variables:
265  * mode:C++
266  * End:
267  */
Base()
Constructor.
Definition: Base.h:85
std::shared_ptr< base_element_type > base
Wrapped reference.
Definition: Base.h:253
bool operator!=(const boolean &lhs, bool rhs)
Compare boolean with bool for inequality.
Definition: boolean.h:214
virtual ~Base()
Destructor.
Definition: Base.h:118
STL namespace.
bool operator==(const boolean &lhs, bool rhs)
Compare boolean with bool for equality.
Definition: boolean.h:172
void reset()
Free the managed resource.
Definition: Base.h:185
virtual void null_check() const
Check if the wrapped type is null.
Definition: Base.h:200
Open Microscopy Environment C++.
Definition: base64.h:48
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:243
Base base_type
Base type.
Definition: Base.h:80
virtual void assign(const base_type &wrapped)
Assign a new wrapped value.
Definition: Base.h:213
virtual void assign(std::shared_ptr< base_element_type > &wrapped)
Assign a new wrapped value.
Definition: Base.h:226
Base(base_element_type *wrapped)
Construct with initial wrapped value (unmanaged).
Definition: Base.h:110