Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
TestClasses.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_TEST_CLASSES_HPP
43 #define TEUCHOS_TEST_CLASSES_HPP
44 
45 
46 #include "Teuchos_RCP.hpp"
47 
48 
49 // Return constants from class functions
50 const int A_g_return = 1;
51 const int A_f_return = 2;
52 const int B1_g_return = 3;
53 const int B1_f_return = 4;
54 const int B2_g_return = 5;
55 const int B2_f_return = 6;
56 const int C_g_return = 7;
57 const int C_f_return = 8;
58 const int D_g_return = 9;
59 const int D_f_return = 10;
60 const int E_g_return = 11;
61 const int E_f_return = 12;
62 
63 
64 /*
65 
66  Polymorphic multiple inheritance example
67 
68  -----
69  | A |
70  -----
71  /|\
72  |
73  ------------
74  | |
75  ----- ------
76  | B1 | | B2 |
77  ----- ------
78  /|\ /|\
79  | |
80  ------------
81  |
82  -----
83  | C |
84  -----
85 
86 */
87 
88 
89 class C;
90 
91 
92 class A {
93  int A_g_, A_f_;
94 public:
96  static Teuchos::RCP<A> create() { return Teuchos::rcp(new A); }
97  virtual ~A() TEUCHOS_NOEXCEPT_FALSE; // See below
98  virtual int A_g() { return A_g_; }
99  virtual int A_f() const { return A_f_; }
100  int call_C_f();
101 private:
103 public:
104  void set_C(const Teuchos::RCP<C> &c ) { c_ = c; }
105 };
106 
107 
108 class B1 : virtual public A {
109  int B1_g_, B1_f_;
110 public:
112  ~B1() { B1_g_ = -1; B1_f_ = -1; }
113  static Teuchos::RCP<B1> create() { return Teuchos::rcp(new B1); }
114  virtual int B1_g() { return B1_g_; }
115  virtual int B1_f() const { return B1_f_; }
116 };
117 
118 
119 class B2 : virtual public A {
120  int B2_g_, B2_f_;
121 public:
123  static Teuchos::RCP<B2> create() { return Teuchos::rcp(new B2); }
124  ~B2() { B2_g_ = -1; B2_f_ = -1; }
125  virtual int B2_g() { return B2_g_; }
126  virtual int B2_f() const { return B2_f_; }
127 };
128 
129 
130 class C : virtual public B1, virtual public B2
131 {
132  int C_g_, C_f_;
133 public:
135  {
136  A_g_on_delete_ = -2;
137  }
138  static Teuchos::RCP<C> create() { return Teuchos::rcp(new C); }
140  {
141  C_g_ = -1; C_f_ = -1;
142  if (call_A_on_delete_) {
143  // VERY BAD THING TO DO!
145  // NOTE: If a_ is a weak pointer and the underlying 'A' object has
146  // already been deleted, then this destructor will throw an exception.
147  // This is *never* a good thing to do in production code. However, I
148  // am allowing this destructor to throw an exception so I can write a
149  // unit test to detect this. The destructor must be declared as
150  // noexcept(false) in C++11 (using the TEUCHOS_NOEXCEPT_FALSE
151  // definition), otherwise it will terminate the program by calling
152  // std::terminate when the exception is thrown. When the ~C()
153  // destructor is declared as noexcept(false), then also the ~A()
154  // destructor must be declared as noexcept(false), because a method in
155  // a derived class cannot throw more exceptions than the corresponding
156  // virtual method in the superclass.
157  }
158  }
159  virtual int C_g() { return C_g_; }
160  virtual int C_f() const { return C_f_; }
161  void call_A_on_delete(bool call_A_on_delete_in)
162  { call_A_on_delete_ = call_A_on_delete_in; }
163  int call_A_g() { return a_->A_g(); }
164  static int get_A_g_on_delete() { return A_g_on_delete_; }
165 private:
168  static int A_g_on_delete_;
169 public:
170  void set_A(const Teuchos::RCP<A> &a ) { a_ = a; }
171  Teuchos::RCP<A> get_A() { return a_; }
172 };
173 
174 
175 // Need to put these here if we have circular references
176 
177 inline
178 A::~A() TEUCHOS_NOEXCEPT_FALSE { A_g_ = -1; A_f_ = -1; }
179 
180 
181 inline
182 int A::call_C_f() { return c_->C_f(); }
183 
184 
186  const A *a_;
188  Get_A_f_return();
189 public:
190  Get_A_f_return( const A *a, int *a_f_return ) : a_(a), a_f_return_(a_f_return) {}
192 };
193 
194 
195 void deallocA(A* ptr);
196 
197 
198 void deallocHandleA(A** handle);
199 
200 
201 /*
202 
203  Non-polymophic classes hiearchy examlpe
204 
205  -----
206  | D |
207  -----
208  /|\
209  |
210  -----
211  | E |
212  -----
213 
214 */
215 
216 
217 class D
218 {
219  int D_g_, D_f_;
220 public:
222  int D_g() { return D_g_; }
223  int D_f() const { return D_f_; }
224 };
225 
226 
227 class E : public D
228 {
229  int E_g_, E_f_;
230 public:
232  int E_g() { return E_g_; }
233  int E_f() const { return E_f_; }
234 };
235 
236 
237 /*
238 
239 Typedef to pointer for undefined struct as an opaque object type without a
240 specialization of TypeNameTraits.
241 
242 This simulates what happens with a lot of MPI implementations.
243 
244 */
245 
246 struct UndefinedType; // Forward declared but never defined!
250 const int getOpaqueValue_return = 5;
251 int getOpaqueValue( Opaque_handle opaque );
252 void destroyOpaque( Opaque_handle * opaque );
253 
254 
255 /*
256 
257 Typedef to pointer for an undefiend struct as an opaque object type out a
258 specialization of TypeNameTraits of the actually type.
259 
260 This allows it to be stored in an RCP object itself.
261 
262 */
263 
264 struct UndefinedType2; // Forward declared but never defined!
269 int getOpaque2Value( Opaque2_handle opaque );
270 void destroyOpaque2( Opaque2_handle * opaque );
271 
272 
273 namespace Teuchos {
274 
275 
276 // Here we define the traits for the underlying type itself.
277 template<>
279 public:
280  static std::string name() { return "UndefinedType2"; }
281  static std::string concreteName(const UndefinedType2&)
282  { return name(); }
283 };
284 
285 
286 } // namespace Teuchos
287 
288 
289 /*
290 
291 Typedef to pointer for an undefiend struct as an opaque object type out a
292 specialization of TypeNameTraits of the actually type.
293 
294 This allows handles to the type be used with Array, ArrayRCP, and ArrayView.
295 However, this type can *not* be used with RCP since it does not define a
296 TypeNameTraits specialization for the underlying undefined type.
297 
298 This simulates what can happen with MPI implementations.
299 
300 */
301 
302 struct UndefinedType3; // Forward declared but never defined!
303 typedef UndefinedType3* Opaque3_handle;
305 
306 
307 namespace Teuchos {
308 
309 // Here we only define the traits class for the handle type and we don't even
310 // need to worry about what the underlying type is (unless we already have a
311 // speicalization defined for it).
312 template<>
314 public:
315  static std::string name() { return "Opaque3_handle"; }
316  static std::string concreteName(Opaque3_handle)
317  { return name(); }
318 };
319 
320 
321 } // namespace Teuchos
322 
323 
324 #endif // TEUCHOS_TEST_CLASSES_HPP
int E_f_
void set_C(const Teuchos::RCP< C > &c)
static Teuchos::RCP< B1 > create()
const int E_f_return
Definition: TestClasses.hpp:61
const int B2_g_return
Definition: TestClasses.hpp:54
virtual int B2_g()
int B2_f_
int B1_g_
void deallocHandleA(A **handle)
Definition: TestClasses.cpp:55
virtual int A_g()
Definition: TestClasses.hpp:98
~C() TEUCHOS_NOEXCEPT_FALSE
const Opaque_handle OPAQUE_HANDLE_NULL
int call_C_f()
Opaque2_handle createOpaque2()
Definition: TestClasses.cpp:96
static Teuchos::RCP< C > create()
Opaque_handle createOpaque()
Definition: TestClasses.cpp:69
bool call_A_on_delete_
virtual int B1_f() const
A()
Definition: TestClasses.hpp:95
const int C_g_return
Definition: TestClasses.hpp:56
int C_g_
static Teuchos::RCP< A > create()
Definition: TestClasses.hpp:96
const int C_f_return
Definition: TestClasses.hpp:57
const int A_f_return
Definition: TestClasses.hpp:51
void destroyOpaque(Opaque_handle *opaque)
Definition: TestClasses.cpp:83
const int getOpaqueValue_return
void deallocA(A *ptr)
Definition: TestClasses.cpp:48
const Opaque2_handle OPAQUE2_HANDLE_NULL
Definition: PackageA.cpp:3
UndefinedType2 * Opaque2_handle
int A_f_
Definition: TestClasses.hpp:93
int A_g_
Definition: TestClasses.hpp:93
const int getOpaque2Value_return
static int A_g_on_delete_
Teuchos::RCP< A > get_A()
virtual int B2_f() const
virtual int A_f() const
Definition: TestClasses.hpp:99
const int D_f_return
Definition: TestClasses.hpp:59
int call_A_g()
int getOpaque2Value(Opaque2_handle opaque)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
virtual int B1_g()
int B1_f_
int D_g_
virtual int C_f() const
int D_f() const
const Opaque3_handle OPAQUE3_HANDLE_NULL
static std::string concreteName(const UndefinedType2 &)
static Teuchos::RCP< B2 > create()
Teuchos::RCP< A > a_
void set_A(const Teuchos::RCP< A > &a)
Definition: PackageC.cpp:3
static std::string concreteName(Opaque3_handle)
static int get_A_g_on_delete()
UndefinedType * Opaque_handle
Get_A_f_return(const A *a, int *a_f_return)
#define TEUCHOS_NOEXCEPT_FALSE
Ptr< T > ptr(T *p)
Create a pointer to an object from a raw pointer.
const int D_g_return
Definition: TestClasses.hpp:58
const int B1_f_return
Definition: TestClasses.hpp:53
int D_g()
Teuchos::RCP< C > c_
Default traits class that just returns typeid(T).name().
UndefinedType3 * Opaque3_handle
int getOpaqueValue(Opaque_handle opaque)
Definition: TestClasses.cpp:77
const int B1_g_return
Definition: TestClasses.hpp:52
int C_f_
int E_g_
int E_f() const
void destroyOpaque2(Opaque2_handle *opaque)
int D_f_
int E_g()
Reference-counted pointer class and non-member templated function implementations.
void call_A_on_delete(bool call_A_on_delete_in)
const int A_g_return
Definition: TestClasses.hpp:50
const int E_g_return
Definition: TestClasses.hpp:60
const int B2_f_return
Definition: TestClasses.hpp:55
virtual int C_g()
int B2_g_