xref: /trunk/main/o3tl/inc/o3tl/lazy_update.hxx (revision 7ee1d29c)
1b0075c8bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3b0075c8bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4b0075c8bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5b0075c8bSAndrew Rist  * distributed with this work for additional information
6b0075c8bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7b0075c8bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8b0075c8bSAndrew Rist  * "License"); you may not use this file except in compliance
9b0075c8bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10b0075c8bSAndrew Rist  *
11b0075c8bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12b0075c8bSAndrew Rist  *
13b0075c8bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14b0075c8bSAndrew Rist  * software distributed under the License is distributed on an
15b0075c8bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b0075c8bSAndrew Rist  * KIND, either express or implied.  See the License for the
17b0075c8bSAndrew Rist  * specific language governing permissions and limitations
18b0075c8bSAndrew Rist  * under the License.
19b0075c8bSAndrew Rist  *
20b0075c8bSAndrew Rist  *************************************************************/
21b0075c8bSAndrew Rist 
22b0075c8bSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef INCLUDED_O3TL_LAZY_UPDATE_HXX
25cdf0e10cSrcweir #define INCLUDED_O3TL_LAZY_UPDATE_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <sal/types.h>
28cdf0e10cSrcweir #include <boost/function.hpp>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir namespace o3tl
31cdf0e10cSrcweir {
32cdf0e10cSrcweir     /** Update output object lazily
33cdf0e10cSrcweir 
34cdf0e10cSrcweir         This template collects data in input type, and updates the
35cdf0e10cSrcweir         output type with the given update functor, but only if the
36cdf0e10cSrcweir         output is requested. Usef�l if updating is expensive, or input
37cdf0e10cSrcweir         changes frequently, but output is only comparatively seldom
38cdf0e10cSrcweir         used.
39cdf0e10cSrcweir 
40cdf0e10cSrcweir         @example
41cdf0e10cSrcweir         <pre>
42cdf0e10cSrcweir LazyUpdate<InType,OutType,LAZYUPDATE_DIRECT_TAG> myValue;
43cdf0e10cSrcweir *myValue = newInput;
44cdf0e10cSrcweir myValue->updateInput( this, that, those );
45cdf0e10cSrcweir 
46cdf0e10cSrcweir output( *myValue );
47cdf0e10cSrcweir         </pre>
48cdf0e10cSrcweir         or
49cdf0e10cSrcweir         <pre>
50cdf0e10cSrcweir output( myValue.getOutValue() );
51cdf0e10cSrcweir         </pre>
52cdf0e10cSrcweir         if the compiler does not recognize the const context.
53cdf0e10cSrcweir      */
54cdf0e10cSrcweir     template< typename InputType, typename OutputType, typename Tag > class LazyUpdate;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     /// LazyUpdate specialization takes boost::function argument
57cdf0e10cSrcweir     struct LAZYUPDATE_FUNCTOR_TAG       {};
58cdf0e10cSrcweir     /// LazyUpdate specialization takes OutputType (*FunctionType)( InputType const& ) argument
59cdf0e10cSrcweir     struct LAZYUPDATE_FUNCTION_TAG      {};
60cdf0e10cSrcweir     /// LazyUpdate specialization can directly convert, OutputType ctor must take InputType argument
61cdf0e10cSrcweir     struct LAZYUPDATE_DIRECT_TAG  {};
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     // -----------------------------------------------------------------------------------------------------
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     namespace detail
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir         /// @internal
68cdf0e10cSrcweir         template< typename InputType, typename OutputType, typename Functor > class LazyUpdateImpl : private Functor
69cdf0e10cSrcweir         {
70cdf0e10cSrcweir         public:
71cdf0e10cSrcweir             typedef OutputType output_type;
72cdf0e10cSrcweir             typedef InputType  input_type;
73cdf0e10cSrcweir 
LazyUpdateImpl()74cdf0e10cSrcweir             LazyUpdateImpl() :
75cdf0e10cSrcweir                 m_aInput()
76cdf0e10cSrcweir             {}
77cdf0e10cSrcweir 
LazyUpdateImpl(ParamType const & rParm)78cdf0e10cSrcweir             template< typename ParamType > explicit LazyUpdateImpl( ParamType const& rParm ) :
79cdf0e10cSrcweir                 Functor(rParm),
80cdf0e10cSrcweir                 m_aInput()
81cdf0e10cSrcweir             {}
82cdf0e10cSrcweir 
83cdf0e10cSrcweir             enum UnaryConstructorTag{ UNARY_CONSTRUCTOR_TAG };
LazyUpdateImpl(const input_type & rInput,UnaryConstructorTag)84cdf0e10cSrcweir             LazyUpdateImpl( const input_type& rInput, UnaryConstructorTag ) :
85cdf0e10cSrcweir                 m_aInput(rInput)
86cdf0e10cSrcweir             {}
87cdf0e10cSrcweir 
LazyUpdateImpl(ParamType const & rParm,const input_type & rInput)88cdf0e10cSrcweir             template< typename ParamType > LazyUpdateImpl( ParamType const&  rParm,
89cdf0e10cSrcweir                                                            const input_type& rInput ) :
90cdf0e10cSrcweir                 Functor(rParm),
91cdf0e10cSrcweir                 m_aInput(rInput)
92cdf0e10cSrcweir             {}
93cdf0e10cSrcweir 
94cdf0e10cSrcweir             // default copy ctor/assignment operator are ok
95cdf0e10cSrcweir             // LazyUpdate( const LazyUpdate& );
96cdf0e10cSrcweir             // LazyUpdate& operator=( const LazyUpdate& );
97cdf0e10cSrcweir 
setInValue(input_type const & rIn)98cdf0e10cSrcweir             void               setInValue( input_type const& rIn ) { Functor::m_bCacheDirty = true; m_aInput = rIn; }
getInValue() const99cdf0e10cSrcweir             input_type const&  getInValue()  const                 { return m_aInput; }
getOutValue() const100*7ee1d29cSAriel Constenla-Haile             output_type const& getOutValue() const                 { return this->implUpdateValue(m_aInput); }
101cdf0e10cSrcweir 
operator *()102cdf0e10cSrcweir             input_type& operator*()  { Functor::m_bCacheDirty = true; return m_aInput;  }
operator ->()103cdf0e10cSrcweir             input_type* operator->() { Functor::m_bCacheDirty = true; return &m_aInput; }
104cdf0e10cSrcweir 
operator *() const105*7ee1d29cSAriel Constenla-Haile             output_type const& operator*() const  { return this->implUpdateValue(m_aInput);  }
operator ->() const106cdf0e10cSrcweir             output_type const* operator->() const { return &implUpdateValue(m_aInput); }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         private:
109cdf0e10cSrcweir             input_type m_aInput;
110cdf0e10cSrcweir         };
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         template< typename InputType, typename OutputType > struct DefaultFunctor
113cdf0e10cSrcweir         {
114cdf0e10cSrcweir         protected:
115cdf0e10cSrcweir             typedef OutputType output_type;
116cdf0e10cSrcweir             typedef InputType  input_type;
117cdf0e10cSrcweir 
DefaultFunctoro3tl::detail::DefaultFunctor118cdf0e10cSrcweir             DefaultFunctor() :
119cdf0e10cSrcweir                 m_aOutput(),
120cdf0e10cSrcweir                 m_bCacheDirty(true)
121cdf0e10cSrcweir             {}
122cdf0e10cSrcweir 
implUpdateValueo3tl::detail::DefaultFunctor123cdf0e10cSrcweir             OutputType const& implUpdateValue( input_type const& rIn ) const
124cdf0e10cSrcweir             {
125cdf0e10cSrcweir                 if( m_bCacheDirty )
126cdf0e10cSrcweir                 {
127cdf0e10cSrcweir                     m_aOutput = output_type( rIn );
128cdf0e10cSrcweir                     m_bCacheDirty = false;
129cdf0e10cSrcweir                 }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir                 return m_aOutput;
132cdf0e10cSrcweir             }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir             mutable output_type m_aOutput;
135cdf0e10cSrcweir             mutable bool        m_bCacheDirty; // when true, m_aOutput needs update
136cdf0e10cSrcweir         };
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         template< typename InputType, typename OutputType, typename FunctionType > struct FunctionPointer
139cdf0e10cSrcweir         {
140cdf0e10cSrcweir         protected:
141cdf0e10cSrcweir             typedef OutputType   output_type;
142cdf0e10cSrcweir             typedef InputType    input_type;
143cdf0e10cSrcweir             typedef FunctionType function_type;
144cdf0e10cSrcweir 
FunctionPointero3tl::detail::FunctionPointer145cdf0e10cSrcweir             FunctionPointer() :
146cdf0e10cSrcweir                 m_pFunc(),
147cdf0e10cSrcweir                 m_aOutput(),
148cdf0e10cSrcweir                 m_bCacheDirty(true)
149cdf0e10cSrcweir 
150cdf0e10cSrcweir             {}
151cdf0e10cSrcweir 
FunctionPointero3tl::detail::FunctionPointer152cdf0e10cSrcweir             explicit FunctionPointer( function_type const& pFunc ) :
153cdf0e10cSrcweir                 m_pFunc(pFunc),
154cdf0e10cSrcweir                 m_aOutput(),
155cdf0e10cSrcweir                 m_bCacheDirty(true)
156cdf0e10cSrcweir 
157cdf0e10cSrcweir             {}
158cdf0e10cSrcweir 
implUpdateValueo3tl::detail::FunctionPointer159cdf0e10cSrcweir             output_type const& implUpdateValue( input_type const& rIn ) const
160cdf0e10cSrcweir             {
161cdf0e10cSrcweir                 if( m_bCacheDirty )
162cdf0e10cSrcweir                 {
163cdf0e10cSrcweir                     m_aOutput = m_pFunc( rIn );
164cdf0e10cSrcweir                     m_bCacheDirty = false;
165cdf0e10cSrcweir                 }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir                 return m_aOutput;
168cdf0e10cSrcweir             }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir             function_type       m_pFunc;
171cdf0e10cSrcweir             mutable output_type m_aOutput;
172cdf0e10cSrcweir             mutable bool        m_bCacheDirty; // when true, m_aOutput needs update
173cdf0e10cSrcweir         };
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     // -----------------------------------------------------------------------------------------------------
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     // partial specializations for the three LAZYUPDATE_* tags
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     template< typename InputType, typename OutputType > class LazyUpdate<InputType,
181cdf0e10cSrcweir                                                                          OutputType,
182cdf0e10cSrcweir                                                                          LAZYUPDATE_DIRECT_TAG> :
183cdf0e10cSrcweir         public detail::LazyUpdateImpl<InputType,
184cdf0e10cSrcweir                                       OutputType,
185cdf0e10cSrcweir                                       detail::DefaultFunctor<InputType, OutputType> >
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir     public:
LazyUpdate()188cdf0e10cSrcweir         LazyUpdate() {}
LazyUpdate(InputType const & rIn)189cdf0e10cSrcweir         explicit LazyUpdate( InputType const& rIn ) :
190cdf0e10cSrcweir             detail::LazyUpdateImpl<InputType,
191cdf0e10cSrcweir                                    OutputType,
192cdf0e10cSrcweir                                    detail::DefaultFunctor<InputType, OutputType> >(
193cdf0e10cSrcweir                                        rIn,
194cdf0e10cSrcweir                                        detail::LazyUpdateImpl<
195cdf0e10cSrcweir                                             InputType,
196cdf0e10cSrcweir                                             OutputType,
197cdf0e10cSrcweir                                             detail::DefaultFunctor<InputType, OutputType> >::UNARY_CONSTRUCTOR_TAG )
198cdf0e10cSrcweir         {}
199cdf0e10cSrcweir     };
200cdf0e10cSrcweir 
201cdf0e10cSrcweir     // -----------------------------------------------------------------------------------------------------
202cdf0e10cSrcweir 
203cdf0e10cSrcweir     template< typename InputType, typename OutputType > class LazyUpdate<InputType,
204cdf0e10cSrcweir                                                                          OutputType,
205cdf0e10cSrcweir                                                                          LAZYUPDATE_FUNCTION_TAG> :
206cdf0e10cSrcweir         public detail::LazyUpdateImpl<InputType,
207cdf0e10cSrcweir                                       OutputType,
208cdf0e10cSrcweir                                       detail::FunctionPointer<
209cdf0e10cSrcweir                                           InputType,
210cdf0e10cSrcweir                                           OutputType,
211cdf0e10cSrcweir                                           OutputType (*)( InputType const& ) > >
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir     public:
LazyUpdate(OutputType (* pFunc)(InputType const &))214cdf0e10cSrcweir         explicit LazyUpdate( OutputType (*pFunc)( InputType const& ) ) :
215cdf0e10cSrcweir             detail::LazyUpdateImpl<InputType,
216cdf0e10cSrcweir                                    OutputType,
217cdf0e10cSrcweir                                    detail::FunctionPointer<
218cdf0e10cSrcweir                                        InputType,
219cdf0e10cSrcweir                                        OutputType,
220cdf0e10cSrcweir                                        OutputType (*)( InputType const& )> >(pFunc)
221cdf0e10cSrcweir         {}
LazyUpdate(OutputType (* pFunc)(InputType const &),InputType const & rIn)222cdf0e10cSrcweir         LazyUpdate( OutputType (*pFunc)( InputType const& ),
223cdf0e10cSrcweir                     InputType const& rIn ) :
224cdf0e10cSrcweir             detail::LazyUpdateImpl<InputType,
225cdf0e10cSrcweir                                    OutputType,
226cdf0e10cSrcweir                                    detail::FunctionPointer<
227cdf0e10cSrcweir                                        InputType,
228cdf0e10cSrcweir                                        OutputType,
229cdf0e10cSrcweir                                        OutputType (*)( InputType const& )> >(pFunc,rIn)
230cdf0e10cSrcweir         {}
231cdf0e10cSrcweir     };
232cdf0e10cSrcweir 
233cdf0e10cSrcweir     // -----------------------------------------------------------------------------------------------------
234cdf0e10cSrcweir 
235cdf0e10cSrcweir     template< typename InputType, typename OutputType > class LazyUpdate<InputType,
236cdf0e10cSrcweir                                                                          OutputType,
237cdf0e10cSrcweir                                                                          LAZYUPDATE_FUNCTOR_TAG> :
238cdf0e10cSrcweir         public detail::LazyUpdateImpl<InputType,
239cdf0e10cSrcweir                                       OutputType,
240cdf0e10cSrcweir                                       detail::FunctionPointer<
241cdf0e10cSrcweir                                           InputType,
242cdf0e10cSrcweir                                           OutputType,
243cdf0e10cSrcweir                                           boost::function1<OutputType,InputType> > >
244cdf0e10cSrcweir     {
245cdf0e10cSrcweir     public:
LazyUpdate(boost::function1<OutputType,InputType> const & rFunc)246cdf0e10cSrcweir         explicit LazyUpdate( boost::function1<OutputType,InputType> const& rFunc ) :
247cdf0e10cSrcweir             detail::LazyUpdateImpl<InputType,
248cdf0e10cSrcweir                                    OutputType,
249cdf0e10cSrcweir                                    detail::FunctionPointer<
250cdf0e10cSrcweir                                        InputType,
251cdf0e10cSrcweir                                        OutputType,
252cdf0e10cSrcweir                                        boost::function1<OutputType,InputType> > >(rFunc)
253cdf0e10cSrcweir         {}
LazyUpdate(boost::function1<OutputType,InputType> const & rFunc,InputType const & rIn)254cdf0e10cSrcweir         LazyUpdate( boost::function1<OutputType,InputType> const& rFunc,
255cdf0e10cSrcweir                     InputType const& rIn ) :
256cdf0e10cSrcweir             detail::LazyUpdateImpl<InputType,
257cdf0e10cSrcweir                                    OutputType,
258cdf0e10cSrcweir                                    detail::FunctionPointer<
259cdf0e10cSrcweir                                        InputType,
260cdf0e10cSrcweir                                        OutputType,
261cdf0e10cSrcweir                                        boost::function1<OutputType,InputType> > >(rFunc,rIn)
262cdf0e10cSrcweir         {}
263cdf0e10cSrcweir     };
264cdf0e10cSrcweir 
265cdf0e10cSrcweir }
266cdf0e10cSrcweir 
267cdf0e10cSrcweir #endif /* INCLUDED_O3TL_LAZY_UPDATE_HXX */
268