aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchcommon/common/subscriptionproxyng.h
blob: dd24480f6895ed465cf3cfe94f9aabb1deaf6ab5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/config/helper/legacysubscriber.hpp>

namespace search {

template <typename ME, typename CFG>
class SubscriptionProxyNg : public config::IFetcherCallback<CFG>
{
    typedef void (ME::*Method)(const CFG &cfg);

private:
    ME                       &_target;
    Method                    _method;
    std::unique_ptr<config::LegacySubscriber> _subscriber;
    vespalib::string          _cfgId;

    SubscriptionProxyNg(const SubscriptionProxyNg&);
    SubscriptionProxyNg &operator=(const SubscriptionProxyNg&);

public:
    SubscriptionProxyNg(ME &target, Method method)
        : _target(target),
          _method(method),
          _subscriber(),
          _cfgId("")
    {
    }
    virtual ~SubscriptionProxyNg() {
        unsubscribe();
    }
    const char *getConfigId() const {
        return _cfgId.c_str();
    }
    void subscribe(const char *configId) {
        if (_subscriber) {
            if (configId != nullptr && strcmp(configId, _subscriber->id().c_str()) == 0)
            {
                return; // same id; ignore
            } else {
                unsubscribe();
            }
        }
        if (configId != nullptr && configId[0] != '\0') {
            _cfgId = configId;
            _subscriber = std::make_unique<config::LegacySubscriber>();
            _subscriber->subscribe<CFG>(configId, this);
        }
    }
    void unsubscribe() {
        _subscriber.reset();
        _cfgId = "";
    }
    void configure(std::unique_ptr<CFG> cfg) override {
        (_target.*_method)(*cfg);
    }
};

} // namespace search