Isis 3 Programmer Reference
EmbreeTargetManager.cpp
Go to the documentation of this file.
1 
25 #include <QtGlobal>
26 #include <QCoreApplication>
27 
28 #include "FileName.h"
29 #include "IException.h"
30 
31 #include "EmbreeTargetManager.h"
32 
33 using namespace std;
34 
35 namespace Isis {
36 
40  EmbreeTargetManager *EmbreeTargetManager::m_maker = 0;
41 
42 
47  EmbreeTargetManager::EmbreeTargetManager()
48  : m_maxCacheSize(10) {
49  // This ensures this singleton is shut down when the application exists
50  qAddPostRoutine(DieAtExit);
51  return;
52  }
53 
54 
59  QList<QString> targetFiles = m_targeCache.keys();
60  for (int i = 0; i < targetFiles.size(); i++) {
61  removeTargetShape(targetFiles[i]);
62  }
63  }
64 
65 
78  delete m_maker;
79  m_maker = 0;
80  return;
81  }
82 
83 
95  if (!m_maker) {
97  }
98  return (m_maker);
99  }
100 
101 
111  QString EmbreeTargetManager::fullFilePath(const QString &filePath) const {
112  FileName fileName(filePath);
113  return fileName.expanded();
114  }
115 
116 
146  EmbreeTargetShape *EmbreeTargetManager::create(const QString &shapeFile) {
147  // Get the full path to the file
148  QString fullPath = fullFilePath(shapeFile);
149 
150  // If the an EmbreeTargetShape already exists, increment its reference count
151  // and return a pointer.
152  if ( inCache(fullPath) ) {
153  ++(m_targeCache[fullPath].m_referenceCount);
154  return ( m_targeCache[fullPath].m_targetShape );
155  }
156 
157  // Otherwise, make a new EmbreeTargetShape
158 
159  // First check how many already exist
160  if ( m_targeCache.size() >= maxCacheSize() ) {
161  QString msg = "Failed creating EmbreeTargetShape for [" + fullPath
162  + "] Too many EmbreeTargetShapes are already open.";
164  }
165 
166  // If there's still space make a new one
167  EmbreeTargetShape *targetShape = new EmbreeTargetShape(fullPath);
168  EmbreeTargetShapeContainer targetShapeContainer(fullPath, targetShape);
169  ++(targetShapeContainer.m_referenceCount);
170  m_targeCache.insert(fullPath, targetShapeContainer);
171  return ( targetShapeContainer.m_targetShape );
172  }
173 
174 
190  void EmbreeTargetManager::free(const QString &shapeFile) {
191  // Get the full path to the file
192  QString fullPath = fullFilePath(shapeFile);
193 
194  // Sanity check
195  if ( !inCache(fullPath) ) {
196  QString msg = "Cannot free EmbreeTargetShape for file ["
197  + fullPath + "] because it is not stored in the cache.";
199  }
200 
201  // Get the container for the shapeFile
202  EmbreeTargetShapeContainer targetContainer = m_targeCache[fullPath];
203 
204  // Decrement the reference count
205  int newCount = --(m_targeCache[fullPath].m_referenceCount);
206 
207  // if the EmbreeTargetShape is no longer in use, delete it
208  if ( newCount < 1 ) {
209  removeTargetShape(fullPath);
210  }
211  }
212 
213 
227  void EmbreeTargetManager::removeTargetShape(const QString &shapeFile) {
228  // Get the full path to the file
229  QString fullPath = fullFilePath(shapeFile);
230 
231  // Sanity check
232  if ( !inCache(fullPath) ) {
233  QString msg = "Cannot free EmbreeTargetShape for file ["
234  + fullPath + "] because it is not stored in the cache.";
236  }
237 
238  // Get the container for the shapeFile
239  EmbreeTargetShapeContainer targetContainer = m_targeCache[fullPath];
240  delete targetContainer.m_targetShape;
241  m_targeCache.remove(fullPath);
242  }
243 
244 
251  return m_targeCache.size();
252  }
253 
254 
261  return m_maxCacheSize;
262  }
263 
264 
273  void EmbreeTargetManager::setMaxCacheSize(const int &numShapes) {
274  m_maxCacheSize = numShapes;
275  }
276 
277 
283  bool EmbreeTargetManager::inCache(const QString &shapeFile) const{
284  return m_targeCache.contains( fullFilePath(shapeFile) );
285  }
286 }
File name manipulation and expansion.
Definition: FileName.h:116
void free(const QString &shapeFile)
Notify the manager that an EmbreeTargetShape is no longer in use.
QMap< QString, EmbreeTargetShapeContainer > m_targeCache
!< Pointer to the singleton factory.
Reference counting container for EmbreeTargetShapes.
Namespace for the standard library.
QString fullFilePath(const QString &filePath) const
Helper function that takes a file path and returns the full file path.
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
void removeTargetShape(const QString &shapeFile)
Method for removing an EmbreeTargetShape from the internal cache.
Class for managing the construction and destruction of EmbreeTargetShapes.
bool inCache(const QString &shapeFile) const
Check if there is an already created EmbreeTargetShape for a file.
static EmbreeTargetManager * getInstance()
Retrieve reference to Singleton instance of this object.
int m_maxCacheSize
!< The cache of created target shapes.
EmbreeTargetShape * m_targetShape
!< The full path to the file used to construct the EmbreeTargetShape.
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:212
Embree Target Shape for planetary bodies.
int maxCacheSize() const
Return the maximum number of stored EmbreeTargetShapes.
static void DieAtExit()
Exit termination routine.
~EmbreeTargetManager()
Destructor that frees all of the EmbreeTargetShapes managed by this object.
int currentCacheSize() const
Return the number of currently stored EmbreeTargetShapes.
static EmbreeTargetManager * m_maker
Initialize the singleton factory pointer.
Isis exception class.
Definition: IException.h:107
EmbreeTargetManager()
This constructor will initialize the EmbreeTargetManager object to default values.
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void setMaxCacheSize(const int &numShapes)
Set the maximum number of stored EmbreeTargetShapes.
EmbreeTargetShape * create(const QString &shapeFile)
Get a pointer to an EmbreeTargetShape containing the information from a shape file.