source: doc/generic_types/evaluation/cpp-vstack.cpp @ ff178ee

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since ff178ee was 2f0fc56, checked in by Aaron Moss <a3moss@…>, 7 years ago

Fix bug in C++ virtual clear()

  • Property mode set to 100644
File size: 1.2 KB
Line 
1#include "cpp-vstack.hpp"
2
3#include <utility>
4
5stack::node::node( const object& v ) : value( v.new_copy() ), next( nullptr ) {}
6
7stack::node::node( std::unique_ptr<object>&& v, node* n ) : value( std::move(v) ), next( n ) {}
8
9void stack::copy(const stack& o) {
10        node** crnt = &head;
11        node* next = o.head;
12        while ( next ) {
13                *crnt = new node{ *next->value };
14                crnt = &(*crnt)->next;
15                next = next->next;
16        }
17        *crnt = nullptr;
18}
19
20void stack::clear() {
21        node* next = head;
22        while ( next ) {
23                node* crnt = next;
24                next = crnt->next;
25                delete crnt;
26        }
27        head = nullptr;
28}
29
30stack::stack() : head(nullptr) {}
31
32stack::stack(const stack& o) { copy(o); }
33
34stack::stack(stack&& o) : head(o.head) { o.head = nullptr; }
35
36stack::~stack() { clear(); }
37
38stack& stack::operator= (const stack& o) {
39        if ( this == &o ) return *this;
40        clear();
41        copy(o);
42        return *this;
43}
44
45stack& stack::operator= (stack&& o) {
46        if ( this == &o ) return *this;
47        head = o.head;
48        o.head = nullptr;
49        return *this;
50}
51
52bool stack::empty() const {
53        return head == nullptr;
54}
55
56void stack::push(std::unique_ptr<object>&& value) {
57        head = new node{ std::move(value), head };
58}
59
60std::unique_ptr<object> stack::pop() {
61        node* n = head;
62        head = n->next;
63        std::unique_ptr<object> x = std::move(n->value);
64        delete n;
65        return x;
66}
Note: See TracBrowser for help on using the repository browser.