/* SLB - Simple Lua Binder Copyright (C) 2007 Jose L. Hidalgo ValiƱo (PpluX) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Jose L. Hidalgo (www.pplux.com) pplux@pplux.com */ #ifndef __SLB_INSTANCE__ #define __SLB_INSTANCE__ #include "ref_ptr.hpp" #include "Manager.hpp" #include "Debug.hpp" #include "Export.hpp" namespace SLB { class SLB_EXPORT InstanceBase { public: enum Type { I_Invalid = 0x00, I_Copy = 0x01, I_Reference = 0x02, I_Pointer = 0x04, I_Const_Pointer = 0x08, }; // functions to override: virtual void* get_ptr() = 0; virtual const void* get_const_ptr() = 0; // this functions tells ClassInfo to keep a copy of the object // as cache. Turn it off on smart pointers, and that kind of // classes to avoid cyclic dependencies. virtual bool keepCopyAsCache() const { return true; } // constructor: InstanceBase( Type , const std::type_info &); ClassInfo *getClass() { return _class.get(); } bool isCopy() const { return _flags & I_Copy; } bool isConst() const { return (_flags & I_Const_Pointer) != 0; } bool isPointer() const { return (_flags & I_Pointer) || (_flags & I_Const_Pointer); } bool isReference() const { return (_flags & I_Reference) != 0; } virtual ~InstanceBase(); protected: int _flags; ref_ptr _class; }; namespace Instance { struct Default { template class Implementation : public virtual InstanceBase { public: // constructor from a pointer // @fromConstructor is true if this instance was created inside the scripting language calling a // class constructor method. If this instance was returned by a function, thus was not // created inside the script then @fromConstructor = false. Implementation( T* ptr, bool fromConstructor = false ) : InstanceBase( I_Pointer, typeid(T) ), _ptr(ptr) { if (fromConstructor) _flags |= I_Copy; } // constructor from const pointer Implementation( const T *ptr ) : InstanceBase( I_Const_Pointer, typeid(T)), _const_ptr(ptr) { } // constructor from reference Implementation( T &ref ) : InstanceBase( I_Reference, typeid(T) ), _ptr( &ref ) { } // copy constructor, Implementation( const T &ref) : InstanceBase( I_Copy, typeid(T) ), _ptr( 0L ) { _ptr = new T( ref ); } virtual ~Implementation() { if (isCopy()) delete _ptr; } void* get_ptr() { return (isConst())? 0L : _ptr; } const void* get_const_ptr() { return _const_ptr; } protected: union { T *_ptr; const T *_const_ptr; }; }; }; struct NoCopy { template class Implementation : public virtual InstanceBase { public: Implementation( T* ptr, bool fromConstructor = false ) : InstanceBase( I_Pointer, typeid(T) ), _ptr(ptr) { if (fromConstructor) _flags |= I_Copy; } // constructor from const pointer Implementation( const T *ptr ) : InstanceBase( I_Const_Pointer, typeid(T)), _const_ptr(ptr) { } // constructor from reference Implementation( T &ref ) : InstanceBase( I_Reference, typeid(T) ), _ptr( &ref ) { } // copy constructor, Implementation( const T &ref) : InstanceBase( I_Invalid, typeid(T) ), _ptr( 0L ) { } virtual ~Implementation() { if (isCopy()) delete _ptr; } void* get_ptr() { return (isConst())? 0L : _ptr; } const void* get_const_ptr() { return _const_ptr; } protected: union { T *_ptr; const T *_const_ptr; }; }; }; struct NoCopyNoDestroy { template class Implementation : public virtual InstanceBase { public: // constructor form a pointer Implementation( T* ptr, bool fromConstructor) : InstanceBase( I_Pointer, typeid(T) ), _ptr(ptr) { if (fromConstructor) { _flags = I_Invalid; _ptr = 0; } } // constructor from const pointer Implementation( const T *ptr ) : InstanceBase( I_Const_Pointer, typeid(T)), _const_ptr(ptr) { } // constructor from reference Implementation( T &ref ) : InstanceBase( I_Reference, typeid(T) ), _ptr( &ref ) { } // copy constructor, Implementation( const T &) : InstanceBase( I_Invalid, typeid(T) ), _ptr( 0L ) { } virtual ~Implementation() {} void* get_ptr() { return (isConst())? 0L : _ptr; } const void* get_const_ptr() { return _const_ptr; } protected: union { T *_ptr; const T *_const_ptr; }; }; }; template