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 
41 #include <memory>
43 
44 namespace Nektar
45 {
46  template<typename DataType>
48  {
49  static DataType& reference(DataType& o) { return o; }
50  static const DataType& const_reference(const DataType& o) { return o; }
51  static DataType* pointer(DataType& o) { return &o; }
52  static const DataType* const_pointer(const DataType& o) { return &o; }
53 
54  static bool ReferencesObject(const DataType& o) {
55  boost::ignore_unused(o);
56  return true;
57  }
58  };
59 
60  template<typename DataType>
61  struct ConsistentObjectAccess<DataType*>
62  {
63  static const DataType& const_reference(DataType* o) { ASSERTL1(o != 0, "Can't dereference null pointer."); return *o; }
64  static const DataType* const_pointer(DataType* o) { return o; }
65  static bool ReferencesObject(DataType* o) { return o != 0; }
66 
67  static DataType& reference(DataType* o) { ASSERTL1(o != 0, "Can't dereference null pointer."); return *o; }
68  static DataType* pointer(DataType* o) { return o; }
69  };
70 
71 
72  template<typename DataType>
73  struct ConsistentObjectAccess<std::shared_ptr<DataType> >
74  {
75  static const DataType& const_reference(const std::shared_ptr<DataType>& o) { ASSERTL1(o, "Can't dereference null pointer."); return *o; }
76  static const DataType* const_pointer(const std::shared_ptr<DataType>& o) { return o.get(); }
77  static DataType& reference(const std::shared_ptr<DataType>& o) { ASSERTL1(o, "Can't dereference null pointer."); return *o; }
78  static DataType* pointer(const std::shared_ptr<DataType>& o) { return o.get(); }
79  static bool ReferencesObject(const std::shared_ptr<DataType>& o) { return o.get(); }
80  };
81 }
82 
83 #endif //NEKTAR_LIB_UTILITIES_BASIC_UTILS_CONSISTENT_ACCESS_OBJECT_HPP
84 
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:250
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
static const DataType * const_pointer(DataType *o)
static const DataType & const_reference(DataType *o)
static const DataType * const_pointer(const std::shared_ptr< DataType > &o)
static const DataType & const_reference(const std::shared_ptr< DataType > &o)
static DataType & reference(const std::shared_ptr< DataType > &o)
static bool ReferencesObject(const std::shared_ptr< DataType > &o)
static DataType * pointer(const std::shared_ptr< DataType > &o)
static const DataType & const_reference(const DataType &o)
static bool ReferencesObject(const DataType &o)
static DataType * pointer(DataType &o)
static DataType & reference(DataType &o)
static const DataType * const_pointer(const DataType &o)