Changes in src/AST/Node.hpp [87701b6:54e41b3]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Node.hpp
r87701b6 r54e41b3 44 44 }; 45 45 46 private: 47 /// Make a copy of this node; should be overridden in subclass with more precise return type 48 virtual Node * clone() const = 0; 49 50 /// Must be copied in ALL derived classes 51 template<typename node_t> 52 friend node_t * mutate(const node_t * node); 53 54 mutable size_t strong_count = 0; 55 mutable size_t weak_count = 0; 56 57 void increment(ref_type ref) const { 46 inline void increment(ref_type ref) const { 58 47 switch (ref) { 59 48 case ref_type::strong: strong_count++; break; … … 62 51 } 63 52 64 void decrement(ast::Node::ref_type ref) const {53 inline void decrement(ref_type ref) const { 65 54 switch (ref) { 66 55 case ref_type::strong: strong_count--; break; … … 72 61 } 73 62 } 63 private: 64 /// Make a copy of this node; should be overridden in subclass with more precise return type 65 virtual const Node * clone() const = 0; 74 66 75 template< typename node_t, enum Node::ref_type ref_t > 76 friend class ptr_base; 67 /// Must be copied in ALL derived classes 68 template<typename node_t> 69 friend auto mutate(const node_t * node); 70 71 mutable size_t strong_count = 0; 72 mutable size_t weak_count = 0; 77 73 }; 78 74 … … 80 76 // problems and be able to use auto return 81 77 template<typename node_t> 82 node_t * mutate( const node_t * node ) { 83 if (node->strong_count <= 1) { 78 auto mutate( const node_t * node ) { 79 assertf( 80 node->strong_count >= 1, 81 "Error: attempting to mutate a node that appears to have been linked" 82 ); 83 if (node->strong_count == 1) { 84 84 return const_cast<node_t *>(node); 85 85 } … … 100 100 public: 101 101 ptr_base() : node(nullptr) {} 102 ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); }103 ~ptr_base() { if( node ) _dec(node); }102 ptr_base( const node_t * n ) : node(n) { if( node ) increment(node, ref_t); } 103 ~ptr_base() { if( node ) decrement(node, ref_t); } 104 104 105 105 template< enum Node::ref_type o_ref_t > 106 106 ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.node) { 107 if( node ) _inc(node);107 if( node ) increment(node, ref_t); 108 108 } 109 109 110 110 template< enum Node::ref_type o_ref_t > 111 111 ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.node) { 112 if( node ) _inc(node);112 if( node ) increment(node, ref_t); 113 113 } 114 114 … … 147 147 assign( n ); 148 148 // get mutable version of `n` 149 auto r = mutate( node ); 149 auto r = mutate( node ); 150 150 // re-assign mutable version in case `mutate()` produced a new pointer 151 151 assign( r ); … … 157 157 private: 158 158 void assign( const node_t * other ) { 159 if( other ) _inc(other);160 if( node ) _dec(node);159 if( other ) increment(other, ref_t); 160 if( node ) decrement(node , ref_t); 161 161 node = other; 162 162 } 163 164 void _inc( const node_t * other );165 void _dec( const node_t * other );166 163 167 164 protected:
Note: See TracChangeset
for help on using the changeset viewer.