14b2c3eb9SBrooks Davis // Copyright 2011 Google Inc.
24b2c3eb9SBrooks Davis // All rights reserved.
34b2c3eb9SBrooks Davis //
44b2c3eb9SBrooks Davis // Redistribution and use in source and binary forms, with or without
54b2c3eb9SBrooks Davis // modification, are permitted provided that the following conditions are
64b2c3eb9SBrooks Davis // met:
74b2c3eb9SBrooks Davis //
84b2c3eb9SBrooks Davis // * Redistributions of source code must retain the above copyright
94b2c3eb9SBrooks Davis // notice, this list of conditions and the following disclaimer.
104b2c3eb9SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
114b2c3eb9SBrooks Davis // notice, this list of conditions and the following disclaimer in the
124b2c3eb9SBrooks Davis // documentation and/or other materials provided with the distribution.
134b2c3eb9SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
144b2c3eb9SBrooks Davis // may be used to endorse or promote products derived from this software
154b2c3eb9SBrooks Davis // without specific prior written permission.
164b2c3eb9SBrooks Davis //
174b2c3eb9SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
184b2c3eb9SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
194b2c3eb9SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
204b2c3eb9SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
214b2c3eb9SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
224b2c3eb9SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
234b2c3eb9SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244b2c3eb9SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
254b2c3eb9SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264b2c3eb9SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
274b2c3eb9SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
284b2c3eb9SBrooks Davis
294b2c3eb9SBrooks Davis #include <cassert>
304b2c3eb9SBrooks Davis #include <cstring>
314b2c3eb9SBrooks Davis
324b2c3eb9SBrooks Davis #include "stack_cleaner.hpp"
334b2c3eb9SBrooks Davis #include "state.ipp"
344b2c3eb9SBrooks Davis
354b2c3eb9SBrooks Davis
364b2c3eb9SBrooks Davis /// Internal implementation for lutok::stack_cleaner.
374b2c3eb9SBrooks Davis struct lutok::stack_cleaner::impl {
384b2c3eb9SBrooks Davis /// Reference to the Lua state this stack_cleaner refers to.
394b2c3eb9SBrooks Davis state& state_ref;
404b2c3eb9SBrooks Davis
414b2c3eb9SBrooks Davis /// The depth of the Lua stack to be restored.
424b2c3eb9SBrooks Davis unsigned int original_depth;
434b2c3eb9SBrooks Davis
444b2c3eb9SBrooks Davis /// Constructor.
454b2c3eb9SBrooks Davis ///
464b2c3eb9SBrooks Davis /// \param state_ref_ Reference to the Lua state.
474b2c3eb9SBrooks Davis /// \param original_depth_ The depth of the Lua stack.
impllutok::stack_cleaner::impl484b2c3eb9SBrooks Davis impl(state& state_ref_, const unsigned int original_depth_) :
494b2c3eb9SBrooks Davis state_ref(state_ref_),
504b2c3eb9SBrooks Davis original_depth(original_depth_)
514b2c3eb9SBrooks Davis {
524b2c3eb9SBrooks Davis }
534b2c3eb9SBrooks Davis };
544b2c3eb9SBrooks Davis
554b2c3eb9SBrooks Davis
564b2c3eb9SBrooks Davis /// Creates a new stack cleaner.
574b2c3eb9SBrooks Davis ///
584b2c3eb9SBrooks Davis /// This gathers the current height of the stack so that extra elements can be
594b2c3eb9SBrooks Davis /// popped during destruction.
604b2c3eb9SBrooks Davis ///
614b2c3eb9SBrooks Davis /// \param state_ The Lua state.
stack_cleaner(state & state_)624b2c3eb9SBrooks Davis lutok::stack_cleaner::stack_cleaner(state& state_) :
634b2c3eb9SBrooks Davis _pimpl(new impl(state_, state_.get_top()))
644b2c3eb9SBrooks Davis {
654b2c3eb9SBrooks Davis }
664b2c3eb9SBrooks Davis
674b2c3eb9SBrooks Davis
684b2c3eb9SBrooks Davis /// Pops any values from the stack not known at construction time.
694b2c3eb9SBrooks Davis ///
704b2c3eb9SBrooks Davis /// \pre The current height of the stack must be equal or greater to the height
714b2c3eb9SBrooks Davis /// of the stack when this object was instantiated.
~stack_cleaner(void)724b2c3eb9SBrooks Davis lutok::stack_cleaner::~stack_cleaner(void)
734b2c3eb9SBrooks Davis {
744b2c3eb9SBrooks Davis const unsigned int current_depth = _pimpl->state_ref.get_top();
754b2c3eb9SBrooks Davis assert(current_depth >= _pimpl->original_depth);
764b2c3eb9SBrooks Davis const unsigned int diff = current_depth - _pimpl->original_depth;
774b2c3eb9SBrooks Davis if (diff > 0)
784b2c3eb9SBrooks Davis _pimpl->state_ref.pop(diff);
794b2c3eb9SBrooks Davis }
804b2c3eb9SBrooks Davis
814b2c3eb9SBrooks Davis
824b2c3eb9SBrooks Davis /// Forgets about any elements currently in the stack.
834b2c3eb9SBrooks Davis ///
844b2c3eb9SBrooks Davis /// This allows a function to return values on the stack because all the
854b2c3eb9SBrooks Davis /// elements that are currently in the stack will not be touched during
864b2c3eb9SBrooks Davis /// destruction when the function is called.
874b2c3eb9SBrooks Davis void
forget(void)884b2c3eb9SBrooks Davis lutok::stack_cleaner::forget(void)
894b2c3eb9SBrooks Davis {
904b2c3eb9SBrooks Davis _pimpl->original_depth = _pimpl->state_ref.get_top();
914b2c3eb9SBrooks Davis }
92