Nektar++
ConsistentObjectAccess.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: ConsistentObjectAccess.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: A wrapper around objects or pointers to objects which give
32// the same interface for both types (i.e., -> will work for both).
33//
34///////////////////////////////////////////////////////////////////////////////
35
36#ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_CONSISTENT_ACCESS_OBJECT_HPP
37#define NEKTAR_LIB_UTILITIES_BASIC_UTILS_CONSISTENT_ACCESS_OBJECT_HPP
38
39#include <boost/core/ignore_unused.hpp>
40
42#include <memory>
43
44namespace Nektar
45{
46template <typename DataType> struct ConsistentObjectAccess
47{
48 static DataType &reference(DataType &o)
49 {
50 return o;
51 }
52 static const DataType &const_reference(const DataType &o)
53 {
54 return o;
55 }
56 static DataType *pointer(DataType &o)
57 {
58 return &o;
59 }
60 static const DataType *const_pointer(const DataType &o)
61 {
62 return &o;
63 }
64
65 static bool ReferencesObject(const DataType &o)
66 {
67 boost::ignore_unused(o);
68 return true;
69 }
70};
71
72template <typename DataType> struct ConsistentObjectAccess<DataType *>
73{
74 static const DataType &const_reference(DataType *o)
75 {
76 ASSERTL1(o != 0, "Can't dereference null pointer.");
77 return *o;
78 }
79 static const DataType *const_pointer(DataType *o)
80 {
81 return o;
82 }
83 static bool ReferencesObject(DataType *o)
84 {
85 return o != 0;
86 }
87
88 static DataType &reference(DataType *o)
89 {
90 ASSERTL1(o != 0, "Can't dereference null pointer.");
91 return *o;
92 }
93 static DataType *pointer(DataType *o)
94 {
95 return o;
96 }
97};
98
99template <typename DataType>
100struct ConsistentObjectAccess<std::shared_ptr<DataType>>
101{
102 static const DataType &const_reference(const std::shared_ptr<DataType> &o)
103 {
104 ASSERTL1(o, "Can't dereference null pointer.");
105 return *o;
106 }
107 static const DataType *const_pointer(const std::shared_ptr<DataType> &o)
108 {
109 return o.get();
110 }
111 static DataType &reference(const std::shared_ptr<DataType> &o)
112 {
113 ASSERTL1(o, "Can't dereference null pointer.");
114 return *o;
115 }
116 static DataType *pointer(const std::shared_ptr<DataType> &o)
117 {
118 return o.get();
119 }
120 static bool ReferencesObject(const std::shared_ptr<DataType> &o)
121 {
122 return o.get();
123 }
124};
125} // namespace Nektar
126
127#endif // NEKTAR_LIB_UTILITIES_BASIC_UTILS_CONSISTENT_ACCESS_OBJECT_HPP
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:249
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
static const DataType & const_reference(DataType *o)
static const DataType * const_pointer(DataType *o)
static DataType & reference(const std::shared_ptr< DataType > &o)
static const DataType * const_pointer(const std::shared_ptr< DataType > &o)
static DataType * pointer(const std::shared_ptr< DataType > &o)
static const DataType & const_reference(const std::shared_ptr< DataType > &o)
static bool ReferencesObject(const std::shared_ptr< DataType > &o)
static const DataType * const_pointer(const DataType &o)
static bool ReferencesObject(const DataType &o)
static DataType * pointer(DataType &o)
static DataType & reference(DataType &o)
static const DataType & const_reference(const DataType &o)