1a1563752Sjmmv // Copyright 2011 Google Inc. 2a1563752Sjmmv // All rights reserved. 3a1563752Sjmmv // 4a1563752Sjmmv // Redistribution and use in source and binary forms, with or without 5a1563752Sjmmv // modification, are permitted provided that the following conditions are 6a1563752Sjmmv // met: 7a1563752Sjmmv // 8a1563752Sjmmv // * Redistributions of source code must retain the above copyright 9a1563752Sjmmv // notice, this list of conditions and the following disclaimer. 10a1563752Sjmmv // * Redistributions in binary form must reproduce the above copyright 11a1563752Sjmmv // notice, this list of conditions and the following disclaimer in the 12a1563752Sjmmv // documentation and/or other materials provided with the distribution. 13a1563752Sjmmv // * Neither the name of Google Inc. nor the names of its contributors 14a1563752Sjmmv // may be used to endorse or promote products derived from this software 15a1563752Sjmmv // without specific prior written permission. 16a1563752Sjmmv // 17a1563752Sjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18a1563752Sjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19a1563752Sjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20a1563752Sjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21a1563752Sjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22a1563752Sjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23a1563752Sjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24a1563752Sjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25a1563752Sjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26a1563752Sjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27a1563752Sjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28a1563752Sjmmv 29a1563752Sjmmv /// \file stack_cleaner.hpp 30a1563752Sjmmv /// Provides the stack_cleaner class. 31a1563752Sjmmv 32a1563752Sjmmv #if !defined(LUTOK_STACK_CLEANER_HPP) 33a1563752Sjmmv #define LUTOK_STACK_CLEANER_HPP 34a1563752Sjmmv 35a1563752Sjmmv #include <memory> 36a1563752Sjmmv 37a1563752Sjmmv #include <lutok/state.hpp> 38a1563752Sjmmv 39a1563752Sjmmv namespace lutok { 40a1563752Sjmmv 41a1563752Sjmmv 42a1563752Sjmmv /// A RAII model for values on the Lua stack. 43a1563752Sjmmv /// 44a1563752Sjmmv /// At creation time, the object records the current depth of the Lua stack and, 45a1563752Sjmmv /// during destruction, restores the recorded depth by popping as many stack 46a1563752Sjmmv /// entries as required. As a corollary, the stack can only grow during the 47a1563752Sjmmv /// lifetime of a stack_cleaner object (or shrink, but cannot become shorter 48a1563752Sjmmv /// than the depth recorded at creation time). 49a1563752Sjmmv /// 50a1563752Sjmmv /// Use this class as follows: 51a1563752Sjmmv /// 52a1563752Sjmmv /// state s; 53a1563752Sjmmv /// { 54a1563752Sjmmv /// stack_cleaner cleaner1(s); 55a1563752Sjmmv /// s.push_integer(3); 56a1563752Sjmmv /// s.push_integer(5); 57a1563752Sjmmv /// ... do stuff here ... 58a1563752Sjmmv /// for (...) { 59a1563752Sjmmv /// stack_cleaner cleaner2(s); 60a1563752Sjmmv /// s.load_string("..."); 61a1563752Sjmmv /// s.pcall(0, 1, 0); 62a1563752Sjmmv /// ... do stuff here ... 63a1563752Sjmmv /// } 64a1563752Sjmmv /// // cleaner2 destroyed; the result of pcall is gone. 65a1563752Sjmmv /// } 66a1563752Sjmmv /// // cleaner1 destroyed; the integers 3 and 5 are gone. 67a1563752Sjmmv /// 68a1563752Sjmmv /// You must give a name to the instantiated objects even if they cannot be 69a1563752Sjmmv /// accessed later. Otherwise, the instance will be destroyed right away and 70a1563752Sjmmv /// will not have the desired effect. 71a1563752Sjmmv class stack_cleaner { 72a1563752Sjmmv struct impl; 73a1563752Sjmmv 74a1563752Sjmmv /// Pointer to the shared internal implementation. 75*8665e607Slukem std::unique_ptr< impl > _pimpl; 76a1563752Sjmmv 77a1563752Sjmmv /// Disallow copies. 78a1563752Sjmmv stack_cleaner(const stack_cleaner&); 79a1563752Sjmmv 80a1563752Sjmmv /// Disallow assignment. 81a1563752Sjmmv stack_cleaner& operator=(const stack_cleaner&); 82a1563752Sjmmv 83a1563752Sjmmv public: 84a1563752Sjmmv stack_cleaner(state&); 85a1563752Sjmmv ~stack_cleaner(void); 86a1563752Sjmmv 87a1563752Sjmmv void forget(void); 88a1563752Sjmmv }; 89a1563752Sjmmv 90a1563752Sjmmv 91a1563752Sjmmv } // namespace lutok 92a1563752Sjmmv 93a1563752Sjmmv #endif // !defined(LUTOK_STACK_CLEANER_HPP) 94