quite a bit of refactoring. mostly formatting and renaming, slight amount of cleanup.
This commit is contained in:
1 parent
3b66e243ae
commit
9043e38ef2
23 files changed
+1856
-1416
No files matched your search
@@ -1,20 +1,21 @@
|
||||
|
||||
#ifndef __Signal2_h__
|
||||
#define __Signal2_h__
|
||||
#ifndef __signal2_h__
|
||||
#define __signal2_h__
|
||||
|
||||
#include "horrible_cast.h"
|
||||
#include "slot_base.h"
|
||||
#include "signal_base.h"
|
||||
|
||||
|
||||
//=========================================================================================================================================
|
||||
// Slot tempplate.
|
||||
//=========================================================================================================================================
|
||||
/**
|
||||
* Abstract base class for Slots with 2 parameter(s).
|
||||
*/
|
||||
|
||||
//
|
||||
// classes slot2, slot2_function, and slot2_method
|
||||
//
|
||||
|
||||
//
|
||||
// Abstract base class for slots with 2 parameter(s).
|
||||
//
|
||||
template<typename T_return, typename T_arg0, typename T_arg1>
|
||||
class Slot2 : public slot_base
|
||||
class slot2 : public slot_base
|
||||
{
|
||||
public:
|
||||
/**
|
||||
@@ -25,23 +26,34 @@ class Slot2 : public slot_base
|
||||
virtual T_return operator() (T_arg0 a0, T_arg1 a1) const = 0;
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// a concrete Slot class for a normal function
|
||||
// A concrete slot class for a normal function.
|
||||
//
|
||||
template <typename T_return, typename T_arg0, typename T_arg1>
|
||||
class Slot2_function: public Slot2<T_return, T_arg0, T_arg1>
|
||||
class slot2_function: public slot2<T_return, T_arg0, T_arg1>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef T_return (*FUNCTION_POINTER)(T_arg0 a0, T_arg1 a1);
|
||||
typedef T_return (*FUNCTION_POINTER)(T_arg0 a0, T_arg1 a1); // convenience typedef
|
||||
|
||||
Slot2_function( const FUNCTION_POINTER func )
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Converts a function pointer into raw data.
|
||||
*/
|
||||
slot2_function( const FUNCTION_POINTER func )
|
||||
{
|
||||
// convert and store function pointer in slot_base
|
||||
this->data[0] = safe_horrible_cast<slot_base::data_container>(func);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform callback.
|
||||
*
|
||||
* Converts raw data back into a function pointer, and calls it.
|
||||
*
|
||||
* @return Returns an object of type T_return; the result of the user callback.
|
||||
*/
|
||||
T_return operator() (T_arg0 a0, T_arg1 a1) const
|
||||
{
|
||||
// retrieve data from slot_base and convert back to a function pointer
|
||||
@@ -52,22 +64,33 @@ class Slot2_function: public Slot2<T_return, T_arg0, T_arg1>
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// a concrete Slot class for an object pointer and method (usually 'this' and 'Class::Method')
|
||||
// A concrete slot class for an object pointer and method (usually 'this' and 'Class::Method').
|
||||
//
|
||||
template <class T_object, typename T_member, typename T_return, typename T_arg0, typename T_arg1>
|
||||
class Slot2_method: public Slot2<T_return, T_arg0, T_arg1>
|
||||
class slot2_method: public slot2<T_return, T_arg0, T_arg1>
|
||||
{
|
||||
public:
|
||||
|
||||
Slot2_method(const T_object* p_object, const T_member p_member)
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Converts an object and method pointer into raw data.
|
||||
*/
|
||||
slot2_method(const T_object* p_object, const T_member p_member)
|
||||
{
|
||||
// convert and store object pointer and member function pointer in slot_base
|
||||
this->data[0] = safe_horrible_cast<slot_base::data_container>(p_object);
|
||||
this->data[1] = safe_horrible_cast<slot_base::data_container>(p_member);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform callback.
|
||||
*
|
||||
* Converts raw data back into a method pointer, and calls it.
|
||||
*
|
||||
* @return Returns an object of type T_return; the result of the user callback.
|
||||
*/
|
||||
T_return operator() (T_arg0 a0, T_arg1 a1) const
|
||||
{
|
||||
// retrieve data from slot_base and convert back to an object pointer and member function pointer
|
||||
@@ -79,103 +102,113 @@ class Slot2_method: public Slot2<T_return, T_arg0, T_arg1>
|
||||
}
|
||||
};
|
||||
|
||||
//=========================================================================================================================================
|
||||
// Signal tempplate.
|
||||
//=========================================================================================================================================
|
||||
|
||||
|
||||
//
|
||||
// class signal2
|
||||
//
|
||||
template< typename T_return, typename T_arg0, typename T_arg1 >
|
||||
class Signal2: public signal_base
|
||||
class signal2: public signal_base
|
||||
{
|
||||
public:
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
// Execute the Signal:
|
||||
// - Walk the list of Slot nodes.
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
/**
|
||||
* Emit the signal.
|
||||
*
|
||||
* Iterate through the list of slots, and call each one.
|
||||
*/
|
||||
void operator() (T_arg0 a0, T_arg1 a1)
|
||||
{
|
||||
if( IsSignalEmitting( ) )
|
||||
// if we're already emitting, return (i.e. no re-entrancy allowed)
|
||||
if( signal_base::emitting )
|
||||
{
|
||||
#if defined( _DEBUG )
|
||||
printf("WARNING: Signal2<...> @ 0x%p recursive Emit attempt\n", this);
|
||||
printf("WARNING: signal2<...> @ 0x%p recursive emit attempt\n", this);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
BeginEmit( );
|
||||
|
||||
list_node* cur = GetFirstNode( );
|
||||
// set emitting flag
|
||||
signal_base::emitting = true;
|
||||
|
||||
// iterate through the list
|
||||
signal_base::list_node* cur = signal_base::head;
|
||||
while( cur )
|
||||
{
|
||||
// make the slot_base pointer a Slot2 pointer
|
||||
Slot2<T_return, T_arg0, T_arg1>* s = static_cast<Slot2<T_return, T_arg0, T_arg1>*>(cur->slot);
|
||||
// cast the slot_base pointer to a slot2 pointer (converts from raw memory to pointer again)
|
||||
slot2<T_return, T_arg0, T_arg1>* s = static_cast<slot2<T_return, T_arg0, T_arg1>*>(cur->slot);
|
||||
|
||||
// call the Signal
|
||||
(*s)(a0, a1);
|
||||
// call the signal as long as it's still valid
|
||||
if ( !cur->deleted )
|
||||
{
|
||||
(*s)(a0, a1);
|
||||
}
|
||||
|
||||
// move to next Slot in the list.
|
||||
// move to next slot in the list.
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
EndEmit( );
|
||||
// delete any pending slot deletions
|
||||
signal_base::remove_pending_nodes( );
|
||||
|
||||
// clear emitting flag
|
||||
signal_base::emitting = false;
|
||||
}
|
||||
|
||||
//=========================================================================================================================================
|
||||
// Signal connections
|
||||
//=========================================================================================================================================
|
||||
|
||||
//
|
||||
// signal connections to slots
|
||||
//
|
||||
|
||||
// convenience typedef
|
||||
typedef T_return (*FUNCTION_POINTER)(T_arg0 a0, T_arg1 a1);
|
||||
|
||||
// Functions
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
// connect to normal functions
|
||||
bool Connect( FUNCTION_POINTER func )
|
||||
{
|
||||
// make a copy of the Slot to store in our list
|
||||
Slot2<T_return, T_arg0, T_arg1>* sNewFunc = new Slot2_function<T_return, T_arg0, T_arg1>( func );
|
||||
// make a copy of the slot to store in our list
|
||||
slot2<T_return, T_arg0, T_arg1>* sNewFunc = new slot2_function<T_return, T_arg0, T_arg1>( func );
|
||||
|
||||
// add it to the end of our linked list
|
||||
return bind(sNewFunc);
|
||||
return signal_base::bind(sNewFunc);
|
||||
}
|
||||
|
||||
// Object methods
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
template <class T_object, typename T_member>
|
||||
// connect to an object's method
|
||||
template <class T_object, typename T_member>
|
||||
bool Connect(T_object* p_object, T_member p_member)
|
||||
{
|
||||
// make a copy of the Slot to store in our list
|
||||
Slot2<T_return, T_arg0, T_arg1>* sNewMethod = new Slot2_method<T_object,T_member,T_return, T_arg0, T_arg1>( p_object, p_member );
|
||||
// make a copy of the slot to store in our list
|
||||
slot2<T_return, T_arg0, T_arg1>* sNewMethod = new slot2_method<T_object,T_member,T_return, T_arg0, T_arg1>( p_object, p_member );
|
||||
|
||||
// add it to the end of our linked list
|
||||
return bind(sNewMethod);
|
||||
return signal_base::bind(sNewMethod);
|
||||
}
|
||||
|
||||
|
||||
//=========================================================================================================================================
|
||||
// Signal Disconnections
|
||||
//=========================================================================================================================================
|
||||
// Functions
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// signal disconnections from slots
|
||||
//
|
||||
|
||||
// disconnect from a normal function
|
||||
bool Disconnect( FUNCTION_POINTER func )
|
||||
{
|
||||
// make a temporary Slot we can use to find a match in the list
|
||||
Slot2_function<T_return, T_arg0, T_arg1> sTest( func );
|
||||
// make a temporary slot we can use to find a match in the list
|
||||
slot2_function<T_return, T_arg0, T_arg1> sTest( func );
|
||||
|
||||
// search and remove it
|
||||
return unbind(&sTest);
|
||||
return signal_base::unbind(&sTest);
|
||||
}
|
||||
|
||||
|
||||
// Object method
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
template <class T_object, typename T_member>
|
||||
// disconnect from an object's method
|
||||
template <class T_object, typename T_member>
|
||||
bool Disconnect(T_object* p_object, T_member p_member)
|
||||
{
|
||||
// make a temporary Slot we can use to find a match in the list
|
||||
Slot2_method<T_object,T_member,T_return, T_arg0, T_arg1> sTemp(p_object,p_member);
|
||||
// make a temporary slot we can use to find a match in the list
|
||||
slot2_method<T_object,T_member,T_return, T_arg0, T_arg1> sTemp(p_object,p_member);
|
||||
|
||||
// search and remove it
|
||||
return unbind(&sTemp);
|
||||
return signal_base::unbind(&sTemp);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // __Signal2_h__
|
||||
#endif // __signal2_h__
|
||||
Reference in new issue
Block a user