ergo
ValidPtr.h
Go to the documentation of this file.
1 /* Ergo, version 3.2, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2012 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Primary academic reference:
19  * Kohn−Sham Density Functional Theory Electronic Structure Calculations
20  * with Linearly Scaling Computational Time and Memory Usage,
21  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
22  * J. Chem. Theory Comput. 7, 340 (2011),
23  * <http://dx.doi.org/10.1021/ct100611z>
24  *
25  * For further information about Ergo, see <http://www.ergoscf.org>.
26  */
27 
36 #ifndef MAT_VALIDPTR
37 #define MAT_VALIDPTR
38 namespace mat {
39 
40 
47  template <typename Tobj>
48  class ValidPtr {
49  public:
51  explicit ValidPtr(Tobj * p)
52  : ptr(p), inMemory(true), haveDataStructure(false){}
54  delete ptr;
55  }
56 
57  /* Pointer can not be changed only object pointed to.
58  * Therefore this is a const operation.
59  * Note that if Tobj is const it can not be changed of course.
60  */
61  Tobj & operator*() const {
62  if (!inMemory)
63  throw Failure("ValidPtr::operator*() const: "
64  "Attempt to access invalid object. "
65  "Object is on file.");
66  if (!haveDataStructure)
67  throw Failure("ValidPtr::operator*() const: "
68  "Attempt to access invalid object. "
69  "Do not have data structure.");
70  return *ptr;
71  }
72 
73  Tobj * operator->() const {
74  if (!inMemory)
75  throw Failure("ValidPtr::operator->() const: "
76  "Attempt to access invalid pointer."
77  "Object is on file.");
78  if (!haveDataStructure)
79  throw Failure("ValidPtr::operator->() const: "
80  "Attempt to access invalid pointer. "
81  "Do not have data structure.");
82  return ptr;
83  }
84 
87  const Tobj & getConstRefForCopying() const {
88  return *ptr;
89  }
90 
91  inline void inMemorySet(bool val) {
92  inMemory = val;
93  }
94  inline bool inMemoryGet() const {
95  return inMemory;
96  }
97  inline void haveDataStructureSet(bool val) {
98  haveDataStructure = val;
99  }
100  inline bool haveDataStructureGet() const {
101  return haveDataStructure;
102  }
103 
104  static void swap( ValidPtr<Tobj> & ptrA, ValidPtr<Tobj> & ptrB ) {
105  // For the moment, we do not allow swapping ptrs with objs
106  // written to file. This could be a feature to add but would
107  // require swapping filenames.
108  if ( !ptrA.inMemoryGet() || !ptrB.inMemoryGet() )
109  throw "Swap called for objects not in memory";
110  if ( !ptrA.haveDataStructureGet() || !ptrB.haveDataStructureGet() )
111  throw "Swap called for objects without data structure";
112  Tobj * tmpPtr = ptrA.ptr;
113  ptrA.ptr = ptrB.ptr;
114  ptrB.ptr = tmpPtr;
115  }
116  protected:
117  Tobj * ptr;
119  bool inMemory;
122  private:
125  };
126 
127 } /* end namespace mat */
128 #endif