51ab75d70df9ae9e5ff93ef6b5060715fbac0412
[occt.git] / src / MeshVS / MeshVS_Buffer.hxx
1 // Created on: 2007-03-07
2 // Created by: msv@EUCLIDEX
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef MeshVS_Buffer_HeaderFile
17 #define MeshVS_Buffer_HeaderFile
18
19 #include <Standard.hxx>
20
21 /**
22  * General purpose buffer that is allocated on the stack with a
23  * constant size MeshVS_BufSize, or is allocated dynamically if the requested
24  * size exceeds the standard one.
25  * It is useful when an allocation of an array of unknown size is needed,
26  * and most often the array is small enough to allocate as automatic C array.
27  */
28
29 //! define the constant to the size of 10 points
30 #define MeshVS_BufSize 10*3*sizeof(double)
31
32 class MeshVS_Buffer 
33 {
34 public:
35   //! Constructor of the buffer of the requested size
36   MeshVS_Buffer (const Standard_Size theSize)
37     : myDynData (0)
38   {
39     if (theSize > MeshVS_BufSize)
40       myDynData = Standard::Allocate (theSize);
41   }
42
43   //! Destructor
44   ~MeshVS_Buffer()
45   {
46     if (myDynData)
47     {
48       Standard::Free (myDynData);
49       myDynData = 0;
50     }
51   }
52
53   //! Cast the buffer to the void pointer
54   operator void* ()
55   {
56     return myDynData ? myDynData : (void*) myAutoData;
57   }
58
59   //! Interpret the buffer as a reference to double
60   operator Standard_Real& ()
61   {
62     return * (myDynData ? (Standard_Real*) myDynData : (Standard_Real*) myAutoData);
63   }
64
65   //! Interpret the buffer as a reference to int
66   operator Standard_Integer& ()
67   {
68     return * (myDynData ? (Standard_Integer*) myDynData : (Standard_Integer*) myAutoData);
69   }
70
71 private:
72   //! Deprecate copy constructor
73   MeshVS_Buffer(const MeshVS_Buffer&) {}
74
75   //! Deprecate copy operation
76   MeshVS_Buffer& operator=(const MeshVS_Buffer&) {return *this;}
77
78   char  myAutoData[ MeshVS_BufSize ];
79   void* myDynData;
80 };
81
82 #endif