Teuchos Package Browser (Single Doxygen Collection)
Version of the Day
core
example
RefCountPtr
core/example/RefCountPtr/cxx_main.cpp
Go to the documentation of this file.
1
/*
2
// @HEADER
3
// ***********************************************************************
4
//
5
// Teuchos: Common Tools Package
6
// Copyright (2004) Sandia Corporation
7
//
8
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9
// license for use of this work by or on behalf of the U.S. Government.
10
//
11
// Redistribution and use in source and binary forms, with or without
12
// modification, are permitted provided that the following conditions are
13
// met:
14
//
15
// 1. Redistributions of source code must retain the above copyright
16
// notice, this list of conditions and the following disclaimer.
17
//
18
// 2. Redistributions in binary form must reproduce the above copyright
19
// notice, this list of conditions and the following disclaimer in the
20
// documentation and/or other materials provided with the distribution.
21
//
22
// 3. Neither the name of the Corporation nor the names of the
23
// contributors may be used to endorse or promote products derived from
24
// this software without specific prior written permission.
25
//
26
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
//
38
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39
//
40
// ***********************************************************************
41
// @HEADER
42
*/
43
44
#include "
Teuchos_RCP.hpp
"
45
#include "
Teuchos_Version.hpp
"
46
47
class
A
{
48
public
:
49
A
() {}
50
virtual
~A
(){}
51
virtual
void
f
(){}
52
};
53
class
B1
:
virtual
public
A
{};
54
class
B2
:
virtual
public
A
{};
55
class
C
:
public
B1
,
public
B2
{};
56
57
using namespace
Teuchos
;
58
59
int
main
(
int
argc,
char
* argv[])
60
{
61
62
std::cout <<
Teuchos::Teuchos_Version
() << std::endl << std::endl;
63
64
// Create some reference-counted pointers.
65
// Create a reference-counted NULL pointer of type A.
66
RCP<A>
a_null_ptr;
67
// Create a reference-counted pointer of non-const type A.
68
RCP<A>
a_ptr =
rcp
(
new
A
);
69
// Create a reference-counted pointer of const type A.
70
RCP<const A>
ca_ptr =
rcp
(
new
A
);
71
// Create a const reference-counted pointer of non-const type A.
72
const
RCP<A>
a_cptr =
rcp
(
new
A
);
73
// Create a const reference-counted pointer of const type A.
74
const
RCP<const A>
ca_cptr =
rcp
(
new
A
);
75
76
// Perform implicit conversions between a derived class and its base class.
77
RCP<B1>
b1_ptr =
rcp
(
new
B1
);
78
RCP<A>
a_ptr1 = b1_ptr;
79
80
/* Other non-implicit type conversions like static, dynamic, or const casts
81
can be taken care of by non-member template functions.
82
*/
83
RCP<const C>
c_ptr =
rcp
(
new
C
);
84
// Implicit cast from C to B2.
85
RCP<const B2>
b2_ptr = c_ptr;
86
// Safe cast, type-checked, from C to A.
87
RCP<const A>
ca_ptr1 = rcp_dynamic_cast<
const
A
>(c_ptr);
88
// Unsafe cast, non-type-checked, from C to A.
89
RCP<const A>
ca_ptr2 = rcp_static_cast<
const
A
>(c_ptr);
90
// Cast away const from B2.
91
RCP<B2>
nc_b2_ptr = rcp_const_cast<
B2
>(b2_ptr);
92
93
/* Using a reference-counted pointer is very similar to using a raw C++ pointer. Some
94
of the operations that are common to both are:
95
*/
96
RCP<A>
97
a_ptr2 =
rcp
(
new
A
),
// Initialize reference-counted pointers.
98
a_ptr3 =
rcp
(
new
A
);
// ""
99
A
*ra_ptr2 =
new
A
,
// Initialize non-reference counted pointers.
100
*ra_ptr3 =
new
A
;
// ""
101
a_ptr2 =
rcp
(ra_ptr3);
// Assign from a raw pointer (only do this once!)
102
a_ptr3 = a_ptr1;
// Assign one smart pointer to another.
103
a_ptr2 =
rcp
(ra_ptr2);
// Assign from a raw pointer (only do this once!)
104
a_ptr2->f();
// Access a member of A using ->
105
ra_ptr2->
f
();
// ""
106
*a_ptr2 = *a_ptr3;
// Dereference the objects and assign.
107
*ra_ptr2 = *ra_ptr3;
// ""
108
109
// Get the raw C++ pointer.
110
A
* true_ptr = 0;
111
true_ptr = a_ptr1.
get
();
112
TEUCHOS_ASSERT_EQUALITY
(true_ptr, b1_ptr.
get
());
113
114
return
0;
115
116
}
A::f
virtual void f()
Definition:
core/example/RefCountPtr/cxx_main.cpp:51
main
int main(int argc, char *argv[])
Definition:
core/example/RefCountPtr/cxx_main.cpp:59
A::A
A()
Definition:
core/example/RefCountPtr/cxx_main.cpp:49
A
Definition:
PackageA.cpp:3
Teuchos::RCP::get
T * get() const
Get the raw C++ pointer to the underlying object.
Definition:
Teuchos_RCP.hpp:363
B2
Definition:
core/example/RefCountPtr/cxx_main.cpp:54
Teuchos::rcp
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Definition:
Teuchos_RCPDecl.hpp:1219
B1
Definition:
core/example/RefCountPtr/cxx_main.cpp:53
Teuchos::Teuchos_Version
std::string Teuchos_Version()
Definition:
Teuchos_Version.hpp:54
A::~A
virtual ~A()
Definition:
core/example/RefCountPtr/cxx_main.cpp:50
C
Definition:
PackageC.cpp:3
Teuchos
Definition:
Teuchos_AbstractFactory.hpp:47
Teuchos::RCP< A >
TEUCHOS_ASSERT_EQUALITY
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
This macro is checks that to numbers are equal and if not then throws an exception with a good error ...
Definition:
Teuchos_Assert.hpp:95
Teuchos_RCP.hpp
Reference-counted pointer class and non-member templated function implementations.
Teuchos_Version.hpp
Generated by
1.8.14