aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <me@robin-krahl.de>2019-01-13 12:04:03 +0100
committerRobin Krahl <me@robin-krahl.de>2019-01-13 12:04:03 +0100
commitd0d934e8333cffe093569b9f48a10b3500d1ff60 (patch)
tree3dcba4099e36611b046a65a09660b7a4888880da
parent6772dcf38e275b2ab9f962a87e86f89541c13a92 (diff)
downloadlibnitrokey-d0d934e8333cffe093569b9f48a10b3500d1ff60.tar.gz
libnitrokey-d0d934e8333cffe093569b9f48a10b3500d1ff60.tar.bz2
Add misc::Option<T> class
Option<T> is a simple replacement for std::optional, which was introduced in C++17. It stores a value that can be present or absent.
-rw-r--r--libnitrokey/misc.h25
1 files changed, 25 insertions, 0 deletions
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 <sstream>
+#include <stdexcept>
#include <iomanip>
namespace nitrokey {
namespace misc {
+/**
+ * Simple replacement for std::optional (C++17).
+ */
+template<typename T>
+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<typename T>
std::string toHex(T value){
using namespace std;