xref: /trunk/main/sysui/source/win32/misc/AutoBuffer.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 //------------------------------------------------------------------------
29 // includes
30 //------------------------------------------------------------------------
31 
32 #include "AutoBuffer.hxx"
33 #include <osl/diagnose.h>
34 
35 #include <windows.h>
36 
37 //------------------------------------------------------------------------
38 // namespace directives
39 //------------------------------------------------------------------------
40 
41 using rtl::OUString;
42 
43 //------------------------------------------------------------------------
44 //
45 //------------------------------------------------------------------------
46 
47 CAutoUnicodeBuffer::CAutoUnicodeBuffer( size_t size, sal_Bool bLazyCreation ) :
48     m_buffSize( size ),
49     m_pBuff( NULL )
50 {
51     if ( !bLazyCreation )
52         init( );
53 }
54 
55 //------------------------------------------------------------------------
56 //
57 //------------------------------------------------------------------------
58 
59 CAutoUnicodeBuffer::~CAutoUnicodeBuffer( )
60 {
61     delete [] m_pBuff;
62 }
63 
64 //------------------------------------------------------------------------
65 //
66 //------------------------------------------------------------------------
67 
68 sal_Bool SAL_CALL CAutoUnicodeBuffer::resize( size_t new_size )
69 {
70     OSL_ASSERT( new_size >= 0 );
71 
72     if ( new_size != m_buffSize )
73     {
74         if ( new_size > m_buffSize )
75         {
76             delete [] m_pBuff;
77             m_pBuff = NULL;
78         }
79 
80         m_buffSize = new_size;
81     }
82 
83     return sal_True;
84 }
85 
86 //------------------------------------------------------------------------
87 //
88 //------------------------------------------------------------------------
89 
90 void SAL_CALL CAutoUnicodeBuffer::empty( )
91 {
92     if ( m_pBuff )
93         ZeroMemory( m_pBuff, m_buffSize * sizeof( sal_Unicode ) );
94 }
95 
96 //------------------------------------------------------------------------
97 //
98 //------------------------------------------------------------------------
99 
100 sal_Bool SAL_CALL CAutoUnicodeBuffer::fill( const sal_Unicode* pContent, size_t nLen )
101 {
102     OSL_ASSERT( pContent && m_buffSize && (m_buffSize >= nLen) );
103 
104     init( );
105 
106     sal_Bool bRet = sal_False;
107 
108     if ( m_pBuff && pContent && nLen )
109     {
110         CopyMemory( m_pBuff, pContent, nLen * sizeof( sal_Unicode ) );
111         bRet = sal_True;
112     }
113 
114     return bRet;
115 }
116 
117 //------------------------------------------------------------------------
118 //
119 //------------------------------------------------------------------------
120 
121 size_t SAL_CALL CAutoUnicodeBuffer::size( ) const
122 {
123     return m_buffSize;
124 }
125 
126 //------------------------------------------------------------------------
127 //
128 //------------------------------------------------------------------------
129 
130 CAutoUnicodeBuffer::operator sal_Unicode*( )
131 {
132     return m_pBuff;
133 }
134 
135 //------------------------------------------------------------------------
136 //
137 //------------------------------------------------------------------------
138 
139 sal_Unicode* CAutoUnicodeBuffer::operator&( )
140 {
141     return m_pBuff;
142 }
143 
144 //------------------------------------------------------------------------
145 //
146 //------------------------------------------------------------------------
147 
148 const sal_Unicode* CAutoUnicodeBuffer::operator&( ) const
149 {
150     return m_pBuff;
151 }
152 
153 //------------------------------------------------------------------------
154 //
155 //------------------------------------------------------------------------
156 
157 void SAL_CALL CAutoUnicodeBuffer::init( )
158 {
159     if ( !m_pBuff && (m_buffSize > 0) )
160         m_pBuff = new sal_Unicode[ m_buffSize ];
161 
162     empty( );
163 }
164