]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/watcher/Utils.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / librbd / watcher / Utils.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_LIBRBD_WATCHER_UTILS_H
5 #define CEPH_LIBRBD_WATCHER_UTILS_H
6
7 #include "include/buffer_fwd.h"
8 #include "include/encoding.h"
9 #include "include/Context.h"
10 #include "librbd/Watcher.h"
11
12 namespace ceph { class Formatter; }
13
14 namespace librbd {
15 namespace watcher {
16 namespace util {
17
18 template <typename Watcher>
19 struct HandlePayloadVisitor : public boost::static_visitor<void> {
20 Watcher *watcher;
21 uint64_t notify_id;
22 uint64_t handle;
23
24 HandlePayloadVisitor(Watcher *watcher_, uint64_t notify_id_,
25 uint64_t handle_)
26 : watcher(watcher_), notify_id(notify_id_), handle(handle_)
27 {
28 }
29
30 template <typename P>
31 inline void operator()(const P &payload) const {
32 typename Watcher::C_NotifyAck *ctx =
33 new typename Watcher::C_NotifyAck(watcher, notify_id, handle);
34 if (watcher->handle_payload(payload, ctx)) {
35 ctx->complete(0);
36 }
37 }
38 };
39
40 class EncodePayloadVisitor : public boost::static_visitor<void> {
41 public:
42 explicit EncodePayloadVisitor(bufferlist &bl) : m_bl(bl) {}
43
44 template <typename P>
45 inline void operator()(const P &payload) const {
46 ::encode(static_cast<uint32_t>(P::NOTIFY_OP), m_bl);
47 payload.encode(m_bl);
48 }
49
50 private:
51 bufferlist &m_bl;
52 };
53
54 class DecodePayloadVisitor : public boost::static_visitor<void> {
55 public:
56 DecodePayloadVisitor(__u8 version, bufferlist::iterator &iter)
57 : m_version(version), m_iter(iter) {}
58
59 template <typename P>
60 inline void operator()(P &payload) const {
61 payload.decode(m_version, m_iter);
62 }
63
64 private:
65 __u8 m_version;
66 bufferlist::iterator &m_iter;
67 };
68
69 } // namespace util
70 } // namespace watcher
71 } // namespace librbd
72
73 #endif // CEPH_LIBRBD_WATCHER_UTILS_H