xref: /trunk/main/salhelper/inc/salhelper/future.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _SALHELPER_FUTURE_HXX_
29 #define _SALHELPER_FUTURE_HXX_
30 
31 #include <sal/types.h>
32 #include <osl/diagnose.h>
33 #include <osl/conditn.hxx>
34 #include <salhelper/refobj.hxx>
35 
36 namespace salhelper
37 {
38 
39 //----------------------------------------------------------------------------
40 
41 #ifndef SALHELPER_COPYCTOR_API
42 #define SALHELPER_COPYCTOR_API(C) C (const C&); C& operator= (const C&)
43 #endif
44 
45 //----------------------------------------------------------------------------
46 
47 template<class value_type>
48 class FutureValue : protected osl::Condition
49 {
50 	/** Representation.
51 	 */
52 	value_type m_aValue;
53 
54 	/** Not implemented.
55 	 */
56 	SALHELPER_COPYCTOR_API(FutureValue<value_type>);
57 
58 public:
59 	inline FutureValue (const value_type& value = value_type()) SAL_THROW(())
60 		: m_aValue (value)
61 	{
62 		Condition::reset();
63 	}
64 
65 	inline ~FutureValue() SAL_THROW(())
66 	{}
67 
68 	inline sal_Bool is() const SAL_THROW(())
69 	{
70 		return const_cast<FutureValue*>(this)->check();
71 	}
72 
73 	inline void set (const value_type& value) SAL_THROW(())
74 	{
75 		m_aValue = value;
76 		Condition::set();
77 	}
78 
79 	inline value_type& get() SAL_THROW(())
80 	{
81 		Condition::wait();
82 		return m_aValue;
83 	}
84 };
85 
86 //----------------------------------------------------------------------------
87 
88 template<class value_type>
89 class Future : public salhelper::ReferenceObject
90 {
91 	/** Representation.
92 	 */
93 	FutureValue<value_type> m_aValue;
94 
95 	/** Not implemented.
96 	 */
97 	SALHELPER_COPYCTOR_API(Future<value_type>);
98 
99 public:
100 	inline Future (const value_type& value = value_type()) SAL_THROW(())
101 		: m_aValue (value)
102 	{}
103 
104 	inline void set (const value_type& value) SAL_THROW(())
105 	{
106 		OSL_PRECOND(!m_aValue.is(), "Future::set(): value already set");
107 		m_aValue.set (value);
108 	}
109 
110 	inline value_type& get() SAL_THROW(())
111 	{
112 		return m_aValue.get();
113 	}
114 };
115 
116 //----------------------------------------------------------------------------
117 
118 } // namespace salhelper
119 
120 #endif /* !_SALHELPER_FUTURE_HXX_ */
121