aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/vespa/vdslib/state/nodetype.cpp
blob: 1f29644ff80de39153367bf8ae970ed4c8631ff3 (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.

#include "nodetype.h"
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <cassert>

namespace storage::lib {

// WARNING: Because static initialization happen in random order, the State
// class use these enum values directly during static initialization since
// these objects may yet not exist. Update in State if you change this.
const NodeType NodeType::STORAGE("storage", NodeType::Type::STORAGE);
const NodeType NodeType::DISTRIBUTOR("distributor", NodeType::Type::DISTRIBUTOR);

const NodeType&
NodeType::get(vespalib::stringref serialized)
{
    if (serialized == STORAGE._name) {
        return STORAGE;
    }
    if (serialized == DISTRIBUTOR._name) {
        return DISTRIBUTOR;
    }
    throw vespalib::IllegalArgumentException(
            "Unknown node type " + serialized + " given.", VESPA_STRLOC);
}

const NodeType&
NodeType::get(Type type) noexcept
{
    switch (type) {
        case Type::STORAGE:
            return STORAGE;
        case Type::DISTRIBUTOR:
            return DISTRIBUTOR;
        case Type::UNKNOWN:
            assert(type != Type::UNKNOWN);
    }
    abort();
}

NodeType::NodeType(vespalib::stringref name, Type type) noexcept
    : _type(type), _name(name)
{
}

std::ostream & operator << (std::ostream & os, const NodeType & n) {
    return os << n.toString();
}

vespalib::asciistream & operator << (vespalib::asciistream & os, const NodeType & n) {
    return os << n.toString();
}

}