LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
Manager.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008, 2009, 2010, 2011 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 #ifndef NDARRAY_Manager_h_INCLUDED
24 #define NDARRAY_Manager_h_INCLUDED
25 
32 #include "ndarray_fwd.h"
33 #include <boost/noncopyable.hpp>
34 #include <boost/intrusive_ptr.hpp>
35 #include <boost/scoped_array.hpp>
36 
37 namespace ndarray {
38 
39 class Manager : private boost::noncopyable {
40 public:
41 
42  typedef boost::intrusive_ptr<Manager> Ptr;
43 
44  friend inline void intrusive_ptr_add_ref(Manager const * manager) {
45  ++manager->_rc;
46  }
47 
48  friend inline void intrusive_ptr_release(Manager const * manager) {
49  if ((--manager->_rc)==0) delete manager;
50  }
51 
52  int getRC() const { return _rc; }
53 
54  virtual bool isUnique() const { return false; }
55 
56 protected:
57 
58  virtual ~Manager() {}
59 
60  explicit Manager() : _rc(0) {}
61 
62 private:
63  mutable int _rc;
64 };
65 
66 template <typename T>
67 class SimpleManager : public Manager {
68  typedef typename boost::remove_const<T>::type U;
69 public:
70 
71  static std::pair<Manager::Ptr,T*> allocate(int size) {
72  boost::intrusive_ptr<SimpleManager> r(new SimpleManager(size));
73  return std::pair<Manager::Ptr,T*>(r, r->_p.get());
74  }
75 
76  virtual bool isUnique() const { return true; }
77 
78 private:
79  explicit SimpleManager(int size) : _p() {
80  if (size > 0) _p.reset(new U[size]);
81  }
82  boost::scoped_array<U> _p;
83 };
84 
85 template <typename T> Manager::Ptr makeManager(T const & owner);
86 
87 template <typename U>
88 class ExternalManager : public Manager, private U {
89 public:
90  typedef U Owner;
91 
92  template <typename T> friend Manager::Ptr makeManager(T const & owner);
93 
94  Owner const & getOwner() const { return *static_cast<Owner const *>(this); }
95 
96 private:
97  explicit ExternalManager(Owner const & owner) : Owner(owner) {}
98 };
99 
100 template <typename T>
101 inline Manager::Ptr makeManager(T const & owner) {
102  return Manager::Ptr(new ExternalManager<T>(owner));
103 }
104 
105 // A no-op overload for makeManager to avoid unnecessary levels of indirection.
106 inline Manager::Ptr makeManager(Manager::Ptr const & owner) {
107  return owner;
108 }
109 
110 } // namespace ndarray
111 
112 #endif // !NDARRAY_Manager_h_INCLUDED
int getRC() const
Definition: Manager.h:52
virtual bool isUnique() const
Definition: Manager.h:54
friend void intrusive_ptr_release(Manager const *manager)
Definition: Manager.h:48
Forward declarations and default template parameters for ndarray.
boost::scoped_array< U > _p
Definition: Manager.h:82
virtual ~Manager()
Definition: Manager.h:58
boost::intrusive_ptr< Manager > Ptr
Definition: Manager.h:42
boost::remove_const< T >::type U
Definition: Manager.h:68
friend Manager::Ptr makeManager(T const &owner)
SimpleManager(int size)
Definition: Manager.h:79
friend void intrusive_ptr_add_ref(Manager const *manager)
Definition: Manager.h:44
virtual bool isUnique() const
Definition: Manager.h:76
Owner const & getOwner() const
Definition: Manager.h:94
static std::pair< Manager::Ptr, T * > allocate(int size)
Definition: Manager.h:71
ExternalManager(Owner const &owner)
Definition: Manager.h:97
Manager::Ptr makeManager(T const &owner)
Definition: Manager.h:101