Blame lib/option.hpp

rpm-build d2b433
/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode:nil; -*- */
rpm-build d2b433
/*
rpm-build d2b433
 * libopenraw - option.hpp
rpm-build d2b433
 *
rpm-build d2b433
 * Copyright (C) 2017 Hubert Figuière
rpm-build d2b433
 *
rpm-build d2b433
 * This library is free software: you can redistribute it and/or
rpm-build d2b433
 * modify it under the terms of the GNU Lesser General Public License
rpm-build d2b433
 * as published by the Free Software Foundation, either version 3 of
rpm-build d2b433
 * the License, or (at your option) any later version.
rpm-build d2b433
 *
rpm-build d2b433
 * This library is distributed in the hope that it will be useful,
rpm-build d2b433
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build d2b433
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build d2b433
 * Lesser General Public License for more details.
rpm-build d2b433
 *
rpm-build d2b433
 * You should have received a copy of the GNU Lesser General Public
rpm-build d2b433
 * License along with this library.  If not, see
rpm-build d2b433
 * <http://www.gnu.org/licenses/>.
rpm-build d2b433
 */
rpm-build d2b433
rpm-build d2b433
// an option<> template class inspired by Rust
rpm-build d2b433
rpm-build d2b433
#pragma once
rpm-build d2b433
rpm-build d2b433
#include <stdexcept>
rpm-build d2b433
rpm-build d2b433
template<class T>
rpm-build d2b433
class Option
rpm-build d2b433
{
rpm-build d2b433
public:
rpm-build d2b433
  typedef T data_type;
rpm-build d2b433
rpm-build d2b433
  Option()
rpm-build d2b433
    : m_none(true)
rpm-build d2b433
    , m_data()
rpm-build d2b433
  {
rpm-build d2b433
  }
rpm-build d2b433
  Option(T&& data)
rpm-build d2b433
    : m_none(false)
rpm-build d2b433
    , m_data(data)
rpm-build d2b433
  {
rpm-build d2b433
  }
rpm-build d2b433
  Option(const T& data)
rpm-build d2b433
    : m_none(false)
rpm-build d2b433
    , m_data(data)
rpm-build d2b433
  {
rpm-build d2b433
  }
rpm-build d2b433
  template<class... Args>
rpm-build d2b433
  Option(Args&&... args)
rpm-build d2b433
    : m_none(false)
rpm-build d2b433
    , m_data(args...)
rpm-build d2b433
  {
rpm-build d2b433
  }
rpm-build d2b433
rpm-build d2b433
  T&& unwrap()
rpm-build d2b433
  {
rpm-build d2b433
    if (m_none) {
rpm-build d2b433
      throw std::runtime_error("none option value");
rpm-build d2b433
    }
rpm-build d2b433
    m_none = true;
rpm-build d2b433
    return std::move(m_data);
rpm-build d2b433
  }
rpm-build d2b433
  T&& unwrap_or(T&& def)
rpm-build d2b433
  {
rpm-build d2b433
    if (m_none) {
rpm-build d2b433
      return std::move(def);
rpm-build d2b433
    }
rpm-build d2b433
    m_none = true;
rpm-build d2b433
    return std::move(m_data);
rpm-build d2b433
  }
rpm-build d2b433
  bool empty() const
rpm-build d2b433
  { return m_none; }
rpm-build d2b433
  bool ok() const
rpm-build d2b433
  { return !m_none; }
rpm-build d2b433
private:
rpm-build d2b433
  bool m_none;
rpm-build d2b433
  T m_data;
rpm-build d2b433
};