ergo
ValidPtr.h
Go to the documentation of this file.
1 /* Ergo, version 3.8, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2019 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
4  * and Anastasia Kruchinina.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU 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  * This program 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Primary academic reference:
20  * Ergo: An open-source program for linear-scaling electronic structure
21  * calculations,
22  * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
23  * Kruchinina,
24  * SoftwareX 7, 107 (2018),
25  * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
26  *
27  * For further information about Ergo, see <http://www.ergoscf.org>.
28  */
29 
38 #ifndef MAT_VALIDPTR
39 #define MAT_VALIDPTR
40 namespace mat {
41 
42 
49  template <typename Tobj>
50  class ValidPtr {
51  public:
53  explicit ValidPtr(Tobj * p)
54  : ptr(p), inMemory(true), haveDataStructure(false){}
56  delete ptr;
57  }
58 
59  /* Pointer can not be changed only object pointed to.
60  * Therefore this is a const operation.
61  * Note that if Tobj is const it can not be changed of course.
62  */
63  Tobj & operator*() const {
64  if (!inMemory)
65  throw Failure("ValidPtr::operator*() const: "
66  "Attempt to access invalid object. "
67  "Object is on file.");
68  if (!haveDataStructure)
69  throw Failure("ValidPtr::operator*() const: "
70  "Attempt to access invalid object. "
71  "Do not have data structure.");
72  return *ptr;
73  }
74 
75  Tobj * operator->() const {
76  if (!inMemory)
77  throw Failure("ValidPtr::operator->() const: "
78  "Attempt to access invalid pointer."
79  "Object is on file.");
80  if (!haveDataStructure)
81  throw Failure("ValidPtr::operator->() const: "
82  "Attempt to access invalid pointer. "
83  "Do not have data structure.");
84  return ptr;
85  }
86 
89  const Tobj & getConstRefForCopying() const {
90  return *ptr;
91  }
92 
93  inline void inMemorySet(bool val) {
94  inMemory = val;
95  }
96  inline bool inMemoryGet() const {
97  return inMemory;
98  }
99  inline void haveDataStructureSet(bool val) {
100  haveDataStructure = val;
101  }
102  inline bool haveDataStructureGet() const {
103  return haveDataStructure;
104  }
105 
106  static void swap( ValidPtr<Tobj> & ptrA, ValidPtr<Tobj> & ptrB ) {
107  // For the moment, we do not allow swapping ptrs with objs
108  // written to file. This could be a feature to add but would
109  // require swapping filenames.
110  if ( !ptrA.inMemoryGet() || !ptrB.inMemoryGet() )
111  throw "Swap called for objects not in memory";
112  if ( !ptrA.haveDataStructureGet() || !ptrB.haveDataStructureGet() )
113  throw "Swap called for objects without data structure";
114  Tobj * tmpPtr = ptrA.ptr;
115  ptrA.ptr = ptrB.ptr;
116  ptrB.ptr = tmpPtr;
117  }
118  protected:
119  Tobj * ptr;
121  bool inMemory;
124  private:
125  ValidPtr<Tobj>(ValidPtr<Tobj> const &) {}
127  };
128 
129 } /* end namespace mat */
130 #endif
mat::ValidPtr
Smart pointer class to control access to object.
Definition: ValidPtr.h:50
mat::ValidPtr::operator*
Tobj & operator*() const
Definition: ValidPtr.h:63
mat::Failure
Definition: Failure.h:57
mat::ValidPtr::haveDataStructureSet
void haveDataStructureSet(bool val)
Definition: ValidPtr.h:99
mat::ValidPtr::swap
static void swap(ValidPtr< Tobj > &ptrA, ValidPtr< Tobj > &ptrB)
Definition: ValidPtr.h:106
mat::ValidPtr::inMemoryGet
bool inMemoryGet() const
Definition: ValidPtr.h:96
mat::ValidPtr::~ValidPtr
~ValidPtr()
Definition: ValidPtr.h:55
mat
Definition: allocate.cc:39
mat::ValidPtr::inMemorySet
void inMemorySet(bool val)
Definition: ValidPtr.h:93
mat::ValidPtr::ValidPtr
ValidPtr(Tobj *p)
Copy ordinary pointer constructor.
Definition: ValidPtr.h:53
mat::ValidPtr::haveDataStructure
bool haveDataStructure
Access to ptr forbidden if haveDataStructure is false.
Definition: ValidPtr.h:123
mat::ValidPtr::ptr
Tobj * ptr
Definition: ValidPtr.h:119
mat::ValidPtr::operator=
ValidPtr< Tobj > & operator=(ValidPtr< Tobj > const &)
Definition: ValidPtr.h:126
mat::ValidPtr::operator->
Tobj * operator->() const
Definition: ValidPtr.h:75
mat::ValidPtr::getConstRefForCopying
const Tobj & getConstRefForCopying() const
getConstRefForCopying() is provided to make it possible to copy the object also when it is written to...
Definition: ValidPtr.h:89
mat::ValidPtr::inMemory
bool inMemory
Access to ptr forbidden if inMemory is false.
Definition: ValidPtr.h:121
mat::ValidPtr::haveDataStructureGet
bool haveDataStructureGet() const
Definition: ValidPtr.h:102