2016-02-11 15:09:36 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2016, John Maguire <john.maguire@gmail.com>
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LAZY_H
|
|
|
|
#define LAZY_H
|
|
|
|
|
2016-02-11 14:45:50 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
|
2016-02-11 15:09:36 +01:00
|
|
|
// Helper for lazy initialisation of objects.
|
2020-05-19 08:49:11 +02:00
|
|
|
// Usage1:
|
2016-02-11 15:09:36 +01:00
|
|
|
// Lazy<Foo> my_lazy_object([]() { return new Foo; });
|
2020-05-19 08:49:11 +02:00
|
|
|
// Usage2:
|
|
|
|
// Lazy<Foo> my_lazy_object([]() { return new Foo; },
|
|
|
|
// [](Foo *foo) { delete foo; });
|
|
|
|
// Note: the Lazy::default_deleter just deletes the object. The default
|
|
|
|
// unique_ptr would have a specialization for arrays. The second usage
|
|
|
|
// should be used with arrays.
|
|
|
|
//
|
2016-02-11 14:45:50 +01:00
|
|
|
template <typename T>
|
|
|
|
class Lazy {
|
|
|
|
public:
|
2020-05-19 08:49:11 +02:00
|
|
|
explicit Lazy(std::function<T*()> init,
|
|
|
|
std::function<void(T*)> deleter = Lazy::default_deleter)
|
|
|
|
: init_(init), ptr_(nullptr, deleter) {}
|
2016-02-11 14:45:50 +01:00
|
|
|
|
2016-02-12 17:01:35 +01:00
|
|
|
// Convenience constructor that will lazily default construct the object.
|
2020-05-19 08:49:11 +02:00
|
|
|
Lazy()
|
|
|
|
: init_([]() { return new T; }), ptr_(nullptr, Lazy::default_deleter) {}
|
2016-02-12 17:01:35 +01:00
|
|
|
|
2016-02-11 15:09:36 +01:00
|
|
|
T* get() const {
|
|
|
|
CheckInitialised();
|
2016-02-11 14:45:50 +01:00
|
|
|
return ptr_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
typename std::add_lvalue_reference<T>::type operator*() const {
|
2016-02-11 15:09:36 +01:00
|
|
|
CheckInitialised();
|
2016-02-11 14:45:50 +01:00
|
|
|
return *ptr_;
|
|
|
|
}
|
|
|
|
|
2016-02-11 15:09:36 +01:00
|
|
|
T* operator->() const { return get(); }
|
2016-02-11 14:45:50 +01:00
|
|
|
|
2016-02-11 16:11:31 +01:00
|
|
|
// Returns true if the object is not yet initialised.
|
|
|
|
explicit operator bool() const { return ptr_; }
|
|
|
|
|
|
|
|
// Deletes the underlying object and will re-run the initialisation function
|
|
|
|
// if the object is requested again.
|
|
|
|
void reset() { ptr_.reset(nullptr); }
|
|
|
|
|
2020-05-19 08:49:11 +02:00
|
|
|
static void default_deleter(T* obj) { delete obj; }
|
|
|
|
|
2016-02-11 14:45:50 +01:00
|
|
|
private:
|
2016-02-11 15:09:36 +01:00
|
|
|
void CheckInitialised() const {
|
|
|
|
if (!ptr_) {
|
|
|
|
ptr_.reset(init_());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::function<T*()> init_;
|
2020-05-19 08:49:11 +02:00
|
|
|
mutable std::unique_ptr<T, std::function<void(T*)>> ptr_;
|
2016-02-11 14:45:50 +01:00
|
|
|
};
|
2016-02-11 15:09:36 +01:00
|
|
|
|
|
|
|
#endif // LAZY_H
|