summaryrefslogtreecommitdiff
path: root/include/o3tl/stack.hxx
blob: 1fa793481294035a522f552064a43a680b19f07a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#ifndef INCLUDED_O3TL_STACK_HXX
#define INCLUDED_O3TL_STACK_HXX

#include <stack>

namespace o3tl
{

/**
 *
 * Same as std::stack (it just wraps it) except at destruction time the
 * container elements are destroyed in order starting from the top of the stack
 * which is the order one would rather assume a stack uses, but doesn't have to
 *
 * https://connect.microsoft.com/VisualStudio/feedback/details/765649/std-vector-does-not-destruct-in-reverse-order-of-construction
 *
 **/

template<class T> class stack final
{
private:
    typedef std::stack<T> stack_t;

    stack_t mStack;
public:

    T& top()
    {
        return mStack.top();
    }

    const T& top() const
    {
        return mStack.top();
    }

    void push(const T& val)
    {
        mStack.push(val);
    }

    void push(T&& val)
    {
        mStack.push(val);
    }

    void pop()
    {
        mStack.pop();
    }

    bool empty() const
    {
        return mStack.empty();
    }

    ~stack()
    {
        while (!mStack.empty())
            mStack.pop();
    }
};

}

#endif /* INCLUDED_O3TL_STACK_HXX */

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */