1
#pragma once
2

            
3
#include <functional>
4
#include <map>
5
#include <memory>
6

            
7
#include "envoy/common/matchers.h"
8
#include "envoy/common/pure.h"
9

            
10
#include "source/common/common/non_copyable.h"
11
#include "source/common/protobuf/protobuf.h"
12

            
13
namespace Envoy {
14
namespace Server {
15

            
16
/**
17
 * ConfigTracker is used by the `/config_dump` admin endpoint to manage storage of config-providing
18
 * callbacks with weak ownership semantics. Callbacks added to ConfigTracker only live as long as
19
 * the returned EntryOwner object (or ConfigTracker itself, if shorter). Keys should be descriptors
20
 * of the configs provided by the corresponding callback. They must be unique.
21
 * ConfigTracker is *not* threadsafe.
22
 */
23
class ConfigTracker {
24
public:
25
  // The passed StringMatcher will be matched against the `name` field of whatever
26
  // proto is returned.
27
  using Cb = std::function<ProtobufTypes::MessagePtr(const Matchers::StringMatcher&)>;
28
  using CbsMap = std::map<std::string, Cb>;
29

            
30
  /**
31
   * EntryOwner supplies RAII semantics for entries in the map.
32
   * The entry is not removed until the EntryOwner or the ConfigTracker itself is destroyed,
33
   * whichever happens first. When you add() an entry, you must hold onto the returned
34
   * owner object for as long as you want the entry to stay in the map.
35
   */
36
  class EntryOwner {
37
  public:
38
103543
    virtual ~EntryOwner() = default;
39

            
40
  protected:
41
103543
    EntryOwner() = default; // A sly way to make this class "abstract."
42
  };
43
  using EntryOwnerPtr = std::unique_ptr<EntryOwner>;
44

            
45
89848
  virtual ~ConfigTracker() = default;
46

            
47
  /**
48
   * @return const CbsMap& The map of string keys to tracked callbacks.
49
   */
50
  virtual const CbsMap& getCallbacksMap() const PURE;
51

            
52
  /**
53
   * Add a new callback to the map under the given key
54
   * @param key the map key for the new callback.
55
   * @param cb the callback to add. *must not* return nullptr.
56
   * @return EntryOwnerPtr the new entry's owner object. nullptr if the key is already present.
57
   */
58
  virtual EntryOwnerPtr add(const std::string& key, Cb cb) PURE;
59
};
60

            
61
} // namespace Server
62
} // namespace Envoy