Skip to content

Instantly share code, notes, and snippets.

@Newlifer
Created October 22, 2014 19:34
Show Gist options
  • Save Newlifer/9797deae7c7da9e71f31 to your computer and use it in GitHub Desktop.
Save Newlifer/9797deae7c7da9e71f31 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <functional>
#include <utility>
#include <array>
#include <map>
using namespace std;
template< typename F, typename Arg >
struct PartialApp {
F f;
Arg arg;
constexpr PartialApp( F&& f, Arg&& arg )
: f( forward< F >( f ) ), arg( forward< Arg >( arg ) )
{
}
template< typename ... Args >
constexpr auto operator() ( Args&& ...args )
{
return f( arg, forward< Args >( args )... );
}
};
template< typename F, typename A >
constexpr PartialApp< F, A > partial( F&& f, A&& a )
{
return PartialApp< F, A> ( forward< F >( f ), forward< A >( a ) );
}
template< typename F, typename A, typename B >
constexpr auto partial( F&& f, A&& a, B&& b )
{
return partial( partial( forward< F >( f) , forward< A >( a ) ), forward< B >( b ) );
}
struct ImageSource {
static void load( int a, int b, int c, int d, int e )
{
cout << a << b << c << d << e;
}
};
struct ImageLoader {
template< typename F, typename T >
void load_p( F f, T value )
{
f( value );
}
template< typename F, typename T, typename... Args >
void load_p( F f, T value, Args... args )
{
load_p( partial(f, value), args...);
}
template< typename T >
void load( std::string source, T value )
{
load_p( &mSources[ source ].load, value );
}
template< typename T, typename... Args >
void load( std::string source, T value, Args... args )
{
load_p( &mSources[ source ].load, value, args... );
}
std::map< std::string, ImageSource > mSources;
};
int main()
{
ImageLoader s;
ImageSource source;
s.mSources[ "default" ] = source;
s.load( "default", 2, 3, 4, 5, 6 );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment