Main MRPT website
>
C++ reference
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
mrpt
utils
CReferencedMemBlock.h
Go to the documentation of this file.
1
/* +---------------------------------------------------------------------------+
2
| The Mobile Robot Programming Toolkit (MRPT) C++ library |
3
| |
4
| http://www.mrpt.org/ |
5
| |
6
| Copyright (C) 2005-2012 University of Malaga |
7
| |
8
| This software was written by the Machine Perception and Intelligent |
9
| Robotics Lab, University of Malaga (Spain). |
10
| Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11
| |
12
| This file is part of the MRPT project. |
13
| |
14
| MRPT is free software: you can redistribute it and/or modify |
15
| it under the terms of the GNU General Public License as published by |
16
| the Free Software Foundation, either version 3 of the License, or |
17
| (at your option) any later version. |
18
| |
19
| MRPT is distributed in the hope that it will be useful, |
20
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
| GNU General Public License for more details. |
23
| |
24
| You should have received a copy of the GNU General Public License |
25
| along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26
| |
27
+---------------------------------------------------------------------------+ */
28
#ifndef CReferencedMemBlock_H
29
#define CReferencedMemBlock_H
30
31
#include <
mrpt/utils/utils_defs.h
>
32
#include <utility>
33
34
namespace
mrpt
35
{
36
namespace
utils
37
{
38
/** Represents a memory block (via "void*") that can be shared between several objects through copy operator (=).
39
* It keeps the reference count and only when it comes to zero, the memory block is really freed.
40
* \ingroup mrpt_base_grp
41
*/
42
class
BASE_IMPEXP
CReferencedMemBlock
:
public
stlplus::smart_ptr
< vector_byte >
43
{
44
public
:
45
/** Constructor with an optional size of the memory block */
46
CReferencedMemBlock
(
size_t
mem_block_size = 0 );
47
48
/** Destructor, calls dereference_once. */
49
virtual
~
CReferencedMemBlock
();
50
51
/** Resize the shared memory block. */
52
void
resize(
size_t
mem_block_size );
53
54
template
<
class
T> T
getAs
()
55
{
56
if
(!
stlplus::smart_ptr< vector_byte >::present
())
57
THROW_EXCEPTION
(
"Trying to access to an uninitialized memory block"
);
58
59
if
(
stlplus::smart_ptr< vector_byte >::operator
->()->
empty
() )
60
THROW_EXCEPTION
(
"Trying to access to a memory block of size 0"
);
61
62
return
reinterpret_cast<
T
>
( &
stlplus::smart_ptr< vector_byte >::operator ->
()->operator [](0) );
63
}
64
65
template
<
class
T> T
getAs
()
const
66
{
67
if
(!
stlplus::smart_ptr< vector_byte >::present
())
68
THROW_EXCEPTION
(
"Trying to access to an uninitialized memory block"
);
69
70
if
(
stlplus::smart_ptr< vector_byte >::operator
->()->
empty
() )
71
THROW_EXCEPTION
(
"Trying to access to a memory block of size 0"
);
72
73
return
reinterpret_cast<
const
T
>
( &
stlplus::smart_ptr< vector_byte >::operator ->
()->operator [](0) );
74
}
75
76
};
// End of class
77
78
}
// End of namespace
79
}
// End of namespace
80
81
#endif
Page generated by
Doxygen 1.8.3
for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013