aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/include/OptBool.h
blob: 90655cb7e447229f407322690afb56fb246f6a9a (plain)
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
#pragma once

#include <map>
#include <string>

namespace vcpkg
{
    struct OptBool final
    {
        enum class BackingEnum
        {
            UNSPECIFIED = 0,
            ENABLED,
            DISABLED
        };

        static OptBool parse(const std::string& s);

        template<class T>
        static OptBool from_map(const std::map<T, std::string>& map, const T& key);

        constexpr OptBool() : backing_enum(BackingEnum::UNSPECIFIED) {}
        constexpr explicit OptBool(BackingEnum backing_enum) : backing_enum(backing_enum) {}
        constexpr operator BackingEnum() const { return backing_enum; }

    private:
        BackingEnum backing_enum;
    };

    namespace OptBoolC
    {
        static constexpr OptBool UNSPECIFIED(OptBool::BackingEnum::UNSPECIFIED);
        static constexpr OptBool ENABLED(OptBool::BackingEnum::ENABLED);
        static constexpr OptBool DISABLED(OptBool::BackingEnum::DISABLED);
    }

    template<class T>
    OptBool OptBool::from_map(const std::map<T, std::string>& map, const T& key)
    {
        auto it = map.find(key);
        if (it == map.cend())
        {
            return OptBoolC::UNSPECIFIED;
        }

        return parse(*it);
    }
}