]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/bit_str.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / common / bit_str.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2016 SUSE LINUX GmbH
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14 #include "common/bit_str.h"
15 #include "common/Formatter.h"
16 #include "include/assert.h"
17 #include "common/debug.h"
18
19 static void _dump_bit_str(
20 uint64_t bits,
21 std::ostream *out,
22 ceph::Formatter *f,
23 std::function<const char*(uint64_t)> func,
24 bool dump_bit_val)
25 {
26 uint64_t b = bits;
27 int cnt = 0;
28 bool outted = false;
29
30 while (b && cnt < 64) {
31 uint64_t r = bits & (1ULL << cnt++);
32 if (r) {
33 if (out) {
34 if (outted)
35 *out << ",";
36 *out << func(r);
37 if (dump_bit_val) {
38 *out << "(" << r << ")";
39 }
40 } else {
41 assert(f != NULL);
42 if (dump_bit_val) {
43 f->dump_stream("bit_flag") << func(r)
44 << "(" << r << ")";
45 } else {
46 f->dump_stream("bit_flag") << func(r);
47 }
48 }
49 outted = true;
50 }
51 b >>= 1;
52 }
53 if (!outted && out)
54 *out << "none";
55 }
56
57 void print_bit_str(
58 uint64_t bits,
59 std::ostream &out,
60 std::function<const char*(uint64_t)> func,
61 bool dump_bit_val)
62 {
63 _dump_bit_str(bits, &out, NULL, func, dump_bit_val);
64 }
65
66 void dump_bit_str(
67 uint64_t bits,
68 ceph::Formatter *f,
69 std::function<const char*(uint64_t)> func,
70 bool dump_bit_val)
71 {
72 _dump_bit_str(bits, NULL, f, func, dump_bit_val);
73 }