QCAD
Open Source 2D CAD
Loading...
Searching...
No Matches
opennurbs_workspace.h
Go to the documentation of this file.
1/* $NoKeywords: $ */
2/*
3//
4// Copyright (c) 1993-2007 Robert McNeel & Associates. All rights reserved.
5// Rhinoceros is a registered trademark of Robert McNeel & Assoicates.
6//
7// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
8// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
9// MERCHANTABILITY ARE HEREBY DISCLAIMED.
10//
11// For complete openNURBS copyright information see <http://www.opennurbs.org>.
12//
14*/
15
16#if !defined(OPENNURBS_WORKSPACE_INC_)
17#define OPENNURBS_WORKSPACE_INC_
18
19/*
20Description:
21 Use ON_Workspace classes on the stack to efficiently get
22 and automatically clean up workspace memory and scratch
23 files.
24*/
26{
27public:
28 /*
29 Description:
30 ON_Workspace classes should be on the stack
31 or as members on classes that are never copied.
32 The destructor frees memory that was allocated by
33 ON_Workspace::GetMemory and closes files that were
34 opened with ON_Workspace::OpenFile.
35 */
37
38 /*
39 Description:
40 The destructor frees memory that was allocated by
41 ON_Workspace::GetMemory and closes files that were
42 opened with ON_Workspace::OpenFile.
43 */
45
46 /*
47 Description:
48 Gets a block of heap memory that will be freed by
49 ~ON_Workspace. The intent of ON_Workspace::GetMemory
50 is to provide an easy way to get blocks of scratch
51 memory without having to worry about cleaning up
52 before returning.
53 Parameters:
54 sz - [in] (>0) size of memory block in bytes.
55 If sz <= 0, then NULL is returned.
56 Returns:
57 A pointer to the memory block.
58 Remarks.
59 onmalloc() is used to get the block of memory.
60 Do NOT free the pointer returned by GetMemory().
61 ~ON_Workspace() will free the memory. If you decide
62 you want to keep the memory block, pass the pointer
63 to KeepMemory before ~ON_Workspace is called.
64 See Also:
65 ON_Workspace::::~ON_Workspace
66 ON_Workspace::KeepMemory
67 ON_Workspace::GrowMemory
68 ON_Workspace::GetIntMemory
69 ON_Workspace::GetDoubleMemory
70 ON_Workspace::GetPointMemory
71 ON_Workspace::GetVectorMemory
72 */
73 void* GetMemory( size_t sz );
74
75 /*
76 Description:
77 Gets an array of integers that will be freed by ~ON_Workspace.
78 The intent of ON_Workspace::GetIntMemory is to provide
79 an easy way to get scratch integer arrays without
80 having to worry about cleaning up before returning.
81 Parameters:
82 count - [in] (>0) number of integers in memory block.
83 If count <= 0, then NULL is returned.
84 Returns:
85 A pointer to the array of integers.
86 Remarks.
87 This is a simple helper function so you don't have to
88 mess around with (int*) casts and sizeof(int)s in a call
89 to GetMemory(). It is exactly like calling
90 (int*)GetMemory(count*sizeof(int));
91 See Also:
92 ON_Workspace::GetMemory
93 ON_Workspace::KeepMemory
94 ON_Workspace::GrowIntMemory
95 */
96 int* GetIntMemory( size_t count );
97
98 /*
99 Description:
100 Gets an matrix of integers
101 Parameters:
102 row_count - [in] (>0) number of rows
103 col_count - [in] (>0) number of columns
104 Returns:
105 A pointer p so that p[i][j] is an integer when
106 0 <= i < row_count and 0 <= j < col_count.
107 Remarks.
108 This is a simple helper function so you don't have to
109 mess around building the 2d array.
110 See Also:
111 ON_Workspace::KeepMemory
112 */
113 int** GetIntMemory( size_t row_count, size_t col_count );
114
115 /*
116 Description:
117 Gets an array of doubles that will be freed by ~ON_Workspace.
118 The intent of ON_Workspace::GetDoubleMemory is to provide
119 an easy way to get scratch double arrays without
120 having to worry about cleaning up before returning.
121 Parameters:
122 count - [in] (>0) number of doubles in memory block.
123 If count <= 0, then NULL is returned.
124 Returns:
125 A pointer to the array of doubles.
126 Remarks.
127 This is a simple helper function so you don't have to
128 mess around with (double*) casts and sizeof(double)s
129 in a call to GetMemory(). It is exactly like calling
130 (double*)GetMemory(count*sizeof(double));
131 See Also:
132 ON_Workspace::GetMemory
133 ON_Workspace::KeepMemory
134 ON_Workspace::GrowIntMemory
135 */
136 double* GetDoubleMemory( size_t count );
137
138 /*
139 Description:
140 Gets an matrix of doubles
141 Parameters:
142 row_count - [in] (>0) number of rows
143 col_count - [in] (>0) number of columns
144 Returns:
145 A pointer p so that p[i][j] is an double when
146 0 <= i < row_count and 0 <= j < col_count.
147 Remarks.
148 This is a simple helper function so you don't have to
149 mess around building the 2d array.
150 See Also:
151 ON_Workspace::KeepMemory
152 */
153 double** GetDoubleMemory( size_t row_count, size_t col_count );
154
155 /*
156 Description:
157 Gets an array of ON_3dPoints that will be freed by ~ON_Workspace.
158 The intent of ON_Workspace::GetPointMemory is to
159 provide an easy way to get scratch point arrays without
160 having to worry about cleaning up before returning.
161 Parameters:
162 count - [in] (>0) number of points in memory block.
163 If count <= 0, then NULL is returned.
164 Returns:
165 A pointer to the memory block.
166 Remarks.
167 This is a simple helper function so you don't have to
168 mess around with (ON_3dPoint*) casts and sizeof(ON_3dPoint)s
169 in a call to GetMemory(). It is exactly like calling
170 (ON_3dPoint*)GetMemory(count*sizeof(ON_3dPoint));
171 See Also:
172 ON_Workspace::GetMemory
173 ON_Workspace::KeepMemory
174 ON_Workspace::GrowIntMemory
175 */
176 ON_3dPoint* GetPointMemory( size_t count );
177
178 /*
179 Description:
180 Gets an array of ON_3dVectors that will be freed by ~ON_Workspace.
181 The intent of ON_Workspace::GetVectorMemory is to
182 provide an easy way to get scratch Vector arrays without
183 having to worry about cleaning up before returning.
184 Parameters:
185 count - [in] (>0) number of Vectors in memory block.
186 If count <= 0, then NULL is returned.
187 Returns:
188 A pointer to the memory block.
189 Remarks.
190 This is a simple helper function so you don't have to
191 mess around with (ON_3dVector*) casts and sizeof(ON_3dVector)s
192 in a call to GetMemory(). It is exactly like calling
193 (ON_3dVector*)GetMemory(count*sizeof(ON_3dVector));
194 See Also:
195 ON_Workspace::GetMemory
196 ON_Workspace::KeepMemory
197 ON_Workspace::GrowIntMemory
198 */
199 ON_3dVector* GetVectorMemory( size_t count );
200
201 /*
202 Description:
203 Grows a block of heap memory that was allocated by
204 ON_Workspace::GetMemory.
205 Parameters:
206 ptr - [in] pointer returned by an earlier call to
207 GetMemory or GrowMemory.
208 sz - [in] (>0) size of memory block in bytes.
209 If sz <= 0, then NULL is returned.
210 If ptr is not NULL and was not allocated by an
211 earlier call to GetMemory or GrowMemory, then
212 NULL is returned.
213 Returns:
214 A pointer to the memory block.
215 Remarks.
216 onrealloc() is used to grow the block of memory.
217 Do NOT free the pointer returned by GrowMemory().
218 ~ON_Workspace() will free the memory. If you decide
219 you want to keep the memory block, pass the pointer
220 to KeepMemory before ~ON_Workspace is called.
221 See Also:
222 ON_Workspace::GetMemory
223 ON_Workspace::KeepMemory
224 ON_Workspace::GrowIntMemory
225 ON_Workspace::GrowDoubleMemory
226 ON_Workspace::GrowPointMemory
227 ON_Workspace::GrowVectorMemory
228 */
229 void* GrowMemory( void* ptr, size_t sz );
230
231 /*
232 Description:
233 Grows the array of integers that was allocated by
234 GetIntMemory or GrowIntMemory.
235 Parameters:
236 ptr - [in] pointer returned by an earlier call to
237 GetIntMemory or GrowIntMemory.
238 count - [in] (>0) number of integers in memory block.
239 If count <= 0, then NULL is returned.
240 If ptr was not allocated by this ON_Workspace
241 class, then NULL is returned.
242 Returns:
243 A pointer to the integer array.
244 Remarks.
245 onrealloc() is used to grow the block of memory.
246 Do NOT free the pointer returned by GrowIntMemory().
247 ~ON_Workspace() will free the memory. If you decide
248 you want to keep the memory block, pass the pointer
249 to KeepMemory before ~ON_Workspace is called.
250 See Also:
251 ON_Workspace::GetIntMemory
252 ON_Workspace::KeepMemory
253 */
254 int* GrowIntMemory( int* ptr, size_t count );
255
256 /*
257 Description:
258 Grows the array of doubles that was allocated by
259 GetDoubleMemory or GrowDoubleMemory.
260 Parameters:
261 ptr - [in] pointer returned by an earlier call to
262 GetDoubleMemory or GrowDoubleMemory.
263 count - [in] (>0) number of doubles in memory block.
264 If count <= 0, then NULL is returned.
265 If ptr was not allocated by this ON_Workspace
266 class, then NULL is returned.
267 Returns:
268 A pointer to the double array.
269 Remarks.
270 onrealloc() is used to grow the block of memory.
271 Do NOT free the pointer returned by GrowDoubleMemory().
272 ~ON_Workspace() will free the memory. If you decide
273 you want to keep the memory block, pass the pointer
274 to KeepMemory before ~ON_Workspace is called.
275 See Also:
276 ON_Workspace::GetDoubleMemory
277 ON_Workspace::KeepMemory
278 */
279 double* GrowDoubleMemory( double* ptr, size_t count );
280
281 /*
282 Description:
283 Grows the array of points that was allocated by
284 GetPointMemory or GrowPointMemory.
285 Parameters:
286 ptr - [in] pointer returned by an earlier call to
287 GetPointMemory or GrowPointMemory.
288 count - [in] (>0) number of points in memory block.
289 If count <= 0, then NULL is returned.
290 If ptr was not allocated by this ON_Workspace
291 class, then NULL is returned.
292 Returns:
293 A pointer to the point array.
294 Remarks.
295 onrealloc() is used to grow the block of memory.
296 Do NOT free the pointer returned by GrowMemory().
297 ~ON_Workspace() will free the memory. If you decide
298 you want to keep the memory block, pass the pointer
299 to KeepMemory before ~ON_Workspace is called.
300 See Also:
301 ON_Workspace::GetPointMemory
302 ON_Workspace::KeepMemory
303 */
304 ON_3dPoint* GrowPointMemory( ON_3dPoint* ptr, size_t count );
305
306 /*
307 Description:
308 Grows the array of vectors that was allocated by
309 GetVectorMemory or GrowVectorMemory.
310 Parameters:
311 ptr - [in] pointer returned by an earlier call to
312 GetVectorMemory or GrowVectorMemory.
313 count - [in] (>0) number of vectors in memory block.
314 If count <= 0, then NULL is returned.
315 If ptr was not allocated by this ON_Workspace
316 class, then NULL is returned.
317 Returns:
318 A pointer to the vector array.
319 Remarks.
320 onrealloc() is used to grow the block of memory.
321 Do NOT free the pointer returned by GrowMemory().
322 ~ON_Workspace() will free the memory. If you decide
323 you want to keep the memory block, pass the pointer
324 to KeepMemory before ~ON_Workspace is called.
325 See Also:
326 ON_Workspace::GetVectorMemory
327 ON_Workspace::KeepMemory
328 */
329 ON_3dVector* GrowVectorMemory( ON_3dVector* ptr, size_t count );
330
331 /*
332 Description:
333 Calling the KeepMemory() function with a pointer
334 returned from one of the Get...() or Grow...() calls
335 keeps the workspace destructor from freeing the memory.
336 After calling KeepMemory(), you can no longer use
337 Grow...() on the pointer. The caller is responsible
338 for using onfree() to release the memory when it is no
339 longer needed.
340 Parameters:
341 ptr - [in] pointer returned by a Get...() or Grow()
342 call to this ON_Workspace.
343 Returns:
344 True if the pointer was successfully freed.
345 See Also:
346 ON_Workspace::~ON_Workspace
347 ON_Workspace::GetMemory
348 ON_Workspace::GrowMemory
349 */
350 ON_BOOL32 KeepMemory( void* ptr );
351
352 /*
353 Description:
354 Uses ON::OpenFile to open a file. ~ON_Workspace will
355 close the file.
356 Parameters:
357 filename - [in] name of file
358 filemode - [in] open mode (just like second argument to fopen).
359 Returns:
360 Pointer to opened file.
361 Remarks:
362 ~ON_Workspace will close the file.
363 See Also:
364 ON_Workspace::~ON_Workspace
365 ON_Workspace::KeepFile
366 ON::OpenFile
367 */
368 FILE* OpenFile(
369 const char* filename,
370 const char* filemode
371 );
372
373 /*
374 Description:
375 Uses ON::OpenFile to open a file. ~ON_Workspace will
376 close the file.
377 Parameters:
378 filename - [in] name of file
379 filemode - [in] open mode (just like second argument to _wfopen).
380 Returns:
381 Pointer to opened file.
382 Remarks:
383 ~ON_Workspace will close the file.
384 See Also:
385 ON_Workspace::~ON_Workspace
386 ON_Workspace::KeepFile
387 ON::OpenFile
388 */
389 FILE* OpenFile(
390 const wchar_t* filename,
391 const wchar_t* filemode
392 );
393
394 /*
395 Description:
396 If you want to prevent ~ON_Workspace from closing a file
397 that was opened with ON_Workspace::OpenFile, then pass
398 the returned FILE pointer to KeepFile. After calling
399 KeepFile, the caller is responsible for calling
400 ON::CloseFile to close the file.
401 Parameters:
402 fileptr - [in] pointer returned by OpenFile.
403 Returns:
404 True if file was successfully closed.
405 See Also:
406 ON_Workspace::~ON_Workspace
407 ON_Workspace::OpenFile
408 ON::OpenFile
409 ON::CloseFile
410 */
411 int KeepFile(FILE* fileptr);
412
413private:
414 struct FBLK
415 {
416 struct FBLK* pNext;
417 FILE* pFile;
418 } * m_pFileBlk;
419
420 struct MBLK
421 {
422 struct MBLK* pNext;
423 void* pMem;
424 } * m_pMemBlk;
425
426private:
427 // There is no implementation of the following to prevent use.
428 // ON_Workspaces should never be copied, or you will get
429 // multiple attempts to free the same pointer.
432};
433
434
435#endif
Definition opennurbs_point.h:403
Definition opennurbs_point.h:931
Definition opennurbs_workspace.h:26
ON_Workspace & operator=(const ON_Workspace &)
ON_Workspace(const ON_Workspace &)
Handles all user interaction to open documents.
Definition OpenFile.js:11
#define ON_CLASS
Definition opennurbs_defines.h:91
int ON_BOOL32
Definition opennurbs_system.h:362
Definition opennurbs_workspace.h:415
struct FBLK * pNext
Definition opennurbs_workspace.h:416
FILE * pFile
Definition opennurbs_workspace.h:417
Definition opennurbs_workspace.h:421
void * pMem
Definition opennurbs_workspace.h:423
struct MBLK * pNext
Definition opennurbs_workspace.h:422