xref: /netbsd-src/external/bsd/lutok/dist/stack_cleaner.cpp (revision a15637525a498a84b68d8b44b1c7ec01de4b564a)
1*a1563752Sjmmv // Copyright 2011 Google Inc.
2*a1563752Sjmmv // All rights reserved.
3*a1563752Sjmmv //
4*a1563752Sjmmv // Redistribution and use in source and binary forms, with or without
5*a1563752Sjmmv // modification, are permitted provided that the following conditions are
6*a1563752Sjmmv // met:
7*a1563752Sjmmv //
8*a1563752Sjmmv // * Redistributions of source code must retain the above copyright
9*a1563752Sjmmv //   notice, this list of conditions and the following disclaimer.
10*a1563752Sjmmv // * Redistributions in binary form must reproduce the above copyright
11*a1563752Sjmmv //   notice, this list of conditions and the following disclaimer in the
12*a1563752Sjmmv //   documentation and/or other materials provided with the distribution.
13*a1563752Sjmmv // * Neither the name of Google Inc. nor the names of its contributors
14*a1563752Sjmmv //   may be used to endorse or promote products derived from this software
15*a1563752Sjmmv //   without specific prior written permission.
16*a1563752Sjmmv //
17*a1563752Sjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*a1563752Sjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*a1563752Sjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*a1563752Sjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*a1563752Sjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*a1563752Sjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*a1563752Sjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*a1563752Sjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*a1563752Sjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*a1563752Sjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*a1563752Sjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*a1563752Sjmmv 
29*a1563752Sjmmv #include <cassert>
30*a1563752Sjmmv #include <cstring>
31*a1563752Sjmmv 
32*a1563752Sjmmv #include "stack_cleaner.hpp"
33*a1563752Sjmmv #include "state.ipp"
34*a1563752Sjmmv 
35*a1563752Sjmmv 
36*a1563752Sjmmv /// Internal implementation for lutok::stack_cleaner.
37*a1563752Sjmmv struct lutok::stack_cleaner::impl {
38*a1563752Sjmmv     /// Reference to the Lua state this stack_cleaner refers to.
39*a1563752Sjmmv     state& state_ref;
40*a1563752Sjmmv 
41*a1563752Sjmmv     /// The depth of the Lua stack to be restored.
42*a1563752Sjmmv     unsigned int original_depth;
43*a1563752Sjmmv 
44*a1563752Sjmmv     /// Constructor.
45*a1563752Sjmmv     ///
46*a1563752Sjmmv     /// \param state_ref_ Reference to the Lua state.
47*a1563752Sjmmv     /// \param original_depth_ The depth of the Lua stack.
impllutok::stack_cleaner::impl48*a1563752Sjmmv     impl(state& state_ref_, const unsigned int original_depth_) :
49*a1563752Sjmmv         state_ref(state_ref_),
50*a1563752Sjmmv         original_depth(original_depth_)
51*a1563752Sjmmv     {
52*a1563752Sjmmv     }
53*a1563752Sjmmv };
54*a1563752Sjmmv 
55*a1563752Sjmmv 
56*a1563752Sjmmv /// Creates a new stack cleaner.
57*a1563752Sjmmv ///
58*a1563752Sjmmv /// This gathers the current height of the stack so that extra elements can be
59*a1563752Sjmmv /// popped during destruction.
60*a1563752Sjmmv ///
61*a1563752Sjmmv /// \param state_ The Lua state.
stack_cleaner(state & state_)62*a1563752Sjmmv lutok::stack_cleaner::stack_cleaner(state& state_) :
63*a1563752Sjmmv     _pimpl(new impl(state_, state_.get_top()))
64*a1563752Sjmmv {
65*a1563752Sjmmv }
66*a1563752Sjmmv 
67*a1563752Sjmmv 
68*a1563752Sjmmv /// Pops any values from the stack not known at construction time.
69*a1563752Sjmmv ///
70*a1563752Sjmmv /// \pre The current height of the stack must be equal or greater to the height
71*a1563752Sjmmv /// of the stack when this object was instantiated.
~stack_cleaner(void)72*a1563752Sjmmv lutok::stack_cleaner::~stack_cleaner(void)
73*a1563752Sjmmv {
74*a1563752Sjmmv     const unsigned int current_depth = _pimpl->state_ref.get_top();
75*a1563752Sjmmv     assert(current_depth >= _pimpl->original_depth);
76*a1563752Sjmmv     const unsigned int diff = current_depth - _pimpl->original_depth;
77*a1563752Sjmmv     if (diff > 0)
78*a1563752Sjmmv         _pimpl->state_ref.pop(diff);
79*a1563752Sjmmv }
80*a1563752Sjmmv 
81*a1563752Sjmmv 
82*a1563752Sjmmv /// Forgets about any elements currently in the stack.
83*a1563752Sjmmv ///
84*a1563752Sjmmv /// This allows a function to return values on the stack because all the
85*a1563752Sjmmv /// elements that are currently in the stack will not be touched during
86*a1563752Sjmmv /// destruction when the function is called.
87*a1563752Sjmmv void
forget(void)88*a1563752Sjmmv lutok::stack_cleaner::forget(void)
89*a1563752Sjmmv {
90*a1563752Sjmmv     _pimpl->original_depth = _pimpl->state_ref.get_top();
91*a1563752Sjmmv }
92