File indexing completed on 2024-05-12 05:29:23

0001 /*
0002  *   SPDX-FileCopyrightText: 2015-2016 Ivan Cukic <ivan.cukic@kde.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #pragma once
0008 
0009 namespace kamd
0010 {
0011 namespace utils
0012 {
0013 struct none_t {
0014 };
0015 inline const none_t none()
0016 {
0017     return none_t();
0018 }
0019 
0020 // A simple implementation of the optional class
0021 // until we can rely on std::optional.
0022 // It is not going to come close in the supported
0023 // features to the std one.
0024 // (we need it in the core library, so we don't
0025 // want to use boost.optional)
0026 template<typename T>
0027 class optional_view
0028 {
0029 public:
0030     explicit optional_view(const T &value)
0031         : m_value(&value)
0032     {
0033     }
0034 
0035     optional_view(const none_t &)
0036         : m_value(nullptr)
0037     {
0038     }
0039 
0040     bool is_initialized() const
0041     {
0042         return m_value != nullptr;
0043     }
0044 
0045     const T &get() const
0046     {
0047         return *m_value;
0048     }
0049 
0050     const T *operator->() const
0051     {
0052         return m_value;
0053     }
0054 
0055 private:
0056     const T *const m_value;
0057 };
0058 
0059 template<typename T>
0060 optional_view<T> make_optional_view(const T &value)
0061 {
0062     return optional_view<T>(value);
0063 }
0064 
0065 } // namespace utils
0066 } // namespace kamd