File indexing completed on 2024-05-19 04:25:09

0001 /*
0002  *  SPDX-FileCopyrightText: 2023 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #ifndef KISLAZYVALUEWRAPPER_H
0007 #define KISLAZYVALUEWRAPPER_H
0008 
0009 #include <functional>
0010 
0011 /**
0012  * A simple wrapper that creates a value from a functor on construction.
0013  * It is not a "lazy value" class in a classic form, because it requires
0014  * to be wrapped into KisLazyStorage to support atomic/thread-safe access.
0015  */
0016 template <typename T>
0017 struct KisLazyValueWrapper
0018 {
0019     using value_type = T;
0020 
0021     KisLazyValueWrapper() = default;
0022 
0023     explicit KisLazyValueWrapper(std::function<value_type()> func) {
0024         value = func();
0025     }
0026 
0027     ~KisLazyValueWrapper() = default;
0028 
0029     operator const value_type&() const {
0030         return value;
0031     }
0032 
0033     KisLazyValueWrapper(const KisLazyValueWrapper&rhs) = delete;
0034     KisLazyValueWrapper& operator=(const KisLazyValueWrapper&rhs) = delete;
0035 
0036     KisLazyValueWrapper(KisLazyValueWrapper&&rhs) = default;
0037     KisLazyValueWrapper& operator=(KisLazyValueWrapper&&rhs) = default;
0038 
0039     value_type value {};
0040 };
0041 
0042 #endif // KISLAZYVALUEWRAPPER_H