Created
April 7, 2017 21:46
-
-
Save uliwitness/95f167249c1658d3f1026b60eff409f5 to your computer and use it in GitHub Desktop.
Proof-of-concept for how one could implement Objective C's target/action paradigm in a static fashion in C++. Basically, the GeneratedWindow class would be what your UI builder (like Interface Builder) generates. Button would be in your application framework. ViewController and the main() function would be how you'd then use them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.cpp | |
// CppTargetActionOutlets | |
// | |
// Created by Uli Kusterer on 07.04.17. | |
// Copyright © 2017 Uli Kusterer. All rights reserved. | |
// | |
#include <iostream> | |
#include <functional> | |
using namespace std; | |
using namespace std::placeholders; | |
class Button | |
{ | |
public: | |
void DoPush() { if( action ) action(this); } | |
std::function<void(Button*)> action; | |
}; | |
class ViewController | |
{ | |
public: | |
void DoOKButton( Button* inButton ) { cout << "Button clicked." << endl; } | |
}; | |
class GeneratedWindow | |
{ | |
public: | |
GeneratedWindow( ViewController* inOwner ) { mButton.action = std::bind(&ViewController::DoOKButton, inOwner, _1); } | |
Button mButton; | |
}; | |
int main( int argc, const char * argv[] ) | |
{ | |
ViewController theController; | |
GeneratedWindow theWindow( &theController ); | |
theWindow.mButton.DoPush(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment