Skip to content

Instantly share code, notes, and snippets.

@jeffomatic
jeffomatic / gist:3281352
Created August 7, 2012 03:54
blog: jl_signal, code: iterating through signal connections
// A sample Emit() method on a signal object for slots that take
// two arguments.
void Emit( TArg1 a1, TArg2 a2 ) const
{
// Here, the iterator is just a pointer to a list node.
// Incrementing the iterator means that we just follow the node's
// "next" pointer. But what happens if the current node is freed
// before the iterator can be incremented?
for ( ConnectionListIter i = m_oConnections.begin();
i.isValid();
@jeffomatic
jeffomatic / gist:3281169
Created August 7, 2012 03:34
blog: jl_signal, code: boy scout principle of signal disconnection
// The "Boy Scout" principle of signal disconnection:
// clean up your own mess!
class ExampleContext
{
void Init()
{
m_pButton = new Button();
m_pWindow = new Window();
m_pButton->ClickSignal()->Connect( m_pWindow, &Window::Open );
}
@jeffomatic
jeffomatic / gist:3280983
Created August 7, 2012 03:06
blog: jl_signal, code: id-based event example
class ExampleObserver : public EventObserver
{
...
virtual void HandleEvent( Event e )
{
switch ( e.id )
{
case eAppEvent_Click:
...
break;
@jeffomatic
jeffomatic / gist:3244608
Created August 3, 2012 05:17
blog: jl_signal, code: signal declarations with macros
JL_SIGNAL() oSignalThatEmitsNoParams;
JL_SIGNAL( int ) oSignalThatEmitsInt;
JL_SIGNAL( const char* const* ) oEmitsConstArraysOfConstStrings;
JL_SIGNAL( unsigned, double, size_t, char**& ) oSignalThatIsComplicated;
JL_SIGNAL( JL_SIGNAL()& ) oSignalThatIsMeta;
@jeffomatic
jeffomatic / gist:3244605
Created August 3, 2012 05:16
blog: jl_signal, code: signal classes using function type template args
typedef jl::Signal< void(int) > SignalInt;
typedef jl::Signal< void(int, char) > SignalIntChar;
@jeffomatic
jeffomatic / gist:3244604
Created August 3, 2012 05:15
blog: jl_signal, code: template with function type parameter
// Forward-declare an empty template class
template< typename SomeFunctionType >
class Signal;
// Specialize the above with a function type and trivially inherit
// from the signal class with the arity in the typename.
template< typename ReturnType, typename P1, typename P2 >
class Signal< ReturnType(P1, P2) > : public Signal2< P1, P2 >
{
// Nothing required in here. But since this is not exactly
@jeffomatic
jeffomatic / gist:3244601
Created August 3, 2012 05:15
blog: jl_signal, code: impossible template syntax
template< typename P1 >
class Signal { ... };
template< typename P1, typename P2 >
class Signal { ... };
typedef Signal< int > SignalInt;
typedef Signal< int, char > SignalIntChar;
@jeffomatic
jeffomatic / gist:3244600
Created August 3, 2012 05:14
blog: jl_signal, code: example of signal classes with arity-based names
template< typename P1 >
class Signal1 { .... };
template< typename P1, typename P2 >
class Signal2 { ... };
...
template< typename P1, typename P2, typename P3, ..., template P8 >
class Signal8 { ... };
@jeffomatic
jeffomatic / gist:3234250
Created August 2, 2012 06:01
blog: jl_signal, code: jQuery event unbinding idiosyncracies
<html>
<body>
<div id="divs"></div>
<br />
<div id="log"></div>
<script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript">
$(function() {
// Build one div for each callback, to display the order of callback dispatch.
@jeffomatic
jeffomatic / gist:3234045
Created August 2, 2012 05:33
blog: jl_signal, code: catastrophic signal connection
JL_SIGNAL() oSignal;
void f1() { oSignal.Disconnect( &f1 ); }
void f2() { /* do something */ }
oSignal.Connect( &f1 );
oSignal.Connect( &f2 );
oSignal.Emit();