doc tweaks
[luabind.git] / luabind / com_policy.hpp
blob9e1f04986ce84ace0170072cb8f524a4db9be776
1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef LUABIND_COM_POLICY_HPP_INCLUDED
24 #define LUABIND_COM_POLICY_HPP_INCLUDED
26 #include <luabind/config.hpp>
27 #include <luabind/detail/policy.hpp>
28 #include <luabind/detail/implicit_cast.hpp>
30 namespace luabind { namespace detail
32 template<class Direction = lua_to_cpp>
33 struct COM_ptr_converter;
35 template<class T>
36 struct COM_release
38 static void release(void* ptr)
40 T* obj = static_cast<T*>(ptr);
41 obj->Release();
45 template<>
46 struct COM_ptr_converter<cpp_to_lua>
48 template<class T>
49 void apply(lua_State* L, T* ptr)
51 if (ptr == 0)
53 lua_pushnil(L);
54 return;
57 class_registry* registry = class_registry::get_registry(L);
58 class_rep* crep = registry->find_class(LUABIND_TYPEID(T));
60 // if you get caught in this assert you are trying
61 // to use an unregistered type
62 assert(crep && "you are trying to use an unregistered type");
64 // create the struct to hold the object
65 void* obj = lua_newuserdata(L, sizeof(object_rep));
66 new(obj) object_rep(ptr, crep, object_rep::owner, COM_release<T>::release);
68 ptr->AddRef();
70 // set the meta table
71 detail::getref(L, crep->metatable_ref());
72 lua_setmetatable(L, -2);
76 template<int N>
77 struct COM_policy : conversion_policy<N>
79 static void precall(lua_State*, const index_map&) {}
80 static void postcall(lua_State*, const index_map&) {}
82 template<class T, class Direction>
83 struct generate_converter
85 typedef COM_ptr_converter<cpp_to_lua> type;
91 namespace luabind
93 template<int N>
94 detail::policy_cons<detail::COM_policy<N>, detail::null_type>
95 COM_managed(boost::arg<N>) { return detail::policy_cons<detail::COM_policy<N>, detail::null_type>(); }
98 #endif // LUABIND_COM_POLICY_HPP_INCLUDED
101 function(L, "createCar", &createCar, COM_managed(result));