Last active
August 29, 2015 14:25
-
-
Save hjanetzek/60b43c9749a80d6544a7 to your computer and use it in GitHub Desktop.
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
/* | |
clang++-3.6 -DVARIADIC_WRAPPER -O3 -Wall -Wpedantic -std=c++11 variadic.cpp | |
./a.out | |
objdump -j .rodata -j .text -d -S a.out > a.txt | |
clang++-3.6 -O3 -Wall -Wpedantic -std=c++11 variadic.cpp | |
./a.out | |
objdump -j .rodata -j .text -d -S a.out > b.txt | |
meld a.txt b.txt | |
g++ -DVARIADIC_WRAPPER -O3 -Wall -Wpedantic -std=c++11 variadic.cpp | |
./a.out | |
objdump -j .rodata -j .text -d -S a.out > a.txt | |
g++ -O3 -Wall -Wpedantic -std=c++11 variadic.cpp | |
./a.out | |
objdump -j .rodata -j .text -d -S a.out > b.txt | |
meld a.txt b.txt | |
*/ | |
#include <tuple> | |
#include <iostream> | |
#include <functional> | |
//#define VARIADIC_WRAPPER 1 | |
#ifdef VARIADIC_WRAPPER | |
__attribute__((noinline)) void foo(int x, int y) { | |
std::cout << "A" << x << ":" << y << std::endl; | |
} | |
template<int ...> | |
struct seq {}; | |
template<int N, int ...S> | |
struct gens : gens<N-1, N-1, S...> {}; | |
template<int ...S> | |
struct gens<0, S...>{ | |
typedef seq<S...> type; | |
}; | |
template <typename ...Args> | |
struct State { | |
using Type = std::tuple<Args...>; | |
State(void (*func)(Args...)) | |
: func(func) {} | |
void (*func)(Args...); | |
Type params; | |
void operator()(Args... _param) { | |
auto _params = std::make_tuple(_param...); | |
if (_params != params) { | |
params = _params; | |
call(typename gens<sizeof...(Args)>::type()); | |
} | |
} | |
template<int ...S> | |
void call(seq<S...>) { | |
func(std::get<S>(params) ...); | |
} | |
}; | |
int main(void) { | |
State<int,int> state(foo); | |
state(1,2); | |
state(1,2); | |
state(2,2); | |
} | |
#else | |
__attribute__((noinline)) void baz(int x, int y) { | |
std::cout << "B" << x << ":" << y << std::endl; | |
} | |
template <typename T> | |
class State { | |
public: | |
void init(const typename T::Type& _default) { | |
T::set(_default); | |
m_current = _default; | |
} | |
inline void operator()(const typename T::Type& _value) { | |
if (m_current != _value) { | |
m_current = _value; | |
T::set(m_current); | |
} | |
} | |
private: | |
typename T::Type m_current; | |
}; | |
struct Test { | |
struct Type { | |
int x; int y; | |
bool operator!=(const Type& _other) { return x != _other.x || y != _other.y;} | |
}; | |
inline static void set(const Type& _type) { | |
baz(_type.x, _type.y); | |
} | |
}; | |
int main(void) { | |
State<Test> state; | |
state({1,2}); | |
state({1,2}); | |
state({2,2}); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment