2004-05-22 02:57:54 +02:00
|
|
|
// EntityProperties.h
|
|
|
|
//
|
2004-07-20 21:30:35 +02:00
|
|
|
// Mark Thompson mot20@cam.ac.uk / mark@wildfiregames.com
|
2004-05-22 02:57:54 +02:00
|
|
|
//
|
|
|
|
// Extended properties table, primarily intended for data-inheritable properties and those defined by JavaScript functions.
|
|
|
|
//
|
2004-08-03 01:14:54 +02:00
|
|
|
// Usage: These properties are accessed via functions in CEntity/CBaseEntity
|
2004-05-22 02:57:54 +02:00
|
|
|
//
|
2004-05-22 01:46:16 +02:00
|
|
|
|
|
|
|
// TODO: Fix the silent failures of the conversion functions: need to work out what to do in these cases.
|
|
|
|
|
|
|
|
// Mark Thompson mot20@cam.ac.uk / mark@wildfiregames.com
|
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
/*
|
2004-05-22 01:46:16 +02:00
|
|
|
#ifndef ENTITY_PROPERTIES_INCLUDED
|
|
|
|
#define ENTITY_PROPERTIES_INCLUDED
|
|
|
|
|
2004-06-02 18:28:12 +02:00
|
|
|
#include "CStr.h"
|
|
|
|
#include "Vector3D.h"
|
2004-10-07 21:23:35 +02:00
|
|
|
#include "ScriptObject.h"
|
|
|
|
|
2004-06-11 00:24:03 +02:00
|
|
|
#include "scripting/JSInterface_Entity.h"
|
|
|
|
#include "scripting/JSInterface_Vector3D.h"
|
2004-05-24 23:01:50 +02:00
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
#if !GCC_VERSION
|
2004-05-24 23:01:50 +02:00
|
|
|
|
2004-05-22 01:46:16 +02:00
|
|
|
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
class IBoundPropertyOwner;
|
2004-06-11 00:24:03 +02:00
|
|
|
class CBaseEntity;
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
// Property interface
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
class IBoundProperty
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
|
|
|
protected:
|
2004-08-03 01:14:54 +02:00
|
|
|
IBoundPropertyOwner* m_owner;
|
|
|
|
void (IBoundPropertyOwner::*m_updateFn)();
|
2004-06-11 00:24:03 +02:00
|
|
|
virtual void set( const jsval value ) = 0;
|
|
|
|
public:
|
2004-08-03 01:14:54 +02:00
|
|
|
void fromjsval( const jsval value );
|
2004-07-20 21:30:35 +02:00
|
|
|
virtual jsval tojsval() = 0;
|
2004-08-03 01:14:54 +02:00
|
|
|
virtual bool rebuild( IBoundProperty* parent, bool triggerFn = true ) = 0; // Returns true if the rebuild changed the value of this property.
|
|
|
|
void associate( IBoundPropertyOwner* owner, const CStrW& name );
|
|
|
|
void associate( IBoundPropertyOwner* owner, const CStrW& name, void (IBoundPropertyOwner::*updateFn)() );
|
2004-10-07 21:23:35 +02:00
|
|
|
virtual ~IBoundProperty() {}
|
2004-06-11 00:24:03 +02:00
|
|
|
};
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
// Specialize at least:
|
|
|
|
// - jsval conversion functions (set, tojsval)
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
// Generic primitive one:
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
template<typename T> class CBoundProperty : public IBoundProperty
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
T m_data;
|
|
|
|
bool m_inherited;
|
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
public:
|
2004-08-03 01:14:54 +02:00
|
|
|
CBoundProperty() { m_inherited = true; }
|
|
|
|
CBoundProperty( const T& copy ) { m_inherited = false; m_data = copy; }
|
|
|
|
operator T&() { return( m_data ); }
|
|
|
|
operator const T&() const { return( m_data ); }
|
|
|
|
T& operator=( const T& copy ) { m_inherited = false; return( m_data = copy ); }
|
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
void set( const jsval value );
|
|
|
|
jsval tojsval();
|
2004-08-03 01:14:54 +02:00
|
|
|
bool rebuild( IBoundProperty* parent, bool triggerFn = true )
|
|
|
|
{
|
|
|
|
if( m_inherited && parent )
|
|
|
|
{
|
|
|
|
*this = *( (CBoundProperty<T>*)parent );
|
|
|
|
if( triggerFn && m_updateFn )
|
|
|
|
(m_owner->*m_updateFn)();
|
|
|
|
}
|
|
|
|
return( !m_inherited );
|
|
|
|
}
|
2004-07-20 21:30:35 +02:00
|
|
|
};
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
// Generic class one:
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
template<typename T> class CBoundObjectProperty : public IBoundProperty, public T
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
bool m_inherited;
|
|
|
|
|
2004-06-11 00:24:03 +02:00
|
|
|
public:
|
2004-08-03 01:14:54 +02:00
|
|
|
CBoundObjectProperty() { m_inherited = true; }
|
|
|
|
CBoundObjectProperty( const T& copy ) : T( copy ) { m_inherited = false; }
|
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
T& operator=( const T& copy )
|
|
|
|
{
|
|
|
|
IBoundPropertyOwner* sv_owner = m_owner;
|
|
|
|
void (IBoundPropertyOwner::*sv_updateFn)() = m_updateFn;
|
|
|
|
|
|
|
|
(T&)*this = copy;
|
|
|
|
|
|
|
|
m_owner = sv_owner;
|
|
|
|
m_updateFn = sv_updateFn;
|
|
|
|
m_inherited = false;
|
|
|
|
|
|
|
|
return( *this );
|
|
|
|
}
|
2004-06-11 00:24:03 +02:00
|
|
|
void set( const jsval value );
|
2004-07-20 21:30:35 +02:00
|
|
|
jsval tojsval();
|
2004-08-03 01:14:54 +02:00
|
|
|
bool rebuild( IBoundProperty* parent, bool triggerFn = true )
|
|
|
|
{
|
|
|
|
if( m_inherited && parent )
|
|
|
|
{
|
2004-10-07 21:23:35 +02:00
|
|
|
// Save some properties so they won't be overwritten
|
|
|
|
IBoundPropertyOwner* sv_owner = m_owner;
|
|
|
|
void (IBoundPropertyOwner::*sv_updateFn)() = m_updateFn;
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
*this = *( (CBoundObjectProperty<T>*)parent );
|
2004-10-07 21:23:35 +02:00
|
|
|
|
|
|
|
m_owner = sv_owner;
|
|
|
|
m_updateFn = sv_updateFn;
|
|
|
|
m_inherited = true;
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
if( triggerFn && m_updateFn )
|
|
|
|
(m_owner->*m_updateFn)();
|
|
|
|
}
|
|
|
|
return( !m_inherited );
|
|
|
|
}
|
2004-06-11 00:24:03 +02:00
|
|
|
};
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
// And an explicit one:
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
template<> class CBoundProperty<CBaseEntity*> : public IBoundProperty
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
CBaseEntity* m_data;
|
|
|
|
|
2004-06-11 00:24:03 +02:00
|
|
|
public:
|
2004-08-03 01:14:54 +02:00
|
|
|
CBoundProperty() { m_data = NULL; }
|
|
|
|
CBoundProperty( CBaseEntity* copy ) { m_data = copy; }
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
operator CBaseEntity*&() { return( m_data ); }
|
|
|
|
operator CBaseEntity*() const { return( m_data ); }
|
|
|
|
CBaseEntity*& operator=( CBaseEntity* copy ) { return( m_data = copy ); }
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
// Standard pointerish things
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
CBaseEntity& operator*() { return( *m_data ); }
|
|
|
|
CBaseEntity* operator->() { return( m_data ); }
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2004-06-11 00:24:03 +02:00
|
|
|
void set( const jsval value );
|
2004-07-20 21:30:35 +02:00
|
|
|
jsval tojsval();
|
2004-08-03 01:14:54 +02:00
|
|
|
bool rebuild( IBoundProperty* parent, bool triggerFn = true )
|
2004-05-22 01:46:16 +02:00
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
return( false ); // Can't inherit a CBaseEntity*
|
2004-06-11 00:24:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
// A jsval property
|
|
|
|
template<> class CBoundProperty<jsval> : public IBoundProperty
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
2004-10-07 21:23:35 +02:00
|
|
|
jsval m_data;
|
|
|
|
bool m_inherited;
|
2004-08-03 01:14:54 +02:00
|
|
|
public:
|
2004-10-07 21:23:35 +02:00
|
|
|
CBoundProperty() { m_data = JSVAL_NULL; m_inherited = true; RootJSVal(); }
|
|
|
|
CBoundProperty( const jsval value ) { set( value ); m_inherited = false; RootJSVal(); }
|
|
|
|
CBoundProperty( CStrW value )
|
|
|
|
{
|
|
|
|
m_data = STRING_TO_JSVAL( JS_NewUCStringCopyZ( g_ScriptingHost.getContext(), value ) );
|
|
|
|
RootJSVal();
|
|
|
|
}
|
|
|
|
~CBoundProperty()
|
|
|
|
{
|
|
|
|
UprootJSVal();
|
|
|
|
}
|
|
|
|
void set( const jsval value ) { UprootJSVal(); m_data = value; m_inherited = false; RootJSVal(); }
|
|
|
|
jsval tojsval() { return( m_data ); }
|
|
|
|
bool rebuild( IBoundProperty* parent, bool triggerFn = true )
|
|
|
|
{
|
|
|
|
if( m_inherited && parent )
|
|
|
|
{
|
|
|
|
UprootJSVal();
|
|
|
|
m_data = ( (CBoundProperty<jsval>*)parent )->m_data;
|
|
|
|
RootJSVal();
|
|
|
|
}
|
|
|
|
return( !m_inherited );
|
|
|
|
}
|
|
|
|
void RootJSVal() { if( JSVAL_IS_GCTHING( m_data ) ) JS_AddRoot( g_ScriptingHost.GetContext(), &m_data ); }
|
|
|
|
void UprootJSVal() { if( JSVAL_IS_GCTHING( m_data ) ) JS_RemoveRoot( g_ScriptingHost.GetContext(), &m_data ); }
|
2004-07-20 21:30:35 +02:00
|
|
|
};
|
|
|
|
|
2004-05-22 01:46:16 +02:00
|
|
|
#endif
|
2004-10-07 21:23:35 +02:00
|
|
|
*/
|