File indexing completed on 2024-05-12 16:59:21
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 #ifndef UTILS_OPTIONAL_VIEW_H 0008 #define UTILS_OPTIONAL_VIEW_H 0009 0010 namespace kamd 0011 { 0012 namespace utils 0013 { 0014 struct none_t { 0015 }; 0016 inline const none_t none() 0017 { 0018 return none_t(); 0019 } 0020 0021 // A simple implementation of the optional class 0022 // until we can rely on std::optional. 0023 // It is not going to come close in the supported 0024 // features to the std one. 0025 // (we need it in the core library, so we don't 0026 // want to use boost.optional) 0027 template<typename T> 0028 class optional_view 0029 { 0030 public: 0031 explicit optional_view(const T &value) 0032 : m_value(&value) 0033 { 0034 } 0035 0036 optional_view(const none_t &) 0037 : m_value(nullptr) 0038 { 0039 } 0040 0041 bool is_initialized() const 0042 { 0043 return m_value != nullptr; 0044 } 0045 0046 const T &get() const 0047 { 0048 return *m_value; 0049 } 0050 0051 const T *operator->() const 0052 { 0053 return m_value; 0054 } 0055 0056 private: 0057 const T *const m_value; 0058 }; 0059 0060 template<typename T> 0061 optional_view<T> make_optional_view(const T &value) 0062 { 0063 return optional_view<T>(value); 0064 } 0065 0066 } // namespace utils 0067 } // namespace kamd 0068 0069 #endif // UTILS_OPTIONAL_VIEW_H