blob: 14dcfb271a4b9ae3c12b24459e682d0b391b515b [file] [log] [blame]
Jeff Thompsonf7d49942013-08-01 16:47:40 -07001/*
2 * Copyright (c) 2012 Glen Joseph Fernandes
3 * glenfe at live dot com
4 *
5 * Distributed under the Boost Software License,
6 * Version 1.0. (See accompanying file LICENSE_1_0.txt
7 * or copy at http://boost.org/LICENSE_1_0.txt)
8 */
9#ifndef BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
10#define BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
11
Jeff Thompson2277ce52013-08-01 17:34:11 -070012#include <ndnboost/smart_ptr/detail/array_utility.hpp>
13#include <ndnboost/smart_ptr/detail/sp_forward.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070014
15namespace ndnboost {
16 namespace detail {
17 template<typename T>
18 class array_deleter;
19 template<typename T>
20 class array_deleter<T[]> {
21 public:
Jeff Thompsona28eed82013-08-22 16:21:10 -070022 array_deleter(std::size_t size_)
23 : size(size_),
Jeff Thompsonf7d49942013-08-01 16:47:40 -070024 object(0) {
25 }
26 ~array_deleter() {
27 if (object) {
28 array_destroy(object, size);
29 }
30 }
31 void init(T* memory) {
32 array_init(memory, size);
33 object = memory;
34 }
35#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
36 void init(T* memory, T&& value) {
37 array_init_value(memory, size, sp_forward<T>(value));
38 object = memory;
39 }
40#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
41 template<typename... Args>
42 void init(T* memory, Args&&... args) {
43 array_init_args(memory, size, sp_forward<Args>(args)...);
44 object = memory;
45 }
46#endif
47#endif
48 void init_list(T* memory, const T* list) {
49 array_init_list(memory, size, list);
50 object = memory;
51 }
52 template<std::size_t M>
53 void init_list(T* memory, const T* list) {
54 array_init_list<T, M>(memory, size, list);
55 object = memory;
56 }
57 void noinit(T* memory) {
58 array_noinit(memory, size);
59 object = memory;
60 }
61 void operator()(const void*) {
62 if (object) {
63 array_destroy(object, size);
64 object = 0;
65 }
66 }
67 private:
68 std::size_t size;
69 T* object;
70 };
71 template<typename T, std::size_t N>
72 class array_deleter<T[N]> {
73 public:
74 array_deleter()
75 : object(0) {
76 }
77 ~array_deleter() {
78 if (object) {
79 array_destroy(object, N);
80 }
81 }
82 void init(T* memory) {
83 array_init(memory, N);
84 object = memory;
85 }
86#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
87 void init(T* memory, T&& value) {
88 array_init_value(memory, N, sp_forward<T>(value));
89 object = memory;
90 }
91#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
92 template<typename... Args>
93 void init(T* memory, Args&&... args) {
94 array_init_args(memory, N, sp_forward<Args>(args)...);
95 object = memory;
96 }
97#endif
98#endif
99 void init_list(T* memory, const T* list) {
100 array_init_list(memory, N, list);
101 object = memory;
102 }
103 template<std::size_t M>
104 void init_list(T* memory, const T* list) {
105 array_init_list<T, M>(memory, N, list);
106 object = memory;
107 }
108 void noinit(T* memory) {
109 array_noinit(memory, N);
110 object = memory;
111 }
112 void operator()(const void*) {
113 if (object) {
114 array_destroy(object, N);
115 object = 0;
116 }
117 }
118 private:
119 T* object;
120 };
121 }
122}
123
124#endif