aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/vespa/vdslib/container/parameters.h
blob: 854aec4be20c9327afad0f6ac6144aef5145879b (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
92
93
94
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @class storage::api::Parameters
 * @ingroup messageapi
 *
 * @brief A serializable way of setting parameters.
 *
 * Utility class for passing sets of name-value parameter pairs around.
 *
 * @author Fledsbo, Håkon Humberset
 * @date 2004-03-24
 * @version $Id$
 */

#pragma once

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

namespace vespalib { class GrowableByteBuffer; }
namespace document { class ByteBuffer; }

namespace vdslib {

class Parameters : public vespalib::xml::XmlSerializable {
public:
    using KeyT = vespalib::stringref;
    class Value : public vespalib::string
    {
    public:
      Value() { }
      Value(vespalib::stringref s) : vespalib::string(s) { }
      Value(const vespalib::string & s) : vespalib::string(s) { }
      Value(const void *v, size_t sz) : vespalib::string(v, sz) { }
      size_t length() const  { return size() - 1; }
    };
    using ValueRef = vespalib::stringref;
    using ParametersMap = vespalib::hash_map<vespalib::string, Value>;
private:
    ParametersMap _parameters;

    void printXml(vespalib::xml::XmlOutputStream& xos) const override;

public:
    Parameters();
    Parameters(document::ByteBuffer& buffer);
    ~Parameters();

    bool operator==(const Parameters &other) const;

    size_t getSerializedSize() const;

    bool hasValue(KeyT id)                 const { return (_parameters.find(id) != _parameters.end()); }
    unsigned int size()                    const { return _parameters.size(); }
    bool lookup(KeyT id, ValueRef & v) const;
    void set(KeyT id, const void * v, size_t sz) { _parameters[id] = Value(v, sz); }

    void print(std::ostream& out, bool verbose, const std::string& indent) const;
    void serialize(vespalib::GrowableByteBuffer& buffer) const;
    void deserialize(document::ByteBuffer& buffer);

    // Disallow
    ParametersMap::const_iterator begin() const { return _parameters.begin(); }
    ParametersMap::const_iterator end() const { return _parameters.end(); }
    /// Convenience from earlier use.
    void set(KeyT id, vespalib::stringref value) { _parameters[id] = Value(value.data(), value.size()); }
    vespalib::stringref get(KeyT id, vespalib::stringref def = "") const;
    /**
     * Set the value identified by the id given. This requires the type to be
     * convertible by stringstreams.
     *
     * @param id The value to get.
     * @param t The value to save. Will be converted to a string.
     */
    template<typename T>
    void set(KeyT id, T t);

    /**
     * Get the value identified by the id given, as the same type as the default
     * value given. This requires the type to be convertible by stringstreams.
     *
     * @param id The value to get.
     * @param def The value to return if the value does not exist.
     * @return The value represented as the same type as the default given, or
     *         the default itself if value did not exist.
     */
    template<typename T>
    T get(KeyT id, T def) const;

    vespalib::string toString() const;
};

} // vdslib