paludis  Version 2.6.0
deferred_construction_ptr.hh
1 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
2 
3 /*
4  * Copyright (c) 2008, 2009 Ciaran McCreesh
5  *
6  * This file is part of the Paludis package manager. Paludis is free software;
7  * you can redistribute it and/or modify it under the terms of the GNU General
8  * Public License version 2, as published by the Free Software Foundation.
9  *
10  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17  * Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_DEFERRED_CONSTRUCTION_PTR_HH
21 #define PALUDIS_GUARD_PALUDIS_UTIL_DEFERRED_CONSTRUCTION_PTR_HH 1
22 
23 #include <paludis/util/deferred_construction_ptr-fwd.hh>
24 #include <functional>
25 
26 namespace paludis
27 {
28  template <typename T_>
29  class DeferredConstructionPtr
30  {
31  private:
32  mutable T_ _ptr;
33  std::function<T_ ()> _f;
34  mutable bool _done;
35 
36  public:
37  DeferredConstructionPtr(const std::function<T_ ()> & f) :
38  _ptr(),
39  _f(f),
40  _done(false)
41  {
42  }
43 
44  DeferredConstructionPtr(const DeferredConstructionPtr & other) :
45  _ptr(other._ptr),
46  _f(other._f),
47  _done(other._done)
48  {
49  }
50 
51  DeferredConstructionPtr &
52  operator= (const DeferredConstructionPtr & other)
53  {
54  if (this != &other)
55  {
56  _ptr = other._ptr;
57  _f = other._f;
58  _done = other._done;
59  }
60  return *this;
61  }
62 
63  T_ operator-> () const
64  {
65  if (! _done)
66  {
67  _ptr = _f();
68  _done = true;
69  }
70 
71  return _ptr;
72  }
73 
74  T_ value() const
75  {
76  return operator-> ();
77  }
78  };
79 }
80 
81 #endif
Definition: about_metadata-fwd.hh:23