docs.html.svn-base
来自「本人找过多个在linux下c++的lua5.1封装库,但很少.luabind已经」· SVN-BASE 代码 · 共 1,535 行 · 第 1/5 页
SVN-BASE
1,535 行
<thead valign="bottom"><tr><th class="head">Source</th><th class="head">Target</th></tr></thead><tbody valign="top"><tr><td><tt class="docutils literal"><span class="pre">holder_type<A></span></tt></td><td><tt class="docutils literal"><span class="pre">holder_type<A></span></tt></td></tr><tr><td><tt class="docutils literal"><span class="pre">holder_type<A</span> <span class="pre">const></span></tt></td><td><tt class="docutils literal"><span class="pre">holder_type<A</span> <span class="pre">const></span></tt></td></tr><tr><td><tt class="docutils literal"><span class="pre">holder_type<A></span> <span class="pre">const&</span></tt></td><td><tt class="docutils literal"><span class="pre">holder_type<A></span></tt></td></tr><tr><td><tt class="docutils literal"><span class="pre">holder_type<A</span> <span class="pre">const></span> <span class="pre">const&</span></tt></td><td><tt class="docutils literal"><span class="pre">holder_type<A</span> <span class="pre">const></span></tt></td></tr></tbody></table></div><p>When using a holder type, it can be useful to know if the pointer is valid(i.e. not null). For example when using std::auto_ptr, the holder will beinvalidated when passed as a parameter to a function. For this purpose thereis a member of all object instances in luabind: <tt class="docutils literal"><span class="pre">__ok</span></tt>.</p><pre class="literal-block">struct X {};void f(std::auto_ptr<X>);module(L)[ class_<X, std::auto_ptr<X> >("X") .def(constructor<>()), def("f", &f)];</pre><pre class="literal-block">Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio> a = X()> f(a)> print a.__okfalse</pre><p>When registering a hierarchy of classes, where all instances are to be heldby a smart pointer, all the classes should have the baseclass' holder type.Like this:</p><pre class="literal-block">module(L)[ class_<base, boost::shared_ptr<base> >("base") .def(constructor<>()), class_<derived, base, <strong>boost::shared_ptr<base></strong> >("base") .def(constructor<>())];</pre><p>Internally, luabind will do the necessary conversions on the raw pointers, whichare first extracted from the holder type.</p></div><div class="section"><h2><a id="splitting-class-registrations" name="splitting-class-registrations">8.8 Splitting class registrations</a></h2><p>In some situations it may be desirable to split a registration of a classacross different compilation units. Partly to save rebuild time when changingin one part of the binding, and in some cases compiler limits may force youto split it. To do this is very simple. Consider the following sample code:</p><pre class="literal-block">void register_part1(class_<X>& x){ x.def(/*...*/);}void register_part2(class_<X>& x){ x.def(/*...*/);}void register_(lua_State* L){ class_<X> x("x"); register_part1(x); register_part2(x); module(L) [ x ];}</pre><p>Here, the class <tt class="docutils literal"><span class="pre">X</span></tt> is registered in two steps. The two functions<tt class="docutils literal"><span class="pre">register_part1</span></tt> and <tt class="docutils literal"><span class="pre">register_part2</span></tt> may be put in separate compilationunits.</p><p>To separate the module registration and the classes to be registered, see<a class="reference" href="#splitting-up-the-registration">Splitting up the registration</a>.</p></div></div><div class="section"><h1><a id="object" name="object">9 Object</a></h1><p>Since functions have to be able to take Lua values (of variable type) we need awrapper around them. This wrapper is called <tt class="docutils literal"><span class="pre">luabind::object</span></tt>. If thefunction you register takes an object, it will match any Lua value. To use it,you need to include <tt class="docutils literal"><span class="pre"><luabind/object.hpp></span></tt>.</p><div class="topic"><p class="topic-title first">Synopsis</p><pre class="literal-block">class object{public: template<class T> object(lua_State*, T const& value); object(from_stack const&); object(object const&); object(); ~object(); lua_State* interpreter() const; void push() const; bool is_valid() const; operator <em>safe_bool_type</em> () const; template<class Key> <em>implementation-defined</em> operator[](Key const&); template<class T> object& operator=(T const&); object& operator=(object const&); bool operator==(object const&) const; bool operator<(object const&) const; bool operator<=(object const&) const; bool operator>(object const&) const; bool operator>=(object const&) const; bool operator!=(object const&) const; template <class T> <em>implementation-defined</em> operator[](T const& key) const void swap(object&); <em>implementation-defined</em> operator()(); template<class A0> <em>implementation-defined</em> operator()(A0 const& a0); template<class A0, class A1> <em>implementation-defined</em> operator()(A0 const& a0, A1 const& a1); /* ... */};</pre></div><p>When you have a Lua object, you can assign it a new value with the assignmentoperator (=). When you do this, the <tt class="docutils literal"><span class="pre">default_policy</span></tt> will be used to make theconversion from C++ value to Lua. If your <tt class="docutils literal"><span class="pre">luabind::object</span></tt> is a table youcan access its members through the operator[] or the <a class="reference" href="#iterators">Iterators</a>. The valuereturned from the operator[] is a proxy object that can be used both forreading and writing values into the table (using operator=).</p><p>Note that it is impossible to know if a Lua value is indexable or not(<tt class="docutils literal"><span class="pre">lua_gettable</span></tt> doesn't fail, it succeeds or crashes). This means that ifyou're trying to index something that cannot be indexed, you're on your own.Lua will call its <tt class="docutils literal"><span class="pre">panic()</span></tt> function. See <a class="reference" href="#lua-panic">lua panic</a>.</p><p>There are also free functions that can be used for indexing the table, see<a class="reference" href="#related-functions">Related functions</a>.</p><p>The constructor that takes a <tt class="docutils literal"><span class="pre">from_stack</span></tt> object is used when you want toinitialize the object with a value from the lua stack. The <tt class="docutils literal"><span class="pre">from_stack</span></tt>type has the following constructor:</p><pre class="literal-block">from_stack(lua_State* L, int index);</pre><p>The index is an ordinary lua stack index, negative values are indexed from thetop of the stack. You use it like this:</p><pre class="literal-block">object o(from_stack(L, -1));</pre><p>This will create the object <tt class="docutils literal"><span class="pre">o</span></tt> and copy the value from the top of the lua stack.</p><p>The <tt class="docutils literal"><span class="pre">interpreter()</span></tt> function returns the Lua state where this object is stored.If you want to manipulate the object with Lua functions directly you can pushit onto the Lua stack by calling <tt class="docutils literal"><span class="pre">push()</span></tt>.</p><p>The operator== will call lua_equal() on the operands and return its result.</p><p>The <tt class="docutils literal"><span class="pre">is_valid()</span></tt> function tells you whether the object has been initializedor not. When created with its default constructor, objects are invalid. To makean object valid, you can assign it a value. If you want to invalidate an objectyou can simply assign it an invalid object.</p><p>The <tt class="docutils literal"><span class="pre">operator</span> <span class="pre">safe_bool_type()</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">is_valid()</span></tt>. This meansthat these snippets are equivalent:</p><pre class="literal-block">object o;// ...if (o){ // ...}...object o;// ...if (o.is_valid()){ // ...}</pre><p>The application operator will call the value as if it was a function. You cangive it any number of parameters (currently the <tt class="docutils literal"><span class="pre">default_policy</span></tt> will be usedfor the conversion). The returned object refers to the return value (currentlyonly one return value is supported). This operator may throw <tt class="docutils literal"><span class="pre">luabind::error</span></tt>if the function call fails. If you want to specify policies to your functioncall, you can use index-operator (operator[]) on the function call, and givethe policies within the [ and ]. Like this:</p><pre class="literal-block">my_function_object( 2 , 8 , new my_complex_structure(6)) [ adopt(_3) ];</pre><p>This tells luabind to make Lua adopt the ownership and responsibility for thepointer passed in to the lua-function.</p><p>It's important that all instances of object have been destructed by the timethe Lua state is closed. The object will keep a pointer to the lua state andrelease its Lua object in its destructor.</p><p>Here's an example of how a function can use a table:</p><pre class="literal-block">void my_function(object const& table){ if (type(table) == LUA_TTABLE) { table["time"] = std::clock(); table["name"] = std::rand() < 500 ? "unusual" : "usual"; std::cout << object_cast<std::string>(table[5]) << "\n"; }}</pre><p>If you take a <tt class="docutils literal"><span class="pre">luabind::object</span></tt> as a parameter to a function, any Lua valuewill match that parameter. That's why we have to make sure it's a table beforewe index into it.</p><pre class="literal-block">std::ostream& operator<<(std::ostream&, object const&);</pre><p>There's a stream operator that makes it possible to print objects or use<tt class="docutils literal"><span class="pre">boost::lexical_cast</span></tt> to convert it to a string. This will use lua's stringconversion function. So if you convert a C++ object with a <tt class="docutils literal"><span class="pre">tostring</span></tt>operator, the stream operator for that type will be used.</p><div class="section"><h2><a id="iterators" name="iterators">9.1 Iterators</a></h2><p>There are two kinds of iterators. The normal iterator that will use the metamethodof the object (if there is any) when the value is retrieved. This iterator is simplycalled <tt class="docutils literal"><span class="pre">luabind::iterator</span></tt>. The other iterator is called <tt class="docutils literal"><span class="pre">luabind::raw_iterator</span></tt>and will bypass the metamethod and give the true contents of the table. They haveidentical interfaces, which implements the <a class="reference" href="http://www.sgi.com/tech/stl/ForwardIterator.html">ForwardIterator</a> concept. Apart fromthe members of standard iterators, they have the following members and constructors:</p><pre class="literal-block">class iterator{ iterator(); iterator(object const&); object key() const; <em>standard iterator members</em>};</pre><p>The constructor that takes a <tt class="docutils literal"><span class="pre">luabind::object</span></tt> is actually a template that can beused with object. Passing an object as the parameter to the iterator willconstruct the iterator to refer to the first element in the object.</p><p>The default constructor will initialize the iterator to the one-past-enditerator. This is used to test for the end of the sequence.</p><p>The value type of the iterator is an implementation defined proxy type whichsupports the same operations as <tt class="docutils literal"><span class="pre">luabind::object</span></tt>. Which means that in mostcases you can just treat it as an ordinary object. The difference is that anyassignments to this proxy will result in the value being inserted at theiterators position, in the table.</p><p>The <tt class="docutils literal"><span class="pre">key()</span></tt> member returns the key used by the iterator when indexing theassociated Lua table.</p><p>An example using iterators:</p><pre class="literal-block">for (iterator i(globals(L)["a"]), end; i != end; ++i){ *i = 1;}</pre><p>The iterator named <tt class="docutils literal"><span class="pre">end</span></tt> will be constructed using the default constructorand hence refer to the end of the sequence. This example will simply iterateover the entries in the global table <tt class="docutils literal"><span class="pre">a</span></tt> and set all its values to 1.</p></div><div class="section"><h2><a id="related-functions" name="related-functions">9.2 Related functions</a></h2><p>There are a couple of functions related to objects and tables.</p><pre class="literal-block">int type(object const&);</pre><p>This function will return the lua type index of the given object.i.e. <tt class="docutils literal"><span class="pre">LUA_TNIL</span></tt>, <tt class="docutils literal"><span class="pre">LUA_TNUMBER</span></tt> etc.</p><pre class="literal-block">template<class T, class K>void settable(object const& o, K const& key, T const& value);template<class K>object gettable(object const& o, K const& key);template<class T, class K>void rawset(object const& o, K const& key, T const& value);template<class K>object rawget(object const& o, K const& key);</pre><p>These functions are used for indexing into tables. <tt class="docutils literal"><span class="pre">settable</span></tt> and <tt class="docutils literal"><span class="pre">gettable</span></tt>translates into calls to <tt class="docutils literal"><span class="pre">lua_settable</span></tt> and <tt class="docutils literal"><span class="pre">lua_gettable</span></tt> respectively. Whichmeans that you could just as well use the index operator of the object.</p><p><tt class="docutils literal"><span class="pre">rawset</span></tt> and <tt class="docutils literal"><span class="pre">rawget</span></tt> will translate into calls to <tt class="docutils literal"><span class="pre">lua_rawset</span></tt> and<tt class="docutils literal"><span class="pre">lua_rawget</span></tt> respectively. So they will bypass any metamethod and giv
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?