Isis 3 Programmer Reference
EmbreeTargetManager.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #include <QtGlobal>
9 #include <QCoreApplication>
10 
11 #include "FileName.h"
12 #include "IException.h"
13 
14 #include "EmbreeTargetManager.h"
15 
16 using namespace std;
17 
18 namespace Isis {
19 
23  EmbreeTargetManager *EmbreeTargetManager::m_maker = 0;
24 
25 
30  EmbreeTargetManager::EmbreeTargetManager()
31  : m_maxCacheSize(10) {
32  // This ensures this singleton is shut down when the application exists
33  qAddPostRoutine(DieAtExit);
34  return;
35  }
36 
37 
42  QList<QString> targetFiles = m_targeCache.keys();
43  for (int i = 0; i < targetFiles.size(); i++) {
44  removeTargetShape(targetFiles[i]);
45  }
46  }
47 
48 
61  delete m_maker;
62  m_maker = 0;
63  return;
64  }
65 
66 
78  if (!m_maker) {
80  }
81  return (m_maker);
82  }
83 
84 
94  QString EmbreeTargetManager::fullFilePath(const QString &filePath) const {
95  FileName fileName(filePath);
96  return fileName.expanded();
97  }
98 
99 
129  EmbreeTargetShape *EmbreeTargetManager::create(const QString &shapeFile) {
130  // Get the full path to the file
131  QString fullPath = fullFilePath(shapeFile);
132 
133  // If the an EmbreeTargetShape already exists, increment its reference count
134  // and return a pointer.
135  if ( inCache(fullPath) ) {
136  ++(m_targeCache[fullPath].m_referenceCount);
137  return ( m_targeCache[fullPath].m_targetShape );
138  }
139 
140  // Otherwise, make a new EmbreeTargetShape
141 
142  // First check how many already exist
143  if ( m_targeCache.size() >= maxCacheSize() ) {
144  QString msg = "Failed creating EmbreeTargetShape for [" + shapeFile
145  + "] Too many EmbreeTargetShapes are already open.";
146  throw IException(IException::Programmer, msg, _FILEINFO_);
147  }
148 
149  // If there's still space make a new one
150  EmbreeTargetShape *targetShape = new EmbreeTargetShape(fullPath);
151  EmbreeTargetShapeContainer targetShapeContainer(fullPath, targetShape);
152  ++(targetShapeContainer.m_referenceCount);
153  m_targeCache.insert(fullPath, targetShapeContainer);
154  return ( targetShapeContainer.m_targetShape );
155  }
156 
157 
173  void EmbreeTargetManager::free(const QString &shapeFile) {
174  // Get the full path to the file
175  QString fullPath = fullFilePath(shapeFile);
176 
177  // Sanity check
178  if ( !inCache(fullPath) ) {
179  QString msg = "Cannot free EmbreeTargetShape for file ["
180  + fullPath + "] because it is not stored in the cache.";
181  throw IException(IException::Programmer, msg, _FILEINFO_);
182  }
183 
184  // Get the container for the shapeFile
185  EmbreeTargetShapeContainer targetContainer = m_targeCache[fullPath];
186 
187  // Decrement the reference count
188  int newCount = --(m_targeCache[fullPath].m_referenceCount);
189 
190  // if the EmbreeTargetShape is no longer in use, delete it
191  if ( newCount < 1 ) {
192  removeTargetShape(fullPath);
193  }
194  }
195 
196 
210  void EmbreeTargetManager::removeTargetShape(const QString &shapeFile) {
211  // Get the full path to the file
212  QString fullPath = fullFilePath(shapeFile);
213 
214  // Sanity check
215  if ( !inCache(fullPath) ) {
216  QString msg = "Cannot free EmbreeTargetShape for file ["
217  + fullPath + "] because it is not stored in the cache.";
218  throw IException(IException::Programmer, msg, _FILEINFO_);
219  }
220 
221  // Get the container for the shapeFile
222  EmbreeTargetShapeContainer targetContainer = m_targeCache[fullPath];
223  delete targetContainer.m_targetShape;
224  m_targeCache.remove(fullPath);
225  }
226 
227 
234  return m_targeCache.size();
235  }
236 
237 
244  return m_maxCacheSize;
245  }
246 
247 
256  void EmbreeTargetManager::setMaxCacheSize(const int &numShapes) {
257  m_maxCacheSize = numShapes;
258  }
259 
260 
266  bool EmbreeTargetManager::inCache(const QString &shapeFile) const{
267  return m_targeCache.contains( fullFilePath(shapeFile) );
268  }
269 }
Isis::EmbreeTargetManager::m_maker
static EmbreeTargetManager * m_maker
Initialize the singleton factory pointer.
Definition: EmbreeTargetManager.h:96
Isis::EmbreeTargetManager::fullFilePath
QString fullFilePath(const QString &filePath) const
Helper function that takes a file path and returns the full file path.
Definition: EmbreeTargetManager.cpp:94
Isis::EmbreeTargetManager::maxCacheSize
int maxCacheSize() const
Return the maximum number of stored EmbreeTargetShapes.
Definition: EmbreeTargetManager.cpp:243
Isis::EmbreeTargetManager::EmbreeTargetShapeContainer
Reference counting container for EmbreeTargetShapes.
Definition: EmbreeTargetManager.h:65
Isis::EmbreeTargetManager::EmbreeTargetShapeContainer::m_targetShape
EmbreeTargetShape * m_targetShape
!< The full path to the file used to construct the EmbreeTargetShape.
Definition: EmbreeTargetManager.h:88
QList< QString >
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::EmbreeTargetManager::removeTargetShape
void removeTargetShape(const QString &shapeFile)
Method for removing an EmbreeTargetShape from the internal cache.
Definition: EmbreeTargetManager.cpp:210
Isis::EmbreeTargetManager::DieAtExit
static void DieAtExit()
Exit termination routine.
Definition: EmbreeTargetManager.cpp:60
Isis::EmbreeTargetManager::inCache
bool inCache(const QString &shapeFile) const
Check if there is an already created EmbreeTargetShape for a file.
Definition: EmbreeTargetManager.cpp:266
Isis::EmbreeTargetManager::currentCacheSize
int currentCacheSize() const
Return the number of currently stored EmbreeTargetShapes.
Definition: EmbreeTargetManager.cpp:233
Isis::EmbreeTargetManager::EmbreeTargetShapeContainer::m_referenceCount
int m_referenceCount
!< The EmbreeTargetShape.
Definition: EmbreeTargetManager.h:89
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::EmbreeTargetManager
Class for managing the construction and destruction of EmbreeTargetShapes.
Definition: EmbreeTargetManager.h:37
Isis::EmbreeTargetManager::getInstance
static EmbreeTargetManager * getInstance()
Retrieve reference to Singleton instance of this object.
Definition: EmbreeTargetManager.cpp:77
Isis::EmbreeTargetManager::EmbreeTargetManager
EmbreeTargetManager()
This constructor will initialize the EmbreeTargetManager object to default values.
Definition: EmbreeTargetManager.cpp:30
Isis::EmbreeTargetManager::create
EmbreeTargetShape * create(const QString &shapeFile)
Get a pointer to an EmbreeTargetShape containing the information from a shape file.
Definition: EmbreeTargetManager.cpp:129
Isis::EmbreeTargetManager::~EmbreeTargetManager
~EmbreeTargetManager()
Destructor that frees all of the EmbreeTargetShapes managed by this object.
Definition: EmbreeTargetManager.cpp:41
Isis::EmbreeTargetManager::m_maxCacheSize
int m_maxCacheSize
!< The cache of created target shapes.
Definition: EmbreeTargetManager.h:99
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::EmbreeTargetShape
Embree Target Shape for planetary bodies.
Definition: EmbreeTargetShape.h:139
Isis::EmbreeTargetManager::setMaxCacheSize
void setMaxCacheSize(const int &numShapes)
Set the maximum number of stored EmbreeTargetShapes.
Definition: EmbreeTargetManager.cpp:256
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
Isis::EmbreeTargetManager::m_targeCache
QMap< QString, EmbreeTargetShapeContainer > m_targeCache
!< Pointer to the singleton factory.
Definition: EmbreeTargetManager.h:97
std
Namespace for the standard library.
Isis::EmbreeTargetManager::free
void free(const QString &shapeFile)
Notify the manager that an EmbreeTargetShape is no longer in use.
Definition: EmbreeTargetManager.cpp:173
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16