Isis 3 Programmer Reference
CubeManager.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "CubeManager.h"
8 
9 #include <iostream>
10 #include <sys/resource.h>
11 #include <cstdlib> // atexit
12 #include <memory> // smart pointers
13 #include <QCoreApplication>
14 
15 #include "Cube.h"
16 #include "CubeAttribute.h"
17 #include "CubeManager.h"
18 #include "FileName.h"
19 #include "IString.h"
20 
21 namespace Isis {
22  CubeManager *CubeManager::p_instance = 0;
23 
29 
30  // Grab the Qcore application instace to check if it has been
31  // instantiated yet. If not, instantiate it.
32  if (QCoreApplication::instance() == 0) {
33  static char **argv = 0;
34 
35  if(!argv) {
36  argv = new char*[2];
37  argv[0] = new char[1024];
38  strcpy(argv[0], "CubeManager");
39  argv[1] = 0;
40  }
41 
42  static int argc = 1;
43  new QCoreApplication(argc, argv);
44  }
45 
46  // Get the maximum allowable number of open files for a process
47  struct rlimit fileLimit;
48 
49  if (getrlimit(RLIMIT_NOFILE, &fileLimit) != 0) {
50  QString msg = "Cannot read the maximum allowable open files from system resources.";
51  throw IException(IException::Programmer, msg, _FILEINFO_);
52  }
53 
54  // Allow for library files, etc used by this process
55  // So set or file limit to 60% of maximum allowed number of opened files
56  p_maxOpenFiles = fileLimit.rlim_cur * .60;
58 
59  // Add the CleanUp() call to QCoreApplication's clean up routine,
60  // this ensures that the static instance of CubeManager is cleaned
61  // up with the rest of ISIS before System application clean up begins
62  // in order to ensure clean destruction. If System deletes CubeManager's
63  // Cubes or any of the individual Cube's memebers before CubeManager gets
64  // deleted, it will cause a segfault.
65  qAddPostRoutine(CleanUp);
66  }
67 
73  CleanCubes();
74  }
75 
76 
95  Cube *CubeManager::OpenCube(const QString &cubeFileName) {
96  CubeAttributeInput attIn(cubeFileName);
97  IString attri = attIn.toString();
98  IString expName = FileName(cubeFileName).expanded();
99 
100  // If there are attributes, we need a plus sign on the name
101  if (attri.size() > 0) {
102  expName += "+";
103  }
104 
105  IString fullName = expName + attri;
106  QString fileName(fullName.ToQt());
107  QMap<QString, Cube *>::iterator searchResult = p_cubes.find(fileName);
108 
109  if (searchResult == p_cubes.end()) {
110  p_cubes.insert(fileName, new Cube());
111  searchResult = p_cubes.find(fileName);
112  // Bands are the only thing input attributes can affect
113  (*searchResult)->setVirtualBands(attIn.bands());
114 
115  // Need to clean up memory if there is a problem opening a cube
116  // This allows the CubeManager class to clean up the dynamically alloc'd
117  // Cube before rethrowing the exception from Cube's open method
118  try {
119  (*searchResult)->open(fileName);
120  }
121  catch (IException &e) {
122  CleanCubes(fileName);
123  throw;
124  }
125  }
126 
127  // Keep track of the newly opened cube in our queue
128  p_opened.removeAll(fileName);
129  p_opened.enqueue(fileName);
130 
131  // cleanup excess cubes
132  while (p_opened.size() > (int)(p_currentLimit)) {
133  QString needsCleaned = p_opened.dequeue();
134  CleanCubes(needsCleaned);
135  }
136 
137  return (*searchResult);
138  }
139 
140 
148  void CubeManager::CleanCubes(const QString &cubeFileName) {
149 
150  QString fileName(FileName(cubeFileName).expanded());
151  QMap<QString, Cube *>::iterator searchResult = p_cubes.find(fileName);
152 
153  if (searchResult == p_cubes.end()) {
154  return;
155  }
156 
157  delete searchResult.value();
158  searchResult.value() = NULL;
159  p_cubes.erase(searchResult);
160  }
161 
162 
169 
170  while (pos != p_cubes.end()) {
171  delete pos.value();
172  pos.value() = NULL;
173  pos ++;
174  }
175 
176  p_cubes.clear();
177  }
178 }
Isis::CubeManager::~CubeManager
~CubeManager()
This is the CubeManager destructor.
Definition: CubeManager.cpp:72
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::CubeManager::p_cubes
QMap< QString, Cube * > p_cubes
This keeps track of the open cubes.
Definition: CubeManager.h:145
Isis::CubeManager::p_instance
static CubeManager * p_instance
There is always at least one instance of CubeManager around.
Definition: CubeManager.h:142
Isis::CubeManager::p_maxOpenFiles
unsigned int p_maxOpenFiles
60% of the maximum number of open files allowed by system resources
Definition: CubeManager.h:154
Isis::CubeManager::OpenCube
Cube * OpenCube(const QString &cubeFileName)
This method opens a cube.
Definition: CubeManager.cpp:95
Isis::CubeManager::CleanUp
static void CleanUp()
This method calls CleanCubes() on the static instance.
Definition: CubeManager.h:130
Isis::FileName::expanded
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:196
Isis::CubeManager::p_currentLimit
unsigned int p_currentLimit
The current limit regarding number of open files allowed.
Definition: CubeManager.h:151
Isis::CubeManager::p_opened
QQueue< QString > p_opened
This keeps track of cubes that have been opened.
Definition: CubeManager.h:148
Isis::Cube
IO Handler for Isis Cubes.
Definition: Cube.h:167
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::CubeAttributeInput::bands
std::vector< QString > bands() const
Return a vector of the input bands specified.
Definition: CubeAttribute.cpp:82
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
Isis::CubeAttributeInput
Manipulate and parse attributes of input cube filenames.
Definition: CubeAttribute.h:381
QMap
This is free and unencumbered software released into the public domain.
Definition: CubeIoHandler.h:22
Isis::IString
Adds specific functionality to C++ strings.
Definition: IString.h:165
Isis::CubeManager::CubeManager
CubeManager()
This initializes a CubeManager object.
Definition: CubeManager.cpp:28
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::IString::ToQt
QString ToQt() const
Retuns the object string as a QString.
Definition: IString.cpp:869
Isis::CubeManager::CleanCubes
void CleanCubes()
This method removes all cubes from memory.
Definition: CubeManager.cpp:167