Changes in src/AST/Node.hpp [54e41b3:87701b6]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Node.hpp
r54e41b3 r87701b6 44 44 }; 45 45 46 inline void increment(ref_type ref) const { 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 { 47 58 switch (ref) { 48 59 case ref_type::strong: strong_count++; break; … … 51 62 } 52 63 53 inline void decrement(ref_type ref) const {64 void decrement(ast::Node::ref_type ref) const { 54 65 switch (ref) { 55 66 case ref_type::strong: strong_count--; break; … … 61 72 } 62 73 } 63 private:64 /// Make a copy of this node; should be overridden in subclass with more precise return type65 virtual const Node * clone() const = 0;66 74 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; 75 template< typename node_t, enum Node::ref_type ref_t > 76 friend class ptr_base; 73 77 }; 74 78 … … 76 80 // problems and be able to use auto return 77 81 template<typename node_t> 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) { 82 node_t * mutate( const node_t * node ) { 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 ) increment(node, ref_t); }103 ~ptr_base() { if( node ) decrement(node, ref_t); }102 ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); } 103 ~ptr_base() { if( node ) _dec(node); } 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 ) increment(node, ref_t);107 if( node ) _inc(node); 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 ) increment(node, ref_t);112 if( node ) _inc(node); 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 ) increment(other, ref_t);160 if( node ) decrement(node , ref_t);159 if( other ) _inc(other); 160 if( node ) _dec(node ); 161 161 node = other; 162 162 } 163 164 void _inc( const node_t * other ); 165 void _dec( const node_t * other ); 163 166 164 167 protected:
Note: See TracChangeset
for help on using the changeset viewer.