vcpkg/toolsrc/include/OptBool.h

49 lines
1.2 KiB
C
Raw Normal View History

2016-09-19 11:50:08 +08:00
#pragma once
2017-02-01 05:31:45 +08:00
#include <string>
#include <map>
2017-04-26 08:01:21 +08:00
namespace vcpkg
2016-09-19 11:50:08 +08:00
{
2017-04-26 08:01:21 +08:00
struct OptBool final
2016-09-19 11:50:08 +08:00
{
2017-04-26 08:01:21 +08:00
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;
2016-09-19 11:50:08 +08:00
};
2017-02-01 05:31:45 +08:00
2017-04-26 08:01:21 +08:00
namespace OptBoolC
{
2017-04-26 08:05:48 +08:00
static constexpr OptBool UNSPECIFIED(OptBool::BackingEnum::UNSPECIFIED);
static constexpr OptBool ENABLED(OptBool::BackingEnum::ENABLED);
static constexpr OptBool DISABLED(OptBool::BackingEnum::DISABLED);
2017-04-26 08:01:21 +08:00
}
2017-02-01 05:31:45 +08:00
2017-04-26 08:01:21 +08:00
template<class T>
OptBool OptBool::from_map(const std::map<T, std::string>& map, const T& key)
2017-02-01 05:31:45 +08:00
{
auto it = map.find(key);
if (it == map.cend())
{
2017-04-26 08:01:21 +08:00
return OptBoolC::UNSPECIFIED;
2017-02-01 05:31:45 +08:00
}
return parse(*it);
}
2016-09-19 11:50:08 +08:00
}