ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
Manager.h
Go to the documentation of this file.
1// -*- c++ -*-
2/*
3 * Copyright (c) 2010-2012, Jim Bosch
4 * All rights reserved.
5 *
6 * ndarray is distributed under a simple BSD-like license;
7 * see the LICENSE file that should be present in the root
8 * of the source distribution, or alternately available at:
9 * https://github.com/ndarray/ndarray
10 */
11#ifndef NDARRAY_Manager_h_INCLUDED
12#define NDARRAY_Manager_h_INCLUDED
13
20#include "ndarray_fwd.h"
21#include <boost/noncopyable.hpp>
22#include <boost/intrusive_ptr.hpp>
23#include <boost/scoped_array.hpp>
24
25namespace ndarray {
26
27class Manager : private boost::noncopyable {
28public:
29
30 typedef boost::intrusive_ptr<Manager> Ptr;
31
32 friend inline void intrusive_ptr_add_ref(Manager const * manager) {
33 ++manager->_rc;
34 }
35
36 friend inline void intrusive_ptr_release(Manager const * manager) {
37 if ((--manager->_rc)==0) delete manager;
38 }
39
40 int getRC() const { return _rc; }
41
42 virtual bool isUnique() const { return false; }
43
44protected:
45
46 virtual ~Manager() {}
47
48 explicit Manager() : _rc(0) {}
49
50private:
51 mutable int _rc;
52};
53
54template <typename T>
55class SimpleManager : public Manager {
56 typedef typename boost::remove_const<T>::type U;
57public:
58
59 static std::pair<Manager::Ptr,T*> allocate(Size size) {
60 boost::intrusive_ptr<SimpleManager> r(new SimpleManager(size));
61 return std::pair<Manager::Ptr,T*>(r, r->_p.get());
62 }
63
64 virtual bool isUnique() const { return true; }
65
66private:
67 explicit SimpleManager(Size size) : _p() {
68 if (size > 0) _p.reset(new U[size]);
69 }
70 boost::scoped_array<U> _p;
71};
72
73template <typename T> Manager::Ptr makeManager(T const & owner);
74
75template <typename U>
76class ExternalManager : public Manager, private U {
77public:
78 typedef U Owner;
79
80 template <typename T> friend Manager::Ptr makeManager(T const & owner);
81
82 Owner const & getOwner() const { return *static_cast<Owner const *>(this); }
83
84private:
85 explicit ExternalManager(Owner const & owner) : Owner(owner) {}
86};
87
88template <typename T>
89inline Manager::Ptr makeManager(T const & owner) {
90 return Manager::Ptr(new ExternalManager<T>(owner));
91}
92
93// A no-op overload for makeManager to avoid unnecessary levels of indirection.
94inline Manager::Ptr makeManager(Manager::Ptr const & owner) {
95 return owner;
96}
97
98} // namespace ndarray
99
100#endif // !NDARRAY_Manager_h_INCLUDED
Definition Manager.h:76
Definition Manager.h:27
Definition Manager.h:55
Forward declarations and default template parameters for ndarray.