mn_node_ptr.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2021 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef MINLIB_aff55cd7_915b_4f3d_82b9_124e264a3df5_H_
19 #define MINLIB_aff55cd7_915b_4f3d_82b9_124e264a3df5_H_
20 
21 #include "../mn_config.hpp"
22 
23 #include "../container/mn_node.hpp"
24 
25 namespace mn {
26  namespace pointer {
27 
28  template <typename T, class TAllocator>
29  class node_ptr {
30  public:
32  using element_type = T;
33  using value_type = T;
34  using pointer = T*;
35  using reference = T&;
36  using node_type = container::basic_value_node<pointer, TAllocator>*;
37 
39  : m_pNode(0) { }
40  explicit node_ptr(const pointer value) {
41  m_pNode = new node_type(value); }
42 
44  clear(); if(m_pNode) delete m_pNode;
45  }
46 
47  void add_child(const pointer value) {
48  node_type* newNode = new node_type(value); ;
49  newNode->insert(m_pNode->Next);
50  }
51  void remove_child(const node_type* pChild) {
52  if(pChild == NULL) return;
53 
54  node_type* nextNode = container::upcast(pChild->Next);
55 
56  if(this->m_pNode == pChild) {
57  nextNode->remove(); delete nextNode;
58  } else {
59  remove_child(nextNode);
60  }
61  }
63  return m_pNode;
64  }
65  void clear() {
66  node_type it = upcast(m_pNode->Next);
67 
68  while (it != m_pNode) {
69  node_type nextIt = upcast(it->Next);
70  if(it) delete it;
71  it = nextIt;
72  }
73  m_root.reset();
74  }
75 
77  pointer tmp = m_pNode->Value;
78  m_pNode->Value = NULL;
79  return tmp;
80  }
81  void reset(pointer p = NULL) {
82  clear();
83 
84  delete m_pNode;
85  m_pNode = new node_type(p);
86 
87  }
88  void swap(self_type &other) {
89  m_pNode->swap(other);
90  }
91 
92  const pointer get() const { return m_pNode->Value; }
93  pointer get() { return m_pNode->Value; }
94 
95  const reference operator *() const { return *m_pNode->Value; }
96  reference operator *() { return *m_pNode->Value; }
97 
98  const pointer operator->() const { return m_pNode->Value; }
99  pointer operator->() { return m_pNode->Value; }
100 
101  node_ptr(const node_ptr &other) = delete;
102  node_ptr const &operator=(const node_ptr &other) = delete;
103 
104  private:
106  };
107  }
108 }
109 
110 #endif
Definition: mn_node_ptr.hpp:29
const pointer operator->() const
Definition: mn_node_ptr.hpp:98
node_ptr(const pointer value)
Definition: mn_node_ptr.hpp:40
T value_type
Definition: mn_node_ptr.hpp:33
node_ptr()
Definition: mn_node_ptr.hpp:38
void clear()
Definition: mn_node_ptr.hpp:65
~node_ptr()
Definition: mn_node_ptr.hpp:43
T & reference
Definition: mn_node_ptr.hpp:35
void add_child(const pointer value)
Definition: mn_node_ptr.hpp:47
void remove_child(const node_type *pChild)
Definition: mn_node_ptr.hpp:51
const pointer get() const
Definition: mn_node_ptr.hpp:92
pointer operator->()
Definition: mn_node_ptr.hpp:99
container::basic_value_node< pointer, TAllocator > * node_type
Definition: mn_node_ptr.hpp:36
pointer get()
Definition: mn_node_ptr.hpp:93
node_ptr const & operator=(const node_ptr &other)=delete
T element_type
Definition: mn_node_ptr.hpp:32
node_type child()
Definition: mn_node_ptr.hpp:62
node_ptr(const node_ptr &other)=delete
pointer release()
Definition: mn_node_ptr.hpp:76
void swap(self_type &other)
Definition: mn_node_ptr.hpp:88
node_type m_pNode
Definition: mn_node_ptr.hpp:105
void reset(pointer p=NULL)
Definition: mn_node_ptr.hpp:81
T * pointer
Definition: mn_node_ptr.hpp:34
const reference operator*() const
Definition: mn_node_ptr.hpp:95
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
Definition: mn_allocator_typetraits.hpp:25