Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Node.hpp

    r87701b6 r54e41b3  
    4444        };
    4545
    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 {
    5847                switch (ref) {
    5948                        case ref_type::strong: strong_count++; break;
     
    6251        }
    6352
    64         void decrement(ast::Node::ref_type ref) const {
     53        inline void decrement(ref_type ref) const {
    6554                switch (ref) {
    6655                        case ref_type::strong: strong_count--; break;
     
    7261                }
    7362        }
     63private:
     64        /// Make a copy of this node; should be overridden in subclass with more precise return type
     65        virtual const Node * clone() const = 0;
    7466
    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;
    7773};
    7874
     
    8076// problems and be able to use auto return
    8177template<typename node_t>
    82 node_t * mutate( const node_t * node ) {
    83         if (node->strong_count <= 1) {
     78auto 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) {
    8484                return const_cast<node_t *>(node);
    8585        }
     
    100100public:
    101101        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); }
    104104
    105105        template< enum Node::ref_type o_ref_t >
    106106        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);
    108108        }
    109109
    110110        template< enum Node::ref_type o_ref_t >
    111111        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);
    113113        }
    114114
     
    147147                assign( n );
    148148                // get mutable version of `n`
    149                 auto r = mutate( node );
     149                auto r = mutate( node ); 
    150150                // re-assign mutable version in case `mutate()` produced a new pointer
    151151                assign( r );
     
    157157private:
    158158        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);
    161161                node = other;
    162162        }
    163 
    164         void _inc( const node_t * other );
    165         void _dec( const node_t * other );
    166163
    167164protected:
Note: See TracChangeset for help on using the changeset viewer.