aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/vespa/vdslib/state/nodetype.h
blob: 1b1b4ec5aa7578c7b55f340d7a661581cd22bad9 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @class vdslib::NodeType
 *
 * Sets what type of node we're talking about. This class exist so we don't need
 * to duplicate all functions for storage and distributor nodes in states, and
 * to avoid using a bool for type. This also makes it more easily expandable
 * with other node types.
 */
#pragma once

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

namespace vespalib {
    class asciistream;
}

namespace storage::lib {

class NodeType  {
public:
    NodeType(const NodeType &) = delete;
    NodeType & operator = (const NodeType &) = delete;
    NodeType(NodeType &&) = delete;
    NodeType & operator =(NodeType &&) = delete;
    enum class Type : uint8_t {STORAGE = 0, DISTRIBUTOR = 1, UNKNOWN = 2};
    static const NodeType DISTRIBUTOR;
    static const NodeType STORAGE;

    /** Throws vespalib::IllegalArgumentException if invalid state given. */
    static const NodeType& get(vespalib::stringref serialized);
    static const NodeType& get(Type type) noexcept;
    const vespalib::string& serialize() const noexcept { return _name; }

    Type getType() const noexcept { return _type; }
    operator uint16_t() const noexcept { return static_cast<uint16_t>(_type); }

    const vespalib::string & toString() const noexcept { return _name; }
    bool operator==(const NodeType& other) const noexcept { return (&other == this); }
    bool operator!=(const NodeType& other) const noexcept { return (&other != this); }

    bool operator<(const NodeType& other) const noexcept {
        return (&other == this ? false : *this == NodeType::DISTRIBUTOR);
    }
private:
    Type             _type;
    vespalib::string _name;

    NodeType(vespalib::stringref name, Type type) noexcept;
};

std::ostream & operator << (std::ostream & os, const NodeType & n);
vespalib::asciistream & operator << (vespalib::asciistream & os, const NodeType & n);

}