summaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/storageutil/utils.h
blob: debb7e71ace6dc97092875c70ffc9e20c88f5c8a (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vector>
#include <sstream>

namespace storage {

/**
 * Creates a vector of the given type with one entry in it.
 */
template<class A>
std::vector<A> toVector(A entry) {
    std::vector<A> entries;
    entries.push_back(entry);
    return entries;
};

/**
 * Creates a vector of the given type with two entries in it.
 */
template<class A>
std::vector<A> toVector(A entry, A entry2) {
    std::vector<A> entries;
    entries.push_back(entry);
    entries.push_back(entry2);
    return entries;
};

/**
 * Creates a vector of the given type with two entries in it.
 */
template<class A>
std::vector<A> toVector(A entry, A entry2, A entry3) {
    std::vector<A> entries;
    entries.push_back(entry);
    entries.push_back(entry2);
    entries.push_back(entry3);
    return entries;
};

/**
 * Creates a vector of the given type with two entries in it.
 */
template<class A>
std::vector<A> toVector(A entry, A entry2, A entry3, A entry4) {
    std::vector<A> entries;
    entries.push_back(entry);
    entries.push_back(entry2);
    entries.push_back(entry3);
    entries.push_back(entry4);
    return entries;
};

template<class A>
std::string dumpVector(const std::vector<A>& vec) {
    std::ostringstream ost;
    for (uint32_t i = 0; i < vec.size(); ++i) {
        if (!ost.str().empty()) {
            ost << ",";
        }
        ost << vec[i];
    }

    return ost.str();
}

template<class A>
bool hasItem(const std::vector<A>& vec, A entry) {
    for (uint32_t i = 0; i < vec.size(); ++i) {
        if (vec[i] == entry) {
            return true;
        }
    }

    return false;
}

template<typename T>
struct ConfigReader : public T::Subscriber, public T
{
    T& config; // Alter to inherit T to simplify but kept this for compatability

    ConfigReader(const std::string& configId) : config(*this) {
        T::subscribe(configId, *this);
    }
    void configure(const T& c) { config = c; }
};

}