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
40#include <memory>
41
42namespace Nektar
43{
44template <typename DataType> struct ConsistentObjectAccess
45{
46 static DataType &reference(DataType &o)
47 {
48 return o;
49 }
50 static const DataType &const_reference(const DataType &o)
51 {
52 return o;
53 }
54 static DataType *pointer(DataType &o)
55 {
56 return &o;
57 }
58 static const DataType *const_pointer(const DataType &o)
59 {
60 return &o;
61 }
62
63 static bool ReferencesObject([[maybe_unused]] const DataType &o)
64 {
65 return true;
66 }
67};
68
69template <typename DataType> struct ConsistentObjectAccess<DataType *>
70{
71 static const DataType &const_reference(DataType *o)
72 {
73 ASSERTL1(o != nullptr, "Can't dereference null pointer.");
74 return *o;
75 }
76 static const DataType *const_pointer(DataType *o)
77 {
78 return o;
79 }
80 static bool ReferencesObject(DataType *o)
81 {
82 return o != nullptr;
83 }
84
85 static DataType &reference(DataType *o)
86 {
87 ASSERTL1(o != nullptr, "Can't dereference null pointer.");
88 return *o;
89 }
90 static DataType *pointer(DataType *o)
91 {
92 return o;
93 }
94};
95
96template <typename DataType>
97struct ConsistentObjectAccess<std::shared_ptr<DataType>>
98{
99 static const DataType &const_reference(const std::shared_ptr<DataType> &o)
100 {
101 ASSERTL1(o, "Can't dereference null pointer.");
102 return *o;
103 }
104 static const DataType *const_pointer(const std::shared_ptr<DataType> &o)
105 {
106 return o.get();
107 }
108 static DataType &reference(const std::shared_ptr<DataType> &o)
109 {
110 ASSERTL1(o, "Can't dereference null pointer.");
111 return *o;
112 }
113 static DataType *pointer(const std::shared_ptr<DataType> &o)
114 {
115 return o.get();
116 }
117 static bool ReferencesObject(const std::shared_ptr<DataType> &o)
118 {
119 return o.get();
120 }
121};
122} // namespace Nektar
123
124#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:242
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)