 Timestamp:
 May 8, 2019, 4:19:13 PM (5 years ago)
 Branches:
 ADT, armeh, astexperimental, cleanupdtors, enum, forallpointerdecay, jacob/cs343translation, jenkinssandbox, master, newast, newastuniqueexpr, pthreademulation, qualifiedEnum
 Children:
 2ed1d50
 Parents:
 b1d3ee1
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

src/AST/Node.hpp
rb1d3ee1 r6a625de 32 32 } 33 33 34 template<typename node_t> 35 friend auto mutate(const node_t * node); 36 34 37 private: 35 38 size_t strong_ref = 0; … … 37 40 }; 38 41 39 template< typename node_t, enum Node::ref_type ref_t> 42 // Mutate a node, nonmember function to avoid static type 43 // problems and be able to use auto return 44 template<typename node_t> 45 auto mutate(const node_t * node) { 46 assertf( 47 node>strong_count >= 1, 48 "Error: attempting to mutate a node that appears to have been linked" 49 ); 50 if (node>strong_count == 1) { 51 return const_cast<node_t *>(node); 52 } 53 54 assertf( 55 node>weak_count == 0, 56 "Error: mutating node with weak references to it will invalided some references" 57 ); 58 return node>clone(); 59 } 60 61 // All accept routines should look as follows : 62 // virtual void accept( Visitor &v ) override { 63 // return v.visit(this); 64 // } 65 // Using the following wrapper to handle the node type 66 template< typename node_t > 67 auto visit_this( visitor & v, node_t * node ) { 68 ptr<node_t> p; 69 p.node = node; 70 auto r = v.visit(p); 71 p.node = nullptr; 72 return r; 73 } 74 75 // Base class for the smart pointer types 76 // should never really be used. 77 template< typename node_t, enum Node::ref_type ref_t> 40 78 class ptr_base { 41 79 public: … … 87 125 88 126 template< typename node_t > 89 class ptr : public ptr_base< node_t, Node::ref_type::strong > { 90 public: 91 typedef ptr_base< node_t, Node::ref_type::strong > base_t; 92 93 ptr() = default; 94 ptr( node_t node ) : base_t( node ) {} 95 ~ptr() = default; 96 97 template< enum Node::ref_type ref_t > 98 ptr( const ptr_base<node_t, ref_t> & o ) : base_t(o) {} 99 100 template< enum Node::ref_type ref_t > 101 ptr( ptr_base<node_t, ref_t> && o ) : base_t( std::move(o) ) {} 102 103 template< enum Node::ref_type o_ref_t > 104 ptr & operator=( const ptr_base<node_t, o_ref_t> & o ) { 105 base_t::operator=(o); 106 return *this; 107 } 108 109 template< enum Node::ref_type o_ref_t > 110 ptr & operator=( ptr_base<node_t, o_ref_t> && o ) { 111 base_t::operator=(std::move(o)); 112 return *this; 113 } 114 115 node_t * mutate() { 116 using base_t::node; 117 assert(node>strong_count); 118 if (node>strong_count == 1) { 119 return node; 120 } 121 122 assertf(node>weak_count == 0, "Error: mutating node with weak references to it will invalided some references"); 123 auto n = new node_t(*node); 124 assign(n); 125 return node; 126 } 127 }; 127 using ptr = ptr_base< node_t, Node::ref_type::strong >; 128 128 129 129 template< typename node_t > 130 class readonly : public ptr_base< node_t, Node::ref_type::weak > { 131 public: 132 typedef ptr_base< node_t, Node::ref_type::strong > base_t; 133 134 readonly() = default; 135 readonly( node_t node ) : base_t( node ) {} 136 ~readonly() = default; 137 138 template< enum Node::ref_type ref_t > 139 readonly( const ptr_base<node_t, ref_t> & o ) : base_t(o) {} 140 141 template< enum Node::ref_type ref_t > 142 readonly( ptr_base<node_t, ref_t> && o ) : base_t( std::move(o) ) {} 143 144 template< enum Node::ref_type o_ref_t > 145 readonly & operator=( const ptr_base<node_t, o_ref_t> & o ) { 146 base_t::operator=(o); 147 return *this; 148 } 149 150 template< enum Node::ref_type o_ref_t > 151 readonly & operator=( ptr_base<node_t, o_ref_t> && o ) { 152 base_t::operator=(std::move(o)); 153 return *this; 154 } 155 }; 130 using readonly = ptr_base< node_t, Node::ref_type::weak >; 156 131 }
Note: See TracChangeset
for help on using the changeset viewer.