1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
2 
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:
9 
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12 
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.
22 
23 
24 #ifndef LUABIND_OBJECT_REP_HPP_INCLUDED
25 #define LUABIND_OBJECT_REP_HPP_INCLUDED
26 
27 #include <boost/aligned_storage.hpp>
28 #include <luabind/config.hpp>
29 #include <luabind/detail/instance_holder.hpp>
30 #include <luabind/detail/ref.hpp>
31 
32 #include <cstdlib>
33 
34 namespace luabind { namespace detail
35 {
36 	class class_rep;
37 
38 	void finalize(lua_State* L, class_rep* crep);
39 
40 	// this class is allocated inside lua for each pointer.
41 	// it contains the actual c++ object-pointer.
42 	// it also tells if it is const or not.
43 	class LUABIND_API object_rep
44 	{
45 	public:
46 		object_rep(instance_holder* instance, class_rep* crep);
47 		~object_rep();
48 
crep() const49 		const class_rep* crep() const { return m_classrep; }
crep()50 		class_rep* crep() { return m_classrep; }
51 
set_instance(instance_holder * instance)52 		void set_instance(instance_holder* instance) { m_instance = instance; }
53 
54 		void add_dependency(lua_State* L, int index);
55         void release_dependency_refs(lua_State* L);
56 
get_instance(class_id target) const57 		std::pair<void*, int> get_instance(class_id target) const
58 		{
59 			if (m_instance == 0)
60 				return std::pair<void*, int>(0, -1);
61 			return m_instance->get(target);
62 		}
63 
is_const() const64 		bool is_const() const
65 		{
66 			return m_instance && m_instance->pointee_const();
67 		}
68 
release()69         void release()
70         {
71             if (m_instance)
72                 m_instance->release();
73         }
74 
allocate(std::size_t size)75 		void* allocate(std::size_t size)
76 		{
77 			if (size <= 32)
78 				return &m_instance_buffer;
79 			return std::malloc(size);
80 		}
81 
deallocate(void * storage)82 		void deallocate(void* storage)
83 		{
84 			if (storage == &m_instance_buffer)
85 				return;
86 			std::free(storage);
87 		}
88 
89 	private:
90 
object_rep(object_rep const &)91 	object_rep(object_rep const&)
92 	{}
93 
operator =(object_rep const &)94 	void operator=(object_rep const&)
95 	{}
96 
97         instance_holder* m_instance;
98         boost::aligned_storage<32> m_instance_buffer;
99 		class_rep* m_classrep; // the class information about this object's type
100         std::size_t m_dependency_cnt; // counts dependencies
101 	};
102 
103 	template<class T>
104 	struct delete_s
105 	{
applyluabind::detail::delete_s106 		static void apply(void* ptr)
107 		{
108 			delete static_cast<T*>(ptr);
109 		}
110 	};
111 
112 	template<class T>
113 	struct destruct_only_s
114 	{
applyluabind::detail::destruct_only_s115 		static void apply(void* ptr)
116 		{
117 			// Removes unreferenced formal parameter warning on VC7.
118 			(void)ptr;
119 #ifndef NDEBUG
120 			int completeness_check[sizeof(T)];
121 			(void)completeness_check;
122 #endif
123 			static_cast<T*>(ptr)->~T();
124 		}
125 	};
126 
127     LUABIND_API object_rep* get_instance(lua_State* L, int index);
128     LUABIND_API void push_instance_metatable(lua_State* L);
129     LUABIND_API object_rep* push_new_instance(lua_State* L, class_rep* cls);
130 
131 }}
132 
133 #endif // LUABIND_OBJECT_REP_HPP_INCLUDED
134 
135