1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_O3TL_QA_COW_WRAPPER_CLIENTS_HXX
21 #define INCLUDED_O3TL_QA_COW_WRAPPER_CLIENTS_HXX
23 #include <o3tl/cow_wrapper.hxx>
24 #include <cppunit/extensions/HelperMacros.h>
27 /* Definition of Cow_Wrapper_Clients classes */
31 /** This is a header and a separate compilation unit on purpose -
32 cow_wrapper needs destructor, copy constructor and assignment
33 operator to be outline, when pimpl idiom is used
36 /// test non-opaque impl type
37 class cow_wrapper_client1
40 cow_wrapper_client1() : maImpl() {}
41 explicit cow_wrapper_client1( int nVal
) : maImpl(nVal
) {}
43 void modify( int nVal
) { *maImpl
= nVal
; }
44 int queryUnmodified() const { return *maImpl
; }
46 void makeUnique() { maImpl
.make_unique(); }
47 bool is_unique() const { return maImpl
.is_unique(); }
48 oslInterlockedCount
use_count() const { return maImpl
.use_count(); }
49 void swap( cow_wrapper_client1
& r
) { o3tl::swap(maImpl
, r
.maImpl
); }
51 bool operator==( const cow_wrapper_client1
& rRHS
) const { return maImpl
== rRHS
.maImpl
; }
52 bool operator!=( const cow_wrapper_client1
& rRHS
) const { return maImpl
!= rRHS
.maImpl
; }
53 bool operator<( const cow_wrapper_client1
& rRHS
) const { return maImpl
< rRHS
.maImpl
; }
56 o3tl::cow_wrapper
< int > maImpl
;
60 class cow_wrapper_client2_impl
;
62 /** test opaque impl type - need to explicitly declare lifetime
65 class cow_wrapper_client2
68 cow_wrapper_client2();
69 explicit cow_wrapper_client2( int nVal
);
70 ~cow_wrapper_client2();
72 cow_wrapper_client2( const cow_wrapper_client2
& );
73 cow_wrapper_client2(cow_wrapper_client2
&&) noexcept
;
74 cow_wrapper_client2
& operator=( const cow_wrapper_client2
& );
75 cow_wrapper_client2
& operator=(cow_wrapper_client2
&&) noexcept
;
77 void modify( int nVal
);
78 int queryUnmodified() const;
81 bool is_unique() const;
82 oslInterlockedCount
use_count() const;
83 void swap( cow_wrapper_client2
& r
);
85 bool operator==( const cow_wrapper_client2
& rRHS
) const;
86 bool operator!=( const cow_wrapper_client2
& rRHS
) const;
87 bool operator<( const cow_wrapper_client2
& rRHS
) const;
90 o3tl::cow_wrapper
< cow_wrapper_client2_impl
> maImpl
;
93 /** test MT-safe cow_wrapper - basically the same as
94 cow_wrapper_client2, only with different refcounting policy
96 class cow_wrapper_client3
99 cow_wrapper_client3();
100 explicit cow_wrapper_client3( int nVal
);
101 ~cow_wrapper_client3();
103 cow_wrapper_client3( const cow_wrapper_client3
& );
104 cow_wrapper_client3(cow_wrapper_client3
&&) noexcept
;
105 cow_wrapper_client3
& operator=( const cow_wrapper_client3
& );
106 cow_wrapper_client3
& operator=(cow_wrapper_client3
&&) noexcept
;
108 void modify( int nVal
);
109 int queryUnmodified() const;
112 bool is_unique() const;
113 oslInterlockedCount
use_count() const;
114 void swap( cow_wrapper_client3
& r
);
116 bool operator==( const cow_wrapper_client3
& rRHS
) const;
117 bool operator!=( const cow_wrapper_client3
& rRHS
) const;
118 bool operator<( const cow_wrapper_client3
& rRHS
) const;
121 o3tl::cow_wrapper
< cow_wrapper_client2_impl
, o3tl::ThreadSafeRefCountingPolicy
> maImpl
;
124 /** test default-object comparison - have default-stored-client4 share
125 the same static impl instance, check if isDefault does the right
128 class cow_wrapper_client4
131 cow_wrapper_client4();
132 explicit cow_wrapper_client4(int);
133 ~cow_wrapper_client4();
135 cow_wrapper_client4( const cow_wrapper_client4
& );
136 cow_wrapper_client4
& operator=( const cow_wrapper_client4
& );
138 bool is_default() const;
140 bool operator==( const cow_wrapper_client4
& rRHS
) const;
141 bool operator!=( const cow_wrapper_client4
& rRHS
) const;
142 bool operator<( const cow_wrapper_client4
& rRHS
) const;
145 o3tl::cow_wrapper
< int > maImpl
;
148 // singleton ref-counting policy used to keep track of when
149 // incrementing and decrementing occurs
150 struct BogusRefCountPolicy
152 static bool s_bShouldIncrement
;
153 static bool s_bShouldDecrement
;
154 static sal_uInt32 s_nEndOfScope
;
155 typedef sal_uInt32 ref_count_t
;
156 static void incrementCount( ref_count_t
& rCount
) {
157 assert(s_bShouldIncrement
&& "Ref-counting policy incremented when it should not have.");
159 s_bShouldIncrement
= false;
161 static bool decrementCount( ref_count_t
& rCount
) {
162 assert((s_nEndOfScope
|| s_bShouldDecrement
) && "Ref-counting policy decremented when it should not have.");
168 else if(s_bShouldDecrement
)
171 s_bShouldDecrement
= false;
177 class cow_wrapper_client5
180 cow_wrapper_client5();
181 explicit cow_wrapper_client5(int);
182 ~cow_wrapper_client5();
184 cow_wrapper_client5( const cow_wrapper_client5
& );
185 cow_wrapper_client5(cow_wrapper_client5
&&) noexcept
;
186 cow_wrapper_client5
& operator=( const cow_wrapper_client5
& );
187 cow_wrapper_client5
& operator=(cow_wrapper_client5
&&) noexcept
;
189 int queryUnmodified() const { return *maImpl
; }
190 sal_uInt32
use_count() const { return maImpl
.use_count(); }
192 bool operator==( const cow_wrapper_client5
& rRHS
) const;
193 bool operator!=( const cow_wrapper_client5
& rRHS
) const;
196 o3tl::cow_wrapper
< int, BogusRefCountPolicy
> maImpl
;
199 template< typename charT
, typename traits
>
200 inline std::basic_ostream
<charT
, traits
> & operator <<(
201 std::basic_ostream
<charT
, traits
> & stream
, const cow_wrapper_client5
& client
)
203 return stream
<< client
.queryUnmodified();
206 } // namespace o3tltests
208 #endif // INCLUDED_O3TL_QA_COW_WRAPPER_CLIENTS_HXX
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */