xref: /trunk/main/salhelper/inc/salhelper/future.hxx (revision 8a25e0a8)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SALHELPER_FUTURE_HXX_
25 #define _SALHELPER_FUTURE_HXX_
26 
27 #include <sal/types.h>
28 #include <osl/diagnose.h>
29 #include <osl/conditn.hxx>
30 #include <salhelper/refobj.hxx>
31 
32 namespace salhelper
33 {
34 
35 //----------------------------------------------------------------------------
36 
37 #ifndef SALHELPER_COPYCTOR_API
38 #define SALHELPER_COPYCTOR_API(C) C (const C&); C& operator= (const C&)
39 #endif
40 
41 //----------------------------------------------------------------------------
42 
43 template<class value_type>
44 class FutureValue : protected osl::Condition
45 {
46 	/** Representation.
47 	 */
48 	value_type m_aValue;
49 
50 	/** Not implemented.
51 	 */
52 	SALHELPER_COPYCTOR_API(FutureValue<value_type>);
53 
54 public:
55 	inline FutureValue (const value_type& value = value_type()) SAL_THROW(())
56 		: m_aValue (value)
57 	{
58 		Condition::reset();
59 	}
60 
61 	inline ~FutureValue() SAL_THROW(())
62 	{}
63 
is() const64 	inline sal_Bool is() const SAL_THROW(())
65 	{
66 		return const_cast<FutureValue*>(this)->check();
67 	}
68 
set(const value_type & value)69 	inline void set (const value_type& value) SAL_THROW(())
70 	{
71 		m_aValue = value;
72 		Condition::set();
73 	}
74 
get()75 	inline value_type& get() SAL_THROW(())
76 	{
77 		Condition::wait();
78 		return m_aValue;
79 	}
80 };
81 
82 //----------------------------------------------------------------------------
83 
84 template<class value_type>
85 class Future : public salhelper::ReferenceObject
86 {
87 	/** Representation.
88 	 */
89 	FutureValue<value_type> m_aValue;
90 
91 	/** Not implemented.
92 	 */
93 	SALHELPER_COPYCTOR_API(Future<value_type>);
94 
95 public:
96 	inline Future (const value_type& value = value_type()) SAL_THROW(())
97 		: m_aValue (value)
98 	{}
99 
set(const value_type & value)100 	inline void set (const value_type& value) SAL_THROW(())
101 	{
102 		OSL_PRECOND(!m_aValue.is(), "Future::set(): value already set");
103 		m_aValue.set (value);
104 	}
105 
get()106 	inline value_type& get() SAL_THROW(())
107 	{
108 		return m_aValue.get();
109 	}
110 };
111 
112 //----------------------------------------------------------------------------
113 
114 } // namespace salhelper
115 
116 #endif /* !_SALHELPER_FUTURE_HXX_ */
117