Created
April 10, 2010 10:53
-
-
Save frsyuki/361966 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
#include <iostream> | |
#include <msgpack.hpp> | |
// source file | |
void doit_impl(void (*func)(void*, const char*, size_t), void* obj) | |
{ | |
for(int i=0; i < 10; ++i) { | |
(*func)(obj, "test", 4); | |
} | |
} | |
// header file | |
template <typename T> | |
static void trampoline(void* obj, const char* data, size_t size) | |
{ | |
reinterpret_cast<T*>(obj)->write(data, size); | |
} | |
template <typename T> | |
void doit(T* stream) | |
{ | |
void (*func)(void*, const char*, size_t) = &trampoline<T>; | |
void* obj = reinterpret_cast<void*>(stream); | |
doit_impl(func, obj); | |
} | |
// user program | |
int main(void) | |
{ | |
doit(&std::cout); // any class that implements T::write(ptr, size) | |
msgpack::sbuffer sbuf; | |
doit(&sbuf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment