Files
Antonio Frighetto c3480b6f9b CLOption: improve isSet for a cl::opt<T>
A logic issue existed when validating whether a `cl::opt<T>`
was set. This has been addressed by replacing `isDefaultOption`
method with `getNumOccurrences`, as the former one was not
meant to be used for checking if the option was set or not.
Likewise, the default Option itself is written to a `std::string`,
in method `get`; the latter is leveraged by `Invokable`.
2023-02-06 09:37:23 +01:00

74 lines
1.9 KiB
C++

#pragma once
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include <cstddef>
#include <tuple>
#include <utility>
#include "llvm/ADT/StringRef.h"
namespace pipeline {
template<typename T>
class Option {
public:
constexpr Option(const char *Name, T Default) :
Default(std::forward<T>(Default)), Name(Name) {}
using Type = std::decay_t<T>;
T Default;
const char *Name;
};
template<typename T>
concept HasOptions = requires(T) {
{ T::Options };
};
/// TODO: Remove after updating to clang-format with concept support.
struct ClangFormatPleaseDoNotBreakMyCode;
// clang-format off
// clang-format on
namespace detail {
template<typename Invokable, size_t Index>
constexpr auto &getOptionInfo() {
return std::get<Index>(Invokable::Options);
}
template<typename Invokable, size_t Index>
llvm::StringRef getOptionName() {
return getOptionInfo<Invokable, Index>().Name;
}
template<typename T, size_t Index>
using ArgTypeImpl = std::decay_t<decltype(getOptionInfo<T, Index>())>;
template<typename InvokableType, size_t Index>
using OptionTypeImpl = typename ArgTypeImpl<InvokableType, Index>::Type;
template<typename InvokableType, size_t Index>
constexpr bool
IsConstCharPtr = std::is_same_v<OptionTypeImpl<InvokableType, Index>,
const char *>;
template<typename InvokableType, size_t Index>
using OptionType = std::conditional_t<IsConstCharPtr<InvokableType, Index>,
std::string,
OptionTypeImpl<InvokableType, Index>>;
template<typename Invokable, size_t Index>
llvm::StringRef getTypeName() {
return typeNameImpl<ArgTypeImpl<Invokable, Index>>();
}
template<typename InvokableType, size_t Index>
OptionType<InvokableType, Index> getOptionDefault() {
return getOptionInfo<InvokableType, Index>().Default;
}
} // namespace detail
} // namespace pipeline