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