Bump version to 6.4.7.2.M8
[LibreOffice.git] / o3tl / qa / cow_wrapper_clients.hxx
blob36d77553b178ae91c68d318625924f8e103a7919
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <assert.h>
27 /* Definition of Cow_Wrapper_Clients classes */
29 namespace o3tltests {
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
39 public:
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; }
55 private:
56 o3tl::cow_wrapper< int > maImpl;
60 class cow_wrapper_client2_impl;
62 /** test opaque impl type - need to explicitly declare lifetime
63 methods
65 class cow_wrapper_client2
67 public:
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;
80 void makeUnique();
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;
89 private:
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
98 public:
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;
111 void makeUnique();
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;
120 private:
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
126 thing
128 class cow_wrapper_client4
130 public:
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;
144 private:
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.");
158 ++rCount;
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.");
163 if(s_nEndOfScope)
165 --rCount;
166 --s_nEndOfScope;
168 else if(s_bShouldDecrement)
170 --rCount;
171 s_bShouldDecrement = false;
173 return rCount != 0;
177 class cow_wrapper_client5
179 public:
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;
195 private:
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: */