Skip to content

Instantly share code, notes, and snippets.

@jchnkl
Last active December 21, 2015 01:39
Show Gist options
  • Select an option

  • Save jchnkl/6229315 to your computer and use it in GitHub Desktop.

Select an option

Save jchnkl/6229315 to your computer and use it in GitHub Desktop.
C++11 wrapper for XCB request-reply functions using variadic templates. Compilable example.
// g++ $(pkg-config --libs --cflags xcb) -std=c++11 -Wall xpp.cpp
#include <memory>
#include <xcb/xcb.h>
namespace xpp {
template<typename ... ARGS>
class Request {
public:
template<typename COOKIE, typename REPLY,
COOKIE (*GET_COOKIE)(xcb_connection_t *, ARGS ...)>,
REPLY *(*GET_REPLY)(xcb_connection_t *, COOKIE, xcb_generic_error_t **)>
class Reply {
public:
Reply(xcb_connection_t * c, ARGS ... args) : m_c(c)
{
m_cookie = GET_COOKIE(m_c, args...);
}
const REPLY * const operator*(void)
{
if (! m_reply) {
m_reply = std::shared_ptr<REPLY>(GET_REPLY(m_c, m_cookie, NULL));
}
return m_reply.get();
}
const REPLY * const operator->(void)
{
return *(*this);
}
private:
xcb_connection_t * m_c;
COOKIE m_cookie;
std::shared_ptr<REPLY> m_reply;
};
};
};
int main(int argc, char ** argv)
{
xcb_connection_t * c;
// ..
xpp::Request<uint8_t, uint16_t, const char *>
::Reply<xcb_intern_atom_cookie_t, xcb_intern_atom_reply_t,
&xcb_intern_atom, &xcb_intern_atom_reply>
reply(c, 0, 6, "foobar");
xcb_atom_t atom = (*reply)->atom;
atom = reply->atom;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment