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 namespace o3tl
24 {
25     /** Update output object lazily
26 
27         This template collects data in input type, and updates the
28         output type with the given update functor, but only if the
29         output is requested. Useful if updating is expensive, or input
30         changes frequently, but output is only comparatively seldom
31         used.
32 
33         @example
34         <pre>
35 LazyUpdate<InType,OutType,decltype(F)> myValue(F);
36 *myValue = newInput;
37 myValue->updateInput( this, that, those );
38 
39 output( *myValue );
40         </pre>
41         or
42         <pre>
43 output( myValue.getOutValue() );
44         </pre>
45         if the compiler does not recognize the const context.
46      */
47     template<typename In, typename Out, typename Func> class LazyUpdate {
48     public:
LazyUpdate(Func const & func)49         LazyUpdate(Func const & func): func_(func), input_(), dirty_(true) {}
50 
getInValue() const51         In const & getInValue() const { return input_; }
52 
getOutValue() const53         Out const & getOutValue() const { return update(); }
54 
operator *()55         In & operator *() {
56             dirty_ = true;
57             return input_;
58         }
59 
operator ->()60         In * operator ->() {
61             dirty_ = true;
62             return &input_;
63         }
64 
operator *() const65         Out const & operator *() const { return update();  }
66 
operator ->() const67         Out const * operator ->() const { return &update(); }
68 
69     private:
update() const70         Out const & update() const {
71             if (dirty_) {
72                 output_ = func_(input_);
73                 dirty_ = false;
74             }
75             return output_;
76         }
77 
78         Func const func_;
79         In input_;
80         mutable Out output_;
81         mutable bool dirty_;
82     };
83 }
84 
85 #endif
86 
87 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
88