xref: /minix3/external/bsd/lutok/dist/examples/raii.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2012 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc /// \file examples/raii.cpp
30*11be35a1SLionel Sambuc /// Demonstrates how RAII helps in keeping the Lua state consistent.
31*11be35a1SLionel Sambuc ///
32*11be35a1SLionel Sambuc /// One of the major complains that is raised against the Lua C API is that it
33*11be35a1SLionel Sambuc /// is very hard to ensure it remains consistent during the execution of the
34*11be35a1SLionel Sambuc /// program.  In the case of native C code, there exist many tools that help the
35*11be35a1SLionel Sambuc /// developer catch memory leaks, access to uninitialized variables, etc.
36*11be35a1SLionel Sambuc /// However, when using the Lua C API, none of these tools can validate that,
37*11be35a1SLionel Sambuc /// for example, the Lua stack remains balanced across calls.
38*11be35a1SLionel Sambuc ///
39*11be35a1SLionel Sambuc /// Enter RAII.  The RAII pattern, intensively applied by Lutok, helps the
40*11be35a1SLionel Sambuc /// developer in maintaining the Lua state consistent at all times in a
41*11be35a1SLionel Sambuc /// transparent manner.  This example program attempts to illustrate this.
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc #include <cassert>
44*11be35a1SLionel Sambuc #include <cstdlib>
45*11be35a1SLionel Sambuc #include <iostream>
46*11be35a1SLionel Sambuc #include <string>
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc #include <lutok/operations.hpp>
49*11be35a1SLionel Sambuc #include <lutok/stack_cleaner.hpp>
50*11be35a1SLionel Sambuc #include <lutok/state.ipp>
51*11be35a1SLionel Sambuc 
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc /// Prints the string-typed field of a table.
54*11be35a1SLionel Sambuc ///
55*11be35a1SLionel Sambuc /// If the field contains a string, this function prints its value.  If the
56*11be35a1SLionel Sambuc /// field contains any other type, this prints an error message.
57*11be35a1SLionel Sambuc ///
58*11be35a1SLionel Sambuc /// \pre The top of the Lua stack in 'state' references a table.
59*11be35a1SLionel Sambuc ///
60*11be35a1SLionel Sambuc /// \param state The Lua state.
61*11be35a1SLionel Sambuc /// \param field The name of the string-typed field.
62*11be35a1SLionel Sambuc static void
print_table_field(lutok::state & state,const std::string & field)63*11be35a1SLionel Sambuc print_table_field(lutok::state& state, const std::string& field)
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc     assert(state.is_table());
66*11be35a1SLionel Sambuc 
67*11be35a1SLionel Sambuc     // Bring in some RAII magic: the stack_cleaner object captures the current
68*11be35a1SLionel Sambuc     // height of the Lua stack at this point.  Whenever the object goes out of
69*11be35a1SLionel Sambuc     // scope, it will pop as many entries from the stack as necessary to restore
70*11be35a1SLionel Sambuc     // the stack to its previous level.
71*11be35a1SLionel Sambuc     //
72*11be35a1SLionel Sambuc     // This ensures that, no matter how we exit the function, we do not leak
73*11be35a1SLionel Sambuc     // objects in the stack.
74*11be35a1SLionel Sambuc     lutok::stack_cleaner cleaner(state);
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc     // Stack contents: -1: table.
77*11be35a1SLionel Sambuc     state.push_string(field);
78*11be35a1SLionel Sambuc     // Stack contents: -2: table, -1: field name.
79*11be35a1SLionel Sambuc     state.get_table();
80*11be35a1SLionel Sambuc     // Stack contents: -2: table, -1: field value.
81*11be35a1SLionel Sambuc 
82*11be35a1SLionel Sambuc     if (!state.is_string()) {
83*11be35a1SLionel Sambuc         std::cout << "The field " << field << " does not contain a string\n";
84*11be35a1SLionel Sambuc         // Stack contents: -2: table, -1: field value.
85*11be35a1SLionel Sambuc         //
86*11be35a1SLionel Sambuc         // This is different than when we started!  We should pop our extra
87*11be35a1SLionel Sambuc         // value from the stack at this point.  However, it is extremely common
88*11be35a1SLionel Sambuc         // for software to have bugs (in this case, leaks) in error paths,
89*11be35a1SLionel Sambuc         // mostly because such code paths are rarely exercised.
90*11be35a1SLionel Sambuc         //
91*11be35a1SLionel Sambuc         // By using the stack_cleaner object, we can be confident that the Lua
92*11be35a1SLionel Sambuc         // stack will be cleared for us at this point, no matter what happened
93*11be35a1SLionel Sambuc         // earlier on the stack nor how we exit the function.
94*11be35a1SLionel Sambuc         return;
95*11be35a1SLionel Sambuc     }
96*11be35a1SLionel Sambuc 
97*11be35a1SLionel Sambuc     std::cout << "String in field " << field << ": " << state.to_string()
98*11be35a1SLionel Sambuc               << '\n';
99*11be35a1SLionel Sambuc     // A well-behaved program explicitly pops anything extra from the stack to
100*11be35a1SLionel Sambuc     // return it to its original state.  Mostly for clarity.
101*11be35a1SLionel Sambuc     state.pop(1);
102*11be35a1SLionel Sambuc 
103*11be35a1SLionel Sambuc     // Stack contents: -1: table.  Same as when we started.
104*11be35a1SLionel Sambuc }
105*11be35a1SLionel Sambuc 
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc /// Program's entry point.
108*11be35a1SLionel Sambuc ///
109*11be35a1SLionel Sambuc /// \return A system exit code.
110*11be35a1SLionel Sambuc int
main(void)111*11be35a1SLionel Sambuc main(void)
112*11be35a1SLionel Sambuc {
113*11be35a1SLionel Sambuc     lutok::state state;
114*11be35a1SLionel Sambuc     state.open_base();
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc     lutok::do_string(state, "example = {foo='hello', bar=123, baz='bye'}");
117*11be35a1SLionel Sambuc 
118*11be35a1SLionel Sambuc     state.get_global("example");
119*11be35a1SLionel Sambuc     print_table_field(state, "foo");
120*11be35a1SLionel Sambuc     print_table_field(state, "bar");
121*11be35a1SLionel Sambuc     print_table_field(state, "baz");
122*11be35a1SLionel Sambuc     state.pop(1);
123*11be35a1SLionel Sambuc 
124*11be35a1SLionel Sambuc     return EXIT_SUCCESS;
125*11be35a1SLionel Sambuc }
126