Update Changelog
[profile/ivi/libgee.git] / gee / lazy.vala
1 /* lazy.vala
2  *
3  * Copyright (C) 2011  Maciej Piechotka
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18  *
19  * Author:
20  *      Maciej Piechotka <uzytkownik2@gmail.com>
21  */
22
23 namespace Gee {
24         public delegate G LazyFunc<G> ();
25 }
26
27 /**
28  * Represents a lazy value. I.e. value that is computed on demand.
29  */
30 public class Gee.Lazy<G> {
31         public Lazy (owned LazyFunc<G> func) {
32                 _func = (owned)func;
33         }
34
35         public Lazy.from_value (G item) {
36                 _value = item;
37         }
38
39         public void eval () {
40                 if (_func != null) {
41                         _value = _func ();
42                         _func = null;
43                 }
44         }
45
46         public new G get () {
47                 eval ();
48                 return _value;
49         }
50
51         public new G value {
52                 get {
53                         eval ();
54                         return _value;
55                 }
56         }
57
58         private LazyFunc<G>? _func;
59         private G? _value;
60 }