File: _node_handle.h

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (162 lines) | stat: -rw-r--r-- 5,173 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
    Copyright (c) 2019-2021 Intel Corporation

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#ifndef __TBB_detail__node_handle_H
#define __TBB_detail__node_handle_H

#include "_allocator_traits.h"
#include "_assert.h"

namespace tbb {
namespace detail {
namespace d1 {

// A structure to access private node handle methods in internal TBB classes
// Regular friend declaration is not convenient because classes which use node handle
// can be placed in the different versioning namespaces.
struct node_handle_accessor {
    template <typename NodeHandleType>
    static typename NodeHandleType::node* get_node_ptr( NodeHandleType& nh ) {
        return nh.get_node_ptr();
    }

    template <typename NodeHandleType>
    static NodeHandleType construct( typename NodeHandleType::node* node_ptr ) {
        return NodeHandleType{node_ptr};
    }

    template <typename NodeHandleType>
    static void deactivate( NodeHandleType& nh ) {
        nh.deactivate();
    }
}; // struct node_handle_accessor

template<typename Value, typename Node, typename Allocator>
class node_handle_base {
public:
    using allocator_type = Allocator;
protected:
    using node = Node;
    using allocator_traits_type = tbb::detail::allocator_traits<allocator_type>;
public:

    node_handle_base() : my_node(nullptr), my_allocator() {}
    node_handle_base(node_handle_base&& nh) : my_node(nh.my_node),
                                              my_allocator(std::move(nh.my_allocator)) {
        nh.my_node = nullptr;
    }

    __TBB_nodiscard bool empty() const { return my_node == nullptr; }
    explicit operator bool() const { return my_node != nullptr; }

    ~node_handle_base() { internal_destroy(); }

    node_handle_base& operator=( node_handle_base&& nh ) {
        internal_destroy();
        my_node = nh.my_node;
        move_assign_allocators(my_allocator, nh.my_allocator);
        nh.deactivate();
        return *this;
    }

    void swap( node_handle_base& nh ) {
        using std::swap;
        swap(my_node, nh.my_node);
        swap_allocators(my_allocator, nh.my_allocator);
    }

    allocator_type get_allocator() const {
        return my_allocator;
    }

protected:
    node_handle_base( node* n ) : my_node(n) {}

    void internal_destroy() {
        if(my_node != nullptr) {
            allocator_traits_type::destroy(my_allocator, my_node->storage());
            typename allocator_traits_type::template rebind_alloc<node> node_allocator(my_allocator);
            node_allocator.deallocate(my_node, 1);
        }
    }

    node* get_node_ptr() { return my_node; }

    void deactivate() { my_node = nullptr; }

    node* my_node;
    allocator_type my_allocator;
};

// node handle for maps
template<typename Key, typename Value, typename Node, typename Allocator>
class node_handle : public node_handle_base<Value, Node, Allocator> {
    using base_type = node_handle_base<Value, Node, Allocator>;
public:
    using key_type = Key;
    using mapped_type = typename Value::second_type;
    using allocator_type = typename base_type::allocator_type;

    node_handle() = default;

    key_type& key() const {
        __TBB_ASSERT(!this->empty(), "Cannot get key from the empty node_type object");
        return *const_cast<key_type*>(&(this->my_node->value().first));
    }

    mapped_type& mapped() const {
        __TBB_ASSERT(!this->empty(), "Cannot get mapped value from the empty node_type object");
        return this->my_node->value().second;
    }

private:
    friend struct node_handle_accessor;

    node_handle( typename base_type::node* n ) : base_type(n) {}
}; // class node_handle

// node handle for sets
template<typename Key, typename Node, typename Allocator>
class node_handle<Key, Key, Node, Allocator> : public node_handle_base<Key, Node, Allocator> {
    using base_type = node_handle_base<Key, Node, Allocator>;
public:
    using value_type = Key;
    using allocator_type = typename base_type::allocator_type;

    node_handle() = default;

    value_type& value() const {
        __TBB_ASSERT(!this->empty(), "Cannot get value from the empty node_type object");
        return *const_cast<value_type*>(&(this->my_node->value()));
    }

private:
    friend struct node_handle_accessor;

    node_handle( typename base_type::node* n ) : base_type(n) {}
}; // class node_handle

template <typename Key, typename Value, typename Node, typename Allocator>
void swap( node_handle<Key, Value, Node, Allocator>& lhs,
           node_handle<Key, Value, Node, Allocator>& rhs ) {
    return lhs.swap(rhs);
}

} // namespace d1
} // namespace detail
} // namespace tbb

#endif // __TBB_detail__node_handle_H