File indexing completed on 2024-05-19 05:29:33

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 template<typename F>
0014 class lazy_val
0015 {
0016 public:
0017     lazy_val(F f)
0018         : _f(std::forward<F>(f))
0019         , valueRetrieved(false)
0020     {
0021     }
0022 
0023 private:
0024     F _f;
0025     mutable decltype(_f()) value;
0026     mutable bool valueRetrieved;
0027 
0028 public:
0029     operator decltype(_f())() const
0030     {
0031         if (!valueRetrieved) {
0032             valueRetrieved = true;
0033             value = _f();
0034         }
0035 
0036         return value;
0037     }
0038 };
0039 
0040 template<typename F>
0041 inline lazy_val<F> make_lazy_val(F &&f)
0042 {
0043     return lazy_val<F>(std::forward<F>(f));
0044 }
0045 
0046 } // namespace utils
0047 } // namespace kamd