aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/fef/objectstore.h
blob: 7ba082841115f138b7b4d2a7e8ed94feaa458053 (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
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/vespalib/stllike/hash_map.h>

namespace search::fef {

/**
 * Top level interface for things to store in an IObjectStore.
 */
class Anything
{
public:
   using UP = std::unique_ptr<Anything>;
   virtual ~Anything() { }
};

/**
 * Implementation of the Anything interface that wraps a value of the given type.
 */
template<typename T>
class AnyWrapper : public Anything
{
public:
    AnyWrapper(T value) : _value(std::move(value)) { }
    const T & getValue() const { return _value; }
    static const T & getValue(const Anything & any) { return static_cast<const AnyWrapper &>(any).getValue(); }
private:
    T _value;
};

/**
 * Interface for a key value store of Anything instances.
 */
class IObjectStore
{
public:
    virtual ~IObjectStore() { }
    virtual void add(const vespalib::string & key, Anything::UP value) = 0;
    virtual const Anything * get(const vespalib::string & key) const = 0;
};

/**
 * Object store implementation on top of a hash map.
 */
class ObjectStore : public IObjectStore
{
public:
    ObjectStore();
    ~ObjectStore();
    void add(const vespalib::string & key, Anything::UP value) override;
    const Anything * get(const vespalib::string & key) const override;
private:
    using ObjectMap = vespalib::hash_map<vespalib::string, Anything *>;
    ObjectMap _objectMap;
};

namespace objectstore {

/**
 * Utility function that gets the value stored in an Anything instance (via AnyWrapper).
 */
template<typename T>
const T &
as_value(const Anything &val) {
    using WrapperType = AnyWrapper<T>;
    const auto *wrapper = dynamic_cast<const WrapperType *>(&val);
    return wrapper->getValue();
}

}

}