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