DOLFIN
DOLFIN C++ interface
Loading...
Searching...
No Matches
LinearAlgebraObject.h
1// Copyright (C) 2007-2012 Anders Logg, Garth N. Wells, Ola Skavhaug,
2// Martin Alnæs
3//
4// This file is part of DOLFIN.
5//
6// DOLFIN is free software: you can redistribute it and/or modify
7// it under the terms of the GNU Lesser General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// DOLFIN is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
18//
19// First added: 2012-08-22
20// Last changed: 2012-08-24
21
22#ifndef __LINEAR_ALGEBRA_OBJECT_H
23#define __LINEAR_ALGEBRA_OBJECT_H
24
25#include <memory>
26#include <dolfin/common/MPI.h>
27#include <dolfin/common/Variable.h>
28
29namespace dolfin
30{
31
35
36 class LinearAlgebraObject : public virtual Variable
37 {
38 public:
39
41 virtual const LinearAlgebraObject* instance() const
42 { return this; }
43
46 { return this; }
47
49 virtual std::shared_ptr<const LinearAlgebraObject> shared_instance() const
50 { return std::shared_ptr<const LinearAlgebraObject>(); }
51
53 virtual std::shared_ptr<LinearAlgebraObject> shared_instance()
54 { return std::shared_ptr<LinearAlgebraObject>(); }
55
57 virtual MPI_Comm mpi_comm() const = 0;
58
59 };
60
63 template<typename Y, typename X>
64 Y& as_type(X& x)
65 {
66 try
67 {
68 return dynamic_cast<Y&>(*x.instance());
69 }
70 catch (std::exception& e)
71 {
72 dolfin_error("LinearAlgebraObject.h",
73 "down-cast linear algebra object to requested type",
74 "%s", e.what());
75 }
76
77 // Return something to keep the compiler happy, code will not be reached
78 return dynamic_cast<Y&>(*x.instance());
79 }
80
83 template<typename Y, typename X>
84 std::shared_ptr<Y> as_type(std::shared_ptr<X> x)
85 {
86 // Try to down cast shared pointer
87 std::shared_ptr<Y> y = std::dynamic_pointer_cast<Y>(x);
88
89 // If down cast fails, try to get shared ptr instance to unwrapped object
90 if (!y)
91 {
92 // Try to get instance to unwrapped object and cast
93 if (x->shared_instance())
94 y = std::dynamic_pointer_cast<Y>(x->shared_instance());
95 }
96
97 return y;
98 }
99
101 template<typename Y, typename X>
102 bool has_type(const X& x)
103 {
104 return bool(dynamic_cast<const Y*>(x.instance()));
105 }
106
107}
108
109#endif
Definition LinearAlgebraObject.h:37
virtual const LinearAlgebraObject * instance() const
Return concrete instance / unwrap (const version)
Definition LinearAlgebraObject.h:41
virtual MPI_Comm mpi_comm() const =0
Return MPI communicator.
virtual std::shared_ptr< LinearAlgebraObject > shared_instance()
Return concrete shared ptr instance / unwrap (non-const version)
Definition LinearAlgebraObject.h:53
virtual std::shared_ptr< const LinearAlgebraObject > shared_instance() const
Return concrete shared ptr instance / unwrap (const version)
Definition LinearAlgebraObject.h:49
virtual LinearAlgebraObject * instance()
Return concrete instance / unwrap (non-const version)
Definition LinearAlgebraObject.h:45
Common base class for DOLFIN variables.
Definition Variable.h:36
Definition adapt.h:30
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition log.cpp:129
bool has_type(const X &x)
Check whether the object matches a specific type.
Definition LinearAlgebraObject.h:102
Y & as_type(X &x)
Definition LinearAlgebraObject.h:64