LibreOffice Module o3tl (master)  1
lazy_update.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_O3TL_LAZY_UPDATE_HXX
21 #define INCLUDED_O3TL_LAZY_UPDATE_HXX
22 
23 #include <utility>
24 
25 namespace o3tl
26 {
49  template<typename In, typename Out, typename Func> class LazyUpdate {
50  public:
51  LazyUpdate(Func const & func): func_(func), input_(), dirty_(true) {}
52 
53  In const & getInValue() const { return input_; }
54 
55  Out const & getOutValue() const { return update(); }
56 
57  In & operator *() {
58  dirty_ = true;
59  return input_;
60  }
61 
63  dirty_ = true;
64  return &input_;
65  }
66 
67  Out const & operator *() const { return update(); }
68 
69  Out const * operator ->() const { return &update(); }
70 
71  private:
72  Out const & update() const {
73  if (dirty_) {
74  output_ = func_(input_);
75  dirty_ = false;
76  }
77  return output_;
78  }
79 
80  Func const func_;
82  mutable Out output_;
83  mutable bool dirty_;
84  };
85 }
86 
87 #endif
88 
89 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
LazyUpdate(Func const &func)
Definition: lazy_update.hxx:51
Func const func_
Definition: lazy_update.hxx:80
In const & getInValue() const
Definition: lazy_update.hxx:53
Out const & update() const
Definition: lazy_update.hxx:72
Writer & Out(const SwAttrFnTab pTab, const SfxPoolItem &rHt, Writer &rWrt)
Out const & getOutValue() const
Definition: lazy_update.hxx:55