Index: /trunk/nv/common.hh
===================================================================
--- /trunk/nv/common.hh	(revision 261)
+++ /trunk/nv/common.hh	(revision 262)
@@ -143,7 +143,4 @@
 namespace nv
 {
-	class object;
-	class root;
-	class type_database;
 	class uid_store;
 
Index: /trunk/nv/io_event.hh
===================================================================
--- /trunk/nv/io_event.hh	(revision 261)
+++ /trunk/nv/io_event.hh	(revision 262)
@@ -15,4 +15,5 @@
 
 #include <nv/common.hh>
+#include <nv/types.hh>
 
 namespace nv
Index: /trunk/nv/lua/lua_dispatch.hh
===================================================================
--- /trunk/nv/lua/lua_dispatch.hh	(revision 261)
+++ /trunk/nv/lua/lua_dispatch.hh	(revision 262)
@@ -147,5 +147,5 @@
 			int object_method_wrapper( lua_State* L ) 
 			{
-				C* c = (C*)to_object( L, 1 );
+				C* c = (C*)to_ref_object( L, 1 );
 				return dispatcher<typename return_type<F>::type>::call( L, 2, *c, f );
 			}
Index: /trunk/nv/lua/lua_object.hh
===================================================================
--- /trunk/nv/lua/lua_object.hh	(revision 262)
+++ /trunk/nv/lua/lua_object.hh	(revision 262)
@@ -0,0 +1,46 @@
+// Copyright (C) 2014 ChaosForge Ltd 
+// http://chaosforge.org/
+//
+// This file is part of NV Libraries.
+// For conditions of distribution and use, see copyright notice in nv.hh
+
+#ifndef NV_LUA_OBJECT_HH
+#define NV_LUA_OBJECT_HH
+
+#include <nv/common.hh>
+#include <nv/lua/lua_values.hh>
+#include <nv/object.hh>
+
+namespace nv
+{
+
+	namespace lua
+	{
+
+		namespace detail
+		{
+
+			template <typename T>
+			struct type_degrade< T*, typename std::enable_if< std::is_base_of<object, T >::value >::type >
+			{
+				typedef object* type;
+			};
+
+			void  push_object  ( lua_State *L, object* o );
+			void* to_object  ( lua_State *L, int index );
+			void* to_object  ( lua_State *L, int index, void* def );
+
+		}
+
+		template <>
+		struct pass_traits<object*>
+		{
+			static void push( lua_State *L, object* o ) { detail::push_object( L, o ); }
+			static object* to( lua_State *L, int index ) { return static_cast< object* >( detail::to_ref_object( L, index ) ); }
+			static object* to( lua_State *L, int index, object* def ) { return static_cast< object* >( detail::to_ref_object( L, index, def ) ); }
+		};
+
+	}
+}
+
+#endif // NV_LUA_OBJECT_HH
Index: /trunk/nv/lua/lua_state.hh
===================================================================
--- /trunk/nv/lua/lua_state.hh	(revision 261)
+++ /trunk/nv/lua/lua_state.hh	(revision 262)
@@ -16,5 +16,4 @@
 #include <map>
 #include <nv/flags.hh>
-#include <nv/types.hh>
 
 #include <nv/lua/lua_path.hh>
@@ -191,5 +190,4 @@
 			explicit state( bool load_libs = false );
 			explicit state( lua_State* state );
-			virtual ~state();
 			bool do_string( const std::string& code, const std::string& name, int rvalues = 0 );
 			bool do_stream( std::istream& stream, const std::string& name );
@@ -198,25 +196,11 @@
 			void log_stack();
 			lua_State* get_raw();
-			reference register_object( object * o );
-			reference register_object( object * o, const char* lua_name );
-			reference register_proto( object * o, const char* storage );
-
-			template< typename TYPE >
-			type_entry& register_type( const char* name )
-			{
-				m_type_database->create_type<TYPE>( name );
-			}
-
-			template< typename TYPE, int SIZE >
-			void register_enum( const char* name, const char* prefix, type_enum (&init_enums)[SIZE] )
-			{
-				m_type_database->create_type<TYPE>( name ).enums( init_enums );
-				register_enum_values( name, prefix );
-			}
-			void register_enum_values( const std::string& name, const std::string& prefix = std::string() );
-
-			void store_metadata( object* o, const std::string& metaname, void* pointer );
-			void unregister_object( object * o ); // DELETE ME
-			void unregister_object( int index );
+			reference register_object( void* o, const char* lua_name );
+			reference register_proto( const char* id, const char* storage );
+			void store_metadata( reference object_index, const std::string& metaname, void* pointer );
+			void unregister_object( reference object_index );
+
+			void register_enum( const char* name, int value );
+
 			void register_native_object_method( const char* lua_name, const char* name, lfunction f );
 			template < typename F, F f >
@@ -226,5 +210,4 @@
 			}
 			operator lua_State*() { return m_state; }
-			type_database* get_type_database() { return m_type_database; }
 		private:
 			int load_string( const std::string& code, const std::string& name );
@@ -233,6 +216,4 @@
 			int do_current( const std::string& name, int rvalues = 0 );
 			void deep_pointer_copy( int index, void* obj );
-		private:
-			type_database* m_type_database;
 		};
 
Index: /trunk/nv/lua/lua_values.hh
===================================================================
--- /trunk/nv/lua/lua_values.hh	(revision 261)
+++ /trunk/nv/lua/lua_values.hh	(revision 262)
@@ -60,24 +60,23 @@
 			void push_string  ( lua_State *L, const std::string& s );
 			void push_cstring ( lua_State *L, const char* s );
-			void push_object  ( lua_State *L, object* o );
 			void push_pointer ( lua_State *L, void* p );
 
-			lunsigned   to_unsigned( lua_State *L, int index );
-			linteger    to_integer ( lua_State *L, int index );
-			lnumber     to_number  ( lua_State *L, int index );
-			bool        to_bool    ( lua_State *L, int index );
-			std::string to_string  ( lua_State *L, int index );
-			const char* to_cstring ( lua_State *L, int index );
-			object*     to_object  ( lua_State *L, int index );
-			void*       to_pointer ( lua_State *L, int index );
-
-			lunsigned   to_unsigned( lua_State *L, int index, lunsigned def );
-			linteger    to_integer ( lua_State *L, int index, linteger def );
-			lnumber     to_number  ( lua_State *L, int index, lnumber def );
-			bool        to_bool    ( lua_State *L, int index, bool def );
-			std::string to_string  ( lua_State *L, int index, const std::string& def );
-			const char* to_cstring ( lua_State *L, int index, const char* def );
-			object*     to_object  ( lua_State *L, int index, object* def );
-			void*       to_pointer ( lua_State *L, int index, void* def );
+			lunsigned   to_unsigned   ( lua_State *L, int index );
+			linteger    to_integer    ( lua_State *L, int index );
+			lnumber     to_number     ( lua_State *L, int index );
+			bool        to_bool       ( lua_State *L, int index );
+			std::string to_string     ( lua_State *L, int index );
+			const char* to_cstring    ( lua_State *L, int index );
+			void*       to_pointer    ( lua_State *L, int index );
+			void*       to_ref_object ( lua_State *L, int index );
+
+			lunsigned   to_unsigned   ( lua_State *L, int index, lunsigned def );
+			linteger    to_integer    ( lua_State *L, int index, linteger def );
+			lnumber     to_number     ( lua_State *L, int index, lnumber def );
+			bool        to_bool       ( lua_State *L, int index, bool def );
+			std::string to_string     ( lua_State *L, int index, const std::string& def );
+			const char* to_cstring    ( lua_State *L, int index, const char* def );
+			void*       to_pointer    ( lua_State *L, int index, void* def );
+			void*       to_ref_object ( lua_State *L, int index, void* def );
 
 			void pop_and_discard( lua_State *L, int count );
@@ -140,12 +139,4 @@
 		};
 
-		template <>
-		struct pass_traits<object*>
-		{
-			static void push( lua_State *L, object* o ) { detail::push_object( L, o ); }
-			static object* to( lua_State *L, int index ) { return detail::to_object( L, index ); }
-			static object* to( lua_State *L, int index, object* def ) { return detail::to_object( L, index, def ); }
-		};
-
 		namespace detail
 		{
@@ -185,10 +176,4 @@
 			{
 				typedef std::string type;
-			};
-
-			template <typename T>
-			struct type_degrade< T*, typename std::enable_if< std::is_base_of<object, T >::value >::type >
-			{
-				typedef object* type;
 			};
 
Index: /trunk/nv/root.hh
===================================================================
--- /trunk/nv/root.hh	(revision 261)
+++ /trunk/nv/root.hh	(revision 262)
@@ -13,4 +13,5 @@
 namespace nv
 {
+
 	/**
 	 * Implements the root of a object tree-like structure.
Index: /trunk/nv/types.hh
===================================================================
--- /trunk/nv/types.hh	(revision 261)
+++ /trunk/nv/types.hh	(revision 262)
@@ -22,4 +22,5 @@
 
 	struct type_entry;
+	class type_database;
 
 	enum type_flag
Index: /trunk/nv/uid.hh
===================================================================
--- /trunk/nv/uid.hh	(revision 261)
+++ /trunk/nv/uid.hh	(revision 262)
@@ -19,4 +19,6 @@
 namespace nv
 {
+	class object;
+
 	class uid_store
 	{
@@ -24,5 +26,5 @@
 		
 		/**
-		 * Creates a new instance of the unique indentifer store.
+		 * Creates a new instance of the unique indentifier store.
 		 */
 		uid_store();
Index: /trunk/src/lua/lua_object.cc
===================================================================
--- /trunk/src/lua/lua_object.cc	(revision 262)
+++ /trunk/src/lua/lua_object.cc	(revision 262)
@@ -0,0 +1,21 @@
+// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
+// http://chaosforge.org/
+//
+// This file is part of NV Libraries.
+// For conditions of distribution and use, see copyright notice in nv.hh
+
+#include "nv/lua/lua_object.hh"
+
+#include "nv/lua/lua_raw.hh"
+
+void nv::lua::detail::push_object  ( lua_State *L, object* o )
+{
+	if ( o == nullptr )
+	{
+		lua_pushnil( L );
+	}
+	else
+	{
+		lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );
+	}
+}
Index: /trunk/src/lua/lua_state.cc
===================================================================
--- /trunk/src/lua/lua_state.cc	(revision 261)
+++ /trunk/src/lua/lua_state.cc	(revision 262)
@@ -11,6 +11,4 @@
 #include "nv/logging.hh"
 #include "nv/string.hh"
-#include "nv/root.hh"
-#include "nv/types.hh"
 
 using namespace nv;
@@ -205,10 +203,10 @@
 // state
 
-lua::state::state( lua_State* state ) : state_wrapper( state, false ), m_type_database( nullptr )
-{
-
-}
-
-lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true ), m_type_database( nullptr )
+lua::state::state( lua_State* state ) : state_wrapper( state, false )
+{
+
+}
+
+lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true )
 {
 	load_lua_library();
@@ -222,5 +220,4 @@
 	if ( load_libs )
 	{
-		m_type_database = new type_database();
 		stack_guard guard( this );
 		static const luaL_Reg lualibs[] =
@@ -331,5 +328,5 @@
 }
 
-lua::reference lua::state::register_object( object * o, const char* lua_name )
+lua::reference lua::state::register_object( void* o, const char* lua_name )
 {
 	if ( o == nullptr ) return ref_none;
@@ -344,5 +341,5 @@
 }
 
-lua::reference lua::state::register_proto( object * o, const char* storage )
+lua::reference lua::state::register_proto( const char* id, const char* storage )
 {
 	stack_guard guard( this );
@@ -352,18 +349,10 @@
 		NV_THROW( runtime_error, std::string( storage ) + " storage not registered!" );
 	}
-	lua_getfield( m_state, -1, o->get_id().c_str() );
+	lua_getfield( m_state, -1, id );
 	if ( lua_isnil( m_state, -1 ) )
 	{
-		NV_THROW( runtime_error, std::string( o->get_id() ) + " not found in " + std::string( storage ) + " storage!" );
+		NV_THROW( runtime_error, std::string( id ) + " not found in " + std::string( storage ) + " storage!" );
 	}
 	return luaL_ref( m_state, LUA_REGISTRYINDEX );
-}
-
-lua::reference lua::state::register_object( object * o )
-{
-	if ( o == nullptr || m_type_database == nullptr ) return ref_none;
-	type_entry* t = m_type_database->get_type(typeid(*o));
-	if ( t == nullptr ) return ref_none;
-	return register_object( o, t->name.c_str() );
 }
 
@@ -380,19 +369,14 @@
 }
 
-void lua::state::unregister_object( object * o )
-{
-	if (!o) return;
-	unregister_object( o->get_lua_index() );
-}
-
-void lua::state::unregister_object( int index )
-{
+void lua::state::unregister_object( reference object_index )
+{
+	if ( object_index == ref_nil ) return;
 	stack_guard guard( this );
-	lua_rawgeti( m_state, LUA_REGISTRYINDEX, index );
+	lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index );
 	lua_pushstring( m_state, "__ptr" );
 	lua_pushboolean( m_state, false );
 	lua_rawset( m_state, -3 );
 	lua_pop( m_state, 1 );
-	luaL_unref( m_state, LUA_REGISTRYINDEX, index );
+	luaL_unref( m_state, LUA_REGISTRYINDEX, object_index );
 }
 
@@ -435,27 +419,9 @@
 }
 
-void lua::state::register_enum_values( const std::string& name, const std::string& prefix /*= std::string() */ )
-{
-	type_entry* et = m_type_database->get_type( name );
-
-	for ( const auto& entry : et->enum_list )
-	{
-		lua_pushinteger( m_state, entry.value );
-		if ( prefix.empty() )
-		{
-			lua_setglobal( m_state, entry.name.c_str() );
-		}
-		else
-		{
-			lua_setglobal( m_state, ( prefix + entry.name ).c_str() );
-		}
-	}
-}
-
-void nv::lua::state::store_metadata( object* o, const std::string& metaname, void* pointer )
-{
-	if (!o) return;
+void nv::lua::state::store_metadata( reference object_index, const std::string& metaname, void* pointer )
+{
+	if ( object_index == ref_nil ) return;
 	stack_guard guard( this );
-	lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
+	lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index );
 	lua_pushstring( m_state, metaname.c_str() );
 	lua_pushlightuserdata( m_state, pointer );
@@ -464,10 +430,8 @@
 }
 
-nv::lua::state::~state()
-{
-	if (m_type_database != nullptr )
-	{
-		delete m_type_database;
-	}
-}
-
+void nv::lua::state::register_enum( const char* name, int value )
+{
+	lua_pushinteger( m_state, value );
+	lua_setglobal( m_state, name );
+}
+
Index: /trunk/src/lua/lua_values.cc
===================================================================
--- /trunk/src/lua/lua_values.cc	(revision 261)
+++ /trunk/src/lua/lua_values.cc	(revision 262)
@@ -8,5 +8,4 @@
 
 #include "nv/lua/lua_raw.hh"
-#include "nv/object.hh"
 
 using nv::lua::linteger;
@@ -71,16 +70,4 @@
 }
 
-void nv::lua::detail::push_object  ( lua_State *L, object* o )
-{
-	if ( o == nullptr )
-	{
-		lua_pushnil( L );
-	}
-	else
-	{
-		lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );
-	}
-}
-
 void nv::lua::detail::push_pointer ( lua_State *L, void* p )
 {
@@ -118,7 +105,12 @@
 }
 
-nv::object* nv::lua::detail::to_object  ( lua_State *L, int index )
+void*       nv::lua::detail::to_pointer ( lua_State *L, int index )
 {
-	object* o = nullptr;
+	return lua_touserdata( L, index );
+}
+
+void* nv::lua::detail::to_ref_object  ( lua_State *L, int index )
+{
+	void* o = nullptr;
 	if ( lua_istable( L , index ) )
 	{
@@ -127,14 +119,9 @@
 		if ( lua_isuserdata( L, -1 ) )
 		{
-			o = static_cast<object*>( lua_touserdata( L, -1 ) );
+			o = lua_touserdata( L, -1 );
 		} 
 		lua_pop( L, 1 );
 	}
 	return o;
-}
-
-void*       nv::lua::detail::to_pointer ( lua_State *L, int index )
-{
-	return lua_touserdata( L, index );
 }
 
@@ -169,7 +156,12 @@
 }
 
-nv::object* nv::lua::detail::to_object  ( lua_State *L, int index, nv::object* def )
+void*       nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )
 {
-	object* o = def;
+	return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def );
+}
+
+void* nv::lua::detail::to_ref_object( lua_State *L, int index, void* def )
+{
+	void* o = def;
 	if ( lua_istable( L , index ) )
 	{
@@ -178,5 +170,5 @@
 		if ( lua_isuserdata( L, -1 ) )
 		{
-			o = static_cast<object*>( lua_touserdata( L, -1 ) );
+			o = lua_touserdata( L, -1 );
 		}
 		lua_pop( L, 1 );
@@ -184,7 +176,2 @@
 	return o;
 }
-
-void*       nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )
-{
-	return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def );
-}
Index: /trunk/src/root.cc
===================================================================
--- /trunk/src/root.cc	(revision 261)
+++ /trunk/src/root.cc	(revision 262)
@@ -24,5 +24,5 @@
 	if ( m_lua_state && o->m_lua_index != lua::ref_none )
 	{
-		m_lua_state->unregister_object( o );
+		m_lua_state->unregister_object( o->m_lua_index );
 	}
 	if ( m_uid_store && o->m_uid != 0 )
@@ -51,5 +51,5 @@
 		if ( storage != nullptr )
 		{
-			o->m_lua_proto_index = m_lua_state->register_proto( o, storage );
+			o->m_lua_proto_index = m_lua_state->register_proto( o->get_id().c_str(), storage );
 		}
 	}
