LeechCraft 0.6.70-17335-ge406ffdcaf
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
lazyinitializer.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <functional>
12#include <optional>
13
14namespace LC
15{
16namespace Util
17{
28 template<typename Source, typename Object>
30 {
31 Source Source_;
32
33 std::optional<Object> Object_;
34
35 std::function<Object (Source)> Initializer_;
36 std::function<void (Source&)> ClearSource_;
37 public:
49 template<typename Init>
50 LazyInitializer (const Source& source,
51 const Init& initializer,
52 const std::function<void (Source&)>& clear = [] (Source& src) { src = Source {}; })
53 : Source_ { source }
54 , Initializer_ { initializer }
55 , ClearSource_ { clear }
56 {
57 }
58
63 LazyInitializer (const Object& object)
64 : Object_ { object }
65 {
66 }
67
73 LazyInitializer& operator= (const Object& object)
74 {
75 Object_ = object;
76 ClearSource_ (Source_);
77 return *this;
78 }
79
83 operator Object ()
84 {
85 CheckInit ();
86 return *Object_;
87 }
88
91 Object& operator-> ()
92 {
93 CheckInit ();
94 return *Object_;
95 }
96
97 void Force ()
98 {
99 CheckInit ();
100 }
101 private:
102 void CheckInit ()
103 {
104 if (!Object_)
105 {
106 Object_ = Initializer_ (Source_);
107 ClearSource_ (Source_);
108 }
109 }
110 };
111}
112}
Object & operator->()
Indirection operator, forcing object construction.
LazyInitializer(const Source &source, const Init &initializer, const std::function< void(Source &)> &clear=[](Source &src) { src=Source {};})
Constructs an unevaluated lazy initializer.
LazyInitializer & operator=(const Object &object)
Assigns an object to this lazy (making it evaluated) initializer and clears the source.
LazyInitializer(const Object &object)
Constructs an evaluated initializer from the object.
constexpr auto Init(List< Args... >)
Definition typelist.h:89
Definition constants.h:15