Skip to content

Commit

Permalink
Finished basic event example
Browse files Browse the repository at this point in the history
  • Loading branch information
gtremper committed Jul 31, 2014
1 parent 8c4be79 commit 0f28ca5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 8 deletions.
20 changes: 14 additions & 6 deletions examples/ContextExample.cpp
Expand Up @@ -19,7 +19,9 @@ class Bar{

int main() {
///////////////////////////
//// GlobalCoreContext ////
// //
// GlobalCoreContext //
// //
///////////////////////////
{
// The CoreContext is the basic unit of organization in Autowriring. They are organized
Expand All @@ -37,7 +39,9 @@ int main() {
}

/////////////////////////////////////////////////
//// Context Creation and Switching Contexts ////
// //
// Context Creation and Switching Contexts //
// //
/////////////////////////////////////////////////
{
// New contexts can be created using the 'Create' factory method on a context.
Expand All @@ -61,9 +65,11 @@ int main() {
// Back in global context
}

//////////////////////////////////////////////////////////
//// Adding members to a context with AutoRequired ////
//////////////////////////////////////////////////////////
///////////////////////////////////////////////////////
// //
// Adding members to a context with AutoRequired //
// //
///////////////////////////////////////////////////////
{
// Switch to a child context
AutoCreateContext ctxt;
Expand All @@ -87,7 +93,9 @@ int main() {
} // 'ctxt' and all members a destroyed when the context goes out of scope

//////////////////////
//// Autowired ////
// //
// Autowired //
// //
//////////////////////
{
{
Expand Down
77 changes: 75 additions & 2 deletions examples/EventExample.cpp
Expand Up @@ -3,12 +3,85 @@
#include <iostream>
#include <memory>

class MyEventReceiver:
/////////////////////////////////////////////////////////////////////
// //
// Autowiring Events //
// //
/////////////////////////////////////////////////////////////////////

// Autowiring events are just function calls on member functions in a context.
// All context members that implement function pointer you "fire" will be called

class MyEvent:
public EventReceiver
{
public:
virtual void myFunction(int) = 0;
};

// Will receive MyEvent::myFunction or FooEvent::myFunction events
class FooEvent:
public MyEvent
{
public:
FooEvent():
m_secret(0)
{}

void myFunction(int i) override {
m_secret = i;
}

int getSecret(){
return m_secret;
}
private:
int m_secret;
};

// Will receive MyEvent::myFunction or BarEvent::myFunction events
class BarEvent:
public MyEvent
{
public:
BarEvent():
m_secret(0)
{}

void myFunction(int i) override {
m_secret = i;
}

int getSecret(){
return m_secret;
}
private:
int m_secret;
};

int main(){
std::cout << "Hello World" << std::endl;
AutoCurrentContext ctxt;

// A context must be initiated before events can be received. Similar to CoreThread
ctxt->Initiate();

// This creates an proxy object that can fire MyEvent::* events
AutoFired<MyEvent> eventFirer;

// Inject receiver types into current context
AutoRequired<FooEvent> foo;
AutoRequired<BarEvent> bar;
std::cout << "Foo should be 0: " << foo->getSecret() << std::endl;
std::cout << "Bar Should be 0: " << bar->getSecret() << std::endl;

// Fire event, this should set m_secret on both foo and bar
eventFirer(&MyEvent::myFunction)(42);
std::cout << "Foo should be 42: " << foo->getSecret() << std::endl;
std::cout << "Bar should be 42: " << bar->getSecret() << std::endl;

// You can also manually fire events on a context with `Invoke`
// Since the function pointer is to `BarEvent`, `FooEvent` won't receive the event
//ctxt->Invoke(&BarEvent::myFunction)(77);
//std::cout << "Foo should be 42: " << foo->getSecret() << std::endl;
//std::cout << "Bar should be 77: " << bar->getSecret() << std::endl;
}

0 comments on commit 0f28ca5

Please sign in to comment.