source: doc/papers/OOPSLA17/evaluation/cpp-vstack.cpp @ ae6b6cf

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ae6b6cf was f4e3419d, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

restructure paper documents

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