varz_node.h
1 // Copyright 2020, Beeri 15. All rights reserved.
2 // Author: Roman Gershman (romange@gmail.com)
3 //
4 #pragma once
5 
6 #include <functional>
7 #include <string>
8 
9 #include "base/RWSpinLock.h"
10 #include "util/stats/varz_value.h"
11 
12 namespace util {
13 
14 class VarzListNode {
15  public:
16  typedef util::VarzValue AnyValue;
17 
18  explicit VarzListNode(const char* name);
19  virtual ~VarzListNode();
20 
21  // New interface. Func is a function accepting 'const char*' and VarzValue&&.
22  static void Iterate(std::function<void(const char*, AnyValue&&)> f);
23 
24  // Old interface. Appends string representations of each active node in the list to res.
25  // Used for outputting the current state.
26  static void IterateValues(std::function<void(const std::string&, const std::string&)> cb) {
27  Iterate([&](const char* name, AnyValue&& av) { cb(name, Format(av)); });
28  }
29 
30  protected:
31  virtual AnyValue GetData() const = 0;
32 
33  const char* name_;
34 
35  static std::string Format(const AnyValue& av);
36 
37  private:
38  // Returns the head to varz linked list. Note that the list becomes invalid after at least one
39  // linked list node was destroyed.
40  static VarzListNode*& global_list();
41  static folly::RWSpinLock g_varz_lock;
42 
43  VarzListNode* next_;
44  VarzListNode* prev_;
45 };
46 
47 } // namespace util