opennurbs_workspace.h
Go to the documentation of this file.
00001 /* $NoKeywords: $ */
00002 /*
00003 //
00004 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
00005 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
00006 // McNeel & Associates.
00007 //
00008 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
00009 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
00010 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
00011 //                              
00012 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
00013 //
00015 */
00016 
00017 #if !defined(OPENNURBS_WORKSPACE_INC_)
00018 #define OPENNURBS_WORKSPACE_INC_
00019 
00020 /*
00021 Description:
00022   Use ON_Workspace classes on the stack to efficiently get 
00023   and automatically clean up workspace memory and scratch 
00024   files.
00025 */
00026 class ON_CLASS ON_Workspace
00027 {
00028 public:
00029   /*
00030   Description:
00031     ON_Workspace classes should be on the stack
00032     or as members on classes that are never copied.
00033     The destructor frees memory that was allocated by
00034     ON_Workspace::GetMemory and closes files that were 
00035     opened with ON_Workspace::OpenFile.
00036   */
00037   ON_Workspace();
00038 
00039   /*
00040   Description:
00041     The destructor frees memory that was allocated by
00042     ON_Workspace::GetMemory and closes files that were 
00043     opened with ON_Workspace::OpenFile.
00044   */
00045   ~ON_Workspace();
00046 
00047 
00048   /*
00049   Description:
00050     The destructor frees memory that was allocated by
00051     ON_Workspace::GetMemory and closes files that were 
00052     opened with ON_Workspace::OpenFile.  The workspace
00053     can be used again after calling destroy.
00054   */
00055   void Destroy();
00056 
00057   /*
00058   Description:
00059     Gets a block of heap memory that will be freed by 
00060     ~ON_Workspace. The intent of ON_Workspace::GetMemory
00061     is to provide an easy way to get blocks of scratch 
00062     memory without having to worry about cleaning up 
00063     before returning.
00064   Parameters:
00065     sz - [in] (>0) size of memory block in bytes. 
00066               If sz <= 0, then NULL is returned.
00067   Returns:
00068     A pointer to the memory block.
00069   Remarks.
00070     onmalloc() is used to get the block of memory.
00071     Do NOT free the pointer returned by GetMemory().
00072     ~ON_Workspace() will free the memory.  If you decide
00073     you want to keep the memory block, pass the pointer
00074     to KeepMemory before ~ON_Workspace is called.
00075   See Also:
00076     ON_Workspace::::~ON_Workspace
00077     ON_Workspace::KeepMemory
00078     ON_Workspace::GrowMemory
00079     ON_Workspace::GetIntMemory
00080     ON_Workspace::GetDoubleMemory
00081     ON_Workspace::GetPointMemory
00082     ON_Workspace::GetVectorMemory
00083   */
00084   void* GetMemory( size_t sz );
00085 
00086   /*
00087   Description:
00088     Gets an array of integers that will be freed by ~ON_Workspace.
00089     The intent of ON_Workspace::GetIntMemory is to provide
00090     an easy way to get scratch integer arrays without
00091     having to worry about cleaning up before returning.
00092   Parameters:
00093     count - [in] (>0) number of integers in memory block.
00094               If count <= 0, then NULL is returned.
00095   Returns:
00096     A pointer to the array of integers.
00097   Remarks.
00098     This is a simple helper function so you don't have to
00099     mess around with (int*) casts and sizeof(int)s in a call
00100     to GetMemory().  It is exactly like calling
00101     (int*)GetMemory(count*sizeof(int));
00102   See Also:
00103     ON_Workspace::GetMemory
00104     ON_Workspace::KeepMemory
00105     ON_Workspace::GrowIntMemory
00106   */
00107   int* GetIntMemory( size_t count );
00108 
00109   /*
00110   Description:
00111     Gets an matrix of integers
00112   Parameters:
00113     row_count - [in] (>0) number of  rows
00114     col_count - [in] (>0) number of columns
00115   Returns:
00116     A pointer p so that p[i][j] is an integer when
00117     0 <= i < row_count and 0 <= j < col_count.
00118   Remarks.
00119     This is a simple helper function so you don't have to
00120     mess around building the 2d array.
00121   See Also:
00122     ON_Workspace::KeepMemory
00123   */
00124   int** GetIntMemory( size_t row_count, size_t col_count );
00125 
00126   /*
00127   Description:
00128     Gets an array of doubles that will be freed by ~ON_Workspace.
00129     The intent of ON_Workspace::GetDoubleMemory is to provide
00130     an easy way to get scratch double arrays without
00131     having to worry about cleaning up before returning.
00132   Parameters:
00133     count - [in] (>0) number of doubles in memory block.
00134               If count <= 0, then NULL is returned.
00135   Returns:
00136     A pointer to the array of doubles.
00137   Remarks.
00138     This is a simple helper function so you don't have to
00139     mess around with (double*) casts and sizeof(double)s 
00140     in a call to GetMemory().  It is exactly like calling
00141     (double*)GetMemory(count*sizeof(double));
00142   See Also:
00143     ON_Workspace::GetMemory
00144     ON_Workspace::KeepMemory
00145     ON_Workspace::GrowIntMemory
00146   */
00147   double* GetDoubleMemory( size_t count );
00148 
00149   /*
00150   Description:
00151     Gets an matrix of doubles
00152   Parameters:
00153     row_count - [in] (>0) number of  rows
00154     col_count - [in] (>0) number of columns
00155   Returns:
00156     A pointer p so that p[i][j] is an double when
00157     0 <= i < row_count and 0 <= j < col_count.
00158   Remarks.
00159     This is a simple helper function so you don't have to
00160     mess around building the 2d array.
00161   See Also:
00162     ON_Workspace::KeepMemory
00163   */
00164   double** GetDoubleMemory( size_t row_count, size_t col_count );
00165 
00166   /*
00167   Description:
00168     Gets an array of ON_3dPoints that will be freed by ~ON_Workspace.
00169     The intent of ON_Workspace::GetPointMemory is to 
00170     provide an easy way to get scratch point arrays without
00171     having to worry about cleaning up before returning.
00172   Parameters:
00173     count - [in] (>0) number of points in memory block.
00174               If count <= 0, then NULL is returned.
00175   Returns:
00176     A pointer to the memory block.
00177   Remarks.
00178     This is a simple helper function so you don't have to
00179     mess around with (ON_3dPoint*) casts and sizeof(ON_3dPoint)s
00180     in a call to GetMemory().  It is exactly like calling
00181     (ON_3dPoint*)GetMemory(count*sizeof(ON_3dPoint));
00182   See Also:
00183     ON_Workspace::GetMemory
00184     ON_Workspace::KeepMemory
00185     ON_Workspace::GrowIntMemory
00186   */
00187   ON_3dPoint* GetPointMemory( size_t count );
00188 
00189   /*
00190   Description:
00191     Gets an array of ON_3dVectors that will be freed by ~ON_Workspace.
00192     The intent of ON_Workspace::GetVectorMemory is to 
00193     provide an easy way to get scratch Vector arrays without
00194     having to worry about cleaning up before returning.
00195   Parameters:
00196     count - [in] (>0) number of Vectors in memory block.
00197               If count <= 0, then NULL is returned.
00198   Returns:
00199     A pointer to the memory block.
00200   Remarks.
00201     This is a simple helper function so you don't have to
00202     mess around with (ON_3dVector*) casts and sizeof(ON_3dVector)s
00203     in a call to GetMemory().  It is exactly like calling
00204     (ON_3dVector*)GetMemory(count*sizeof(ON_3dVector));
00205   See Also:
00206     ON_Workspace::GetMemory
00207     ON_Workspace::KeepMemory
00208     ON_Workspace::GrowIntMemory
00209   */
00210   ON_3dVector* GetVectorMemory( size_t count );
00211 
00212   /*
00213   Description:
00214     Grows a block of heap memory that was allocated by
00215     ON_Workspace::GetMemory.
00216   Parameters:
00217     ptr - [in] pointer returned by an earlier call to
00218                GetMemory or GrowMemory.
00219     sz - [in] (>0) size of memory block in bytes. 
00220               If sz <= 0, then NULL is returned.
00221               If ptr is not NULL and was not allocated by an 
00222               earlier call to GetMemory or GrowMemory, then
00223               NULL is returned.
00224   Returns:
00225     A pointer to the memory block.
00226   Remarks.
00227     onrealloc() is used to grow the block of memory.
00228     Do NOT free the pointer returned by GrowMemory().
00229     ~ON_Workspace() will free the memory.  If you decide
00230     you want to keep the memory block, pass the pointer
00231     to KeepMemory before ~ON_Workspace is called.
00232   See Also:
00233     ON_Workspace::GetMemory
00234     ON_Workspace::KeepMemory
00235     ON_Workspace::GrowIntMemory
00236     ON_Workspace::GrowDoubleMemory
00237     ON_Workspace::GrowPointMemory
00238     ON_Workspace::GrowVectorMemory
00239   */
00240   void* GrowMemory( void* ptr, size_t sz );
00241 
00242   /*
00243   Description:
00244     Grows the array of integers that was allocated by
00245     GetIntMemory or GrowIntMemory.
00246   Parameters:
00247     ptr - [in] pointer returned by an earlier call to
00248                GetIntMemory or GrowIntMemory.
00249     count - [in] (>0) number of integers in memory block.
00250               If count <= 0, then NULL is returned.
00251               If ptr was not allocated by this ON_Workspace
00252               class, then NULL is returned.
00253   Returns:
00254     A pointer to the integer array.
00255   Remarks.
00256     onrealloc() is used to grow the block of memory.
00257     Do NOT free the pointer returned by GrowIntMemory().
00258     ~ON_Workspace() will free the memory.  If you decide
00259     you want to keep the memory block, pass the pointer
00260     to KeepMemory before ~ON_Workspace is called.
00261   See Also:
00262     ON_Workspace::GetIntMemory
00263     ON_Workspace::KeepMemory
00264   */
00265   int* GrowIntMemory( int* ptr, size_t count );
00266 
00267   /*
00268   Description:
00269     Grows the array of doubles that was allocated by
00270     GetDoubleMemory or GrowDoubleMemory.
00271   Parameters:
00272     ptr - [in] pointer returned by an earlier call to
00273                GetDoubleMemory or GrowDoubleMemory.
00274     count - [in] (>0) number of doubles in memory block.
00275               If count <= 0, then NULL is returned.
00276               If ptr was not allocated by this ON_Workspace
00277               class, then NULL is returned.
00278   Returns:
00279     A pointer to the double array.
00280   Remarks.
00281     onrealloc() is used to grow the block of memory.
00282     Do NOT free the pointer returned by GrowDoubleMemory().
00283     ~ON_Workspace() will free the memory.  If you decide
00284     you want to keep the memory block, pass the pointer
00285     to KeepMemory before ~ON_Workspace is called.
00286   See Also:
00287     ON_Workspace::GetDoubleMemory
00288     ON_Workspace::KeepMemory
00289   */
00290   double* GrowDoubleMemory( double* ptr, size_t count );
00291 
00292   /*
00293   Description:
00294     Grows the array of points that was allocated by
00295     GetPointMemory or GrowPointMemory.
00296   Parameters:
00297     ptr - [in] pointer returned by an earlier call to
00298                GetPointMemory or GrowPointMemory.
00299     count - [in] (>0) number of points in memory block.
00300               If count <= 0, then NULL is returned.
00301               If ptr was not allocated by this ON_Workspace
00302               class, then NULL is returned.
00303   Returns:
00304     A pointer to the point array.
00305   Remarks.
00306     onrealloc() is used to grow the block of memory.
00307     Do NOT free the pointer returned by GrowMemory().
00308     ~ON_Workspace() will free the memory.  If you decide
00309     you want to keep the memory block, pass the pointer
00310     to KeepMemory before ~ON_Workspace is called.
00311   See Also:
00312     ON_Workspace::GetPointMemory
00313     ON_Workspace::KeepMemory
00314   */
00315   ON_3dPoint* GrowPointMemory( ON_3dPoint* ptr, size_t count );
00316 
00317   /*
00318   Description:
00319     Grows the array of vectors that was allocated by
00320     GetVectorMemory or GrowVectorMemory.
00321   Parameters:
00322     ptr - [in] pointer returned by an earlier call to
00323                GetVectorMemory or GrowVectorMemory.
00324     count - [in] (>0) number of vectors in memory block.
00325               If count <= 0, then NULL is returned.
00326               If ptr was not allocated by this ON_Workspace
00327               class, then NULL is returned.
00328   Returns:
00329     A pointer to the vector array.
00330   Remarks.
00331     onrealloc() is used to grow the block of memory.
00332     Do NOT free the pointer returned by GrowMemory().
00333     ~ON_Workspace() will free the memory.  If you decide
00334     you want to keep the memory block, pass the pointer
00335     to KeepMemory before ~ON_Workspace is called.
00336   See Also:
00337     ON_Workspace::GetVectorMemory
00338     ON_Workspace::KeepMemory
00339   */
00340   ON_3dVector* GrowVectorMemory( ON_3dVector* ptr, size_t count );
00341 
00342   /*
00343   Description:
00344     Calling the KeepMemory() function with a pointer 
00345     returned from one of the Get...() or Grow...() calls 
00346     keeps the workspace destructor from freeing the memory.
00347     After calling KeepMemory(), you can no longer use
00348     Grow...() on the pointer.  The caller is responsible 
00349     for using onfree() to release the memory when it is no
00350     longer needed.
00351   Parameters:
00352     ptr - [in] pointer returned by a Get...() or Grow()
00353                call to this ON_Workspace.
00354   Returns:
00355     True if the pointer was successfully found and removed
00356     from this ON_Workspace.
00357   See Also:
00358     ON_Workspace::~ON_Workspace
00359     ON_Workspace::GetMemory
00360     ON_Workspace::KeepAllMemory
00361   */
00362   ON_BOOL32 KeepMemory( void* ptr );
00363 
00364   /*
00365   Description:
00366     Calling KeepAllMemory() has the same effect as calling
00367     KeepMemory(p) for every active allocation in the workspace.
00368     After calling KeepAllMemory(), you can no longer use
00369     Grow...() on the pointers and you are responsible 
00370     for using onfree() to release the memory when it is no
00371     longer needed.
00372   See Also:
00373     ON_Workspace::~ON_Workspace
00374     ON_Workspace::GetMemory
00375     ON_Workspace::KeepMemory
00376   */
00377   void KeepAllMemory();
00378 
00379   /*
00380   Description:
00381     Uses ON::OpenFile to open a file.  ~ON_Workspace will
00382     close the file.
00383   Parameters:
00384     filename - [in] name of file
00385     filemode - [in] open mode (just like second argument to fopen).
00386   Returns:
00387     Pointer to opened file.
00388   Remarks:
00389     ~ON_Workspace will close the file.
00390   See Also:
00391     ON_Workspace::~ON_Workspace
00392     ON_Workspace::KeepFile
00393     ON::OpenFile
00394   */
00395   FILE* OpenFile(
00396           const char* filename, 
00397           const char* filemode
00398           );
00399 
00400   /*
00401   Description:
00402     Uses ON::OpenFile to open a file.  ~ON_Workspace will
00403     close the file.
00404   Parameters:
00405     filename - [in] name of file
00406     filemode - [in] open mode (just like second argument to _wfopen).
00407   Returns:
00408     Pointer to opened file.
00409   Remarks:
00410     ~ON_Workspace will close the file.
00411   See Also:
00412     ON_Workspace::~ON_Workspace
00413     ON_Workspace::KeepFile
00414     ON::OpenFile
00415   */
00416   FILE* OpenFile(
00417           const wchar_t* filename, 
00418           const wchar_t* filemode
00419           );
00420 
00421   /*
00422   Description:
00423     If you want to prevent ~ON_Workspace from closing a file
00424     that was opened with ON_Workspace::OpenFile, then pass
00425     the returned FILE pointer to KeepFile.  After calling
00426     KeepFile, the caller is responsible for calling
00427     ON::CloseFile to close the file.
00428   Parameters:
00429     fileptr - [in] pointer returned by OpenFile.
00430   Returns:
00431     True if file was successfully closed.
00432   See Also:
00433     ON_Workspace::~ON_Workspace
00434     ON_Workspace::OpenFile
00435     ON::OpenFile
00436     ON::CloseFile
00437   */
00438   int KeepFile(FILE* fileptr);
00439 
00440 private:
00441   struct ON_Workspace_FBLK * m_pFileBlk;
00442   struct ON_Workspace_MBLK * m_pMemBlk;
00443 
00444 private:
00445   // There is no implementation of the following to prevent use.
00446   // ON_Workspaces should never be copied, or you will get
00447   // multiple attempts to free the same pointer.
00448   ON_Workspace( const ON_Workspace& );
00449   ON_Workspace& operator=( const ON_Workspace& );
00450 };
00451 
00452 
00453 #endif


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:27:03