From d0d934e8333cffe093569b9f48a10b3500d1ff60 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 13 Jan 2019 12:04:03 +0100 Subject: Add misc::Option class Option is a simple replacement for std::optional, which was introduced in C++17. It stores a value that can be present or absent. --- libnitrokey/misc.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/libnitrokey/misc.h b/libnitrokey/misc.h index 88254dd..d10c8df 100644 --- a/libnitrokey/misc.h +++ b/libnitrokey/misc.h @@ -29,12 +29,37 @@ #include "log.h" #include "LibraryException.h" #include +#include #include namespace nitrokey { namespace misc { +/** + * Simple replacement for std::optional (C++17). + */ +template +class Option { +public: + Option() : m_hasValue(false), m_value() {} + Option(T value) : m_hasValue(true), m_value(value) {} + + bool has_value() const { + return m_hasValue; + } + T value() const { + if (!m_hasValue) { + throw std::logic_error("Called Option::value without value"); + } + return m_value; + } + +private: + bool m_hasValue; + T m_value; +}; + template std::string toHex(T value){ using namespace std; -- cgit v1.2.3