// Copyright 2011 Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include <cassert>29#include <cstring>3031#include "stack_cleaner.hpp"32#include "state.ipp"333435/// Internal implementation for lutok::stack_cleaner.36struct lutok::stack_cleaner::impl {37/// Reference to the Lua state this stack_cleaner refers to.38state& state_ref;3940/// The depth of the Lua stack to be restored.41unsigned int original_depth;4243/// Constructor.44///45/// \param state_ref_ Reference to the Lua state.46/// \param original_depth_ The depth of the Lua stack.47impl(state& state_ref_, const unsigned int original_depth_) :48state_ref(state_ref_),49original_depth(original_depth_)50{51}52};535455/// Creates a new stack cleaner.56///57/// This gathers the current height of the stack so that extra elements can be58/// popped during destruction.59///60/// \param state_ The Lua state.61lutok::stack_cleaner::stack_cleaner(state& state_) :62_pimpl(new impl(state_, state_.get_top()))63{64}656667/// Pops any values from the stack not known at construction time.68///69/// \pre The current height of the stack must be equal or greater to the height70/// of the stack when this object was instantiated.71lutok::stack_cleaner::~stack_cleaner(void)72{73const unsigned int current_depth = _pimpl->state_ref.get_top();74assert(current_depth >= _pimpl->original_depth);75const unsigned int diff = current_depth - _pimpl->original_depth;76if (diff > 0)77_pimpl->state_ref.pop(diff);78}798081/// Forgets about any elements currently in the stack.82///83/// This allows a function to return values on the stack because all the84/// elements that are currently in the stack will not be touched during85/// destruction when the function is called.86void87lutok::stack_cleaner::forget(void)88{89_pimpl->original_depth = _pimpl->state_ref.get_top();90}919293