Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #ifndef INCLUDED_RTL_BYTESEQ_H
20 : #define INCLUDED_RTL_BYTESEQ_H
21 :
22 : #include <sal/config.h>
23 :
24 : #include <rtl/alloc.h>
25 : #include <rtl/ustring.h>
26 : #include <sal/saldllapi.h>
27 : #include <sal/types.h>
28 :
29 : #ifdef __cplusplus
30 : extern "C"
31 : {
32 : #endif
33 :
34 : /** Assures that the reference count of the given byte sequence is one. Otherwise a new copy
35 : of the sequence is created with a reference count of one.
36 :
37 : @param ppSequence sequence
38 : */
39 : SAL_DLLPUBLIC void SAL_CALL rtl_byte_sequence_reference2One(
40 : sal_Sequence ** ppSequence )
41 : SAL_THROW_EXTERN_C();
42 :
43 : /** Reallocates length of byte sequence.
44 :
45 : @param ppSequence sequence
46 : @param nSize new size of sequence
47 : */
48 : SAL_DLLPUBLIC void SAL_CALL rtl_byte_sequence_realloc(
49 : sal_Sequence ** ppSequence, sal_Int32 nSize )
50 : SAL_THROW_EXTERN_C();
51 :
52 : /** Acquires the byte sequence
53 :
54 : @param pSequence sequence, that is to be acquired
55 : */
56 : SAL_DLLPUBLIC void SAL_CALL rtl_byte_sequence_acquire(
57 : sal_Sequence *pSequence )
58 : SAL_THROW_EXTERN_C();
59 :
60 : /** Releases the byte sequence. If the refcount drops to zero, the sequence is freed.
61 :
62 : @param pSequence sequence, that is to be released; invalid after call
63 : */
64 : SAL_DLLPUBLIC void SAL_CALL rtl_byte_sequence_release(
65 : sal_Sequence *pSequence )
66 : SAL_THROW_EXTERN_C();
67 :
68 : /** Constructs a bytes sequence with length nLength. All bytes are set to zero.
69 :
70 : @param ppSequence inout sequence; on entry *ppSequence may be null, otherwise it is released;
71 : after the call, *ppSequence contains the newly constructed sequence
72 : @param nLength length of new sequence
73 : */
74 : SAL_DLLPUBLIC void SAL_CALL rtl_byte_sequence_construct(
75 : sal_Sequence **ppSequence , sal_Int32 nLength )
76 : SAL_THROW_EXTERN_C();
77 :
78 : /** Constructs a bytes sequence with length nLength. The data is not initialized.
79 :
80 : @param ppSequence inout sequence; on entry *ppSequence may be null, otherwise it is released;
81 : after the call, *ppSequence contains the newly constructed sequence
82 : @param nLength length of new sequence
83 : */
84 : SAL_DLLPUBLIC void SAL_CALL rtl_byte_sequence_constructNoDefault(
85 : sal_Sequence **ppSequence , sal_Int32 nLength )
86 : SAL_THROW_EXTERN_C();
87 :
88 : /** Constructs a byte sequence with length nLength and copies nLength bytes from pData.
89 :
90 : @param ppSequence inout sequence; on entry *ppSequence may be null, otherwise it is released;
91 : after the call, *ppSequence contains the newly constructed sequence
92 : @param pData initial data
93 : @param nLength length of new sequence
94 : */
95 : SAL_DLLPUBLIC void SAL_CALL rtl_byte_sequence_constructFromArray(
96 : sal_Sequence **ppSequence, const sal_Int8 *pData , sal_Int32 nLength )
97 : SAL_THROW_EXTERN_C();
98 :
99 : /** Assigns the byte sequence pSequence to *ppSequence.
100 :
101 : @param ppSequence inout sequence; on entry *ppSequence may be null, otherwise it is released;
102 : after the call, *ppSequence references pSequence
103 : @param pSequence the source sequence
104 : */
105 : SAL_DLLPUBLIC void SAL_CALL rtl_byte_sequence_assign(
106 : sal_Sequence **ppSequence , sal_Sequence *pSequence )
107 : SAL_THROW_EXTERN_C();
108 :
109 : /** Compares two byte sequences.
110 :
111 : @return true, if the data within the sequences are identical; false otherwise
112 : */
113 : SAL_DLLPUBLIC sal_Bool SAL_CALL rtl_byte_sequence_equals(
114 : sal_Sequence *pSequence1 , sal_Sequence *pSequence2 )
115 : SAL_THROW_EXTERN_C();
116 :
117 : /** Returns the data array pointer of the sequence.
118 :
119 : @return read-pointer to the data array of the sequence. If rtl_byte_sequence_reference2One()
120 : has been called before, the pointer may be casted to a non const pointer and
121 : the sequence may be modified
122 : */
123 : SAL_DLLPUBLIC const sal_Int8 *SAL_CALL rtl_byte_sequence_getConstArray(
124 : sal_Sequence *pSequence )
125 : SAL_THROW_EXTERN_C();
126 :
127 : /** Returns the length of the sequence
128 :
129 : @param pSequence sequence handle
130 : @return length of the sequence
131 : */
132 : SAL_DLLPUBLIC sal_Int32 SAL_CALL rtl_byte_sequence_getLength(
133 : sal_Sequence *pSequence )
134 : SAL_THROW_EXTERN_C();
135 :
136 : #ifdef __cplusplus
137 : }
138 : namespace rtl
139 : {
140 :
141 : enum __ByteSequence_NoDefault
142 : {
143 : /** This enum value can be used to create a bytesequence with uninitalized data
144 : */
145 : BYTESEQ_NODEFAULT = 0xcafe
146 : };
147 :
148 : enum __ByteSequence_NoAcquire
149 : {
150 : /** This enum value can be used to create a bytesequence from a C-Handle without
151 : acquiring the handle.
152 : */
153 : BYTESEQ_NOACQUIRE = 0xcafebabe
154 : };
155 :
156 : /** C++ class representing a SAL byte sequence.
157 : C++ Sequences are reference counted and shared, so the sequence keeps a handle to its data.
158 : To keep value semantics, copies are only generated if the sequence is to be modified
159 : (new handle).
160 : */
161 : class SAL_WARN_UNUSED ByteSequence
162 : {
163 : /** sequence handle
164 : */
165 : sal_Sequence * _pSequence;
166 :
167 : public:
168 : /// @cond INTERNAL
169 : // these are here to force memory de/allocation to sal lib.
170 : inline static void * SAL_CALL operator new ( size_t nSize ) SAL_THROW(())
171 : { return ::rtl_allocateMemory( nSize ); }
172 : inline static void SAL_CALL operator delete ( void * pMem ) SAL_THROW(())
173 : { ::rtl_freeMemory( pMem ); }
174 0 : inline static void * SAL_CALL operator new ( size_t, void * pMem ) SAL_THROW(())
175 0 : { return pMem; }
176 : inline static void SAL_CALL operator delete ( void *, void * ) SAL_THROW(())
177 : {}
178 : /// @endcond
179 :
180 : /** Default constructor: Creates an empty sequence.
181 : */
182 : inline ByteSequence() SAL_THROW(());
183 : /** Copy constructor: Creates a copy of given sequence.
184 :
185 : @param rSeq another byte sequence
186 : */
187 : inline ByteSequence( const ByteSequence & rSeq ) SAL_THROW(());
188 : /** Copy constructor Creates a copy from the C-Handle.
189 :
190 : @param pSequence another byte sequence handle
191 : */
192 : inline ByteSequence( sal_Sequence *pSequence ) SAL_THROW(());
193 : /** Constructor: Creates a copy of given data bytes.
194 :
195 : @param pElements an array of bytes
196 : @param len number of bytes
197 : */
198 : inline ByteSequence( const sal_Int8 * pElements, sal_Int32 len );
199 : /** Constructor: Creates sequence of given length and initializes all bytes to 0.
200 :
201 : @param len initial sequence length
202 : */
203 : inline ByteSequence( sal_Int32 len );
204 : /** Constructor: Creates sequence of given length and does NOT initialize data.
205 : Use this ctor for performance optimization only.
206 :
207 : @param len initial sequence length
208 : @param nodefault dummy parameter forcing explicit BYTESEQ_NODEFAULT
209 : */
210 : inline ByteSequence( sal_Int32 len , enum __ByteSequence_NoDefault nodefault );
211 : /** Constructor:
212 : Creates a sequence from a C-Handle without acquiring the handle, thus taking
213 : over owenership. Eitherway the handle is release by the destructor.
214 : This ctor is useful, when working with a c-interface (it safes a pair of
215 : acquire and release call and is thus a performance optimization only).
216 :
217 : @param pSequence sequence handle to be taken over
218 : @param noacquire dummy parameter forcing explicit BYTESEQ_NOACQUIRE
219 : */
220 : inline ByteSequence( sal_Sequence *pSequence , enum __ByteSequence_NoAcquire noacquire ) SAL_THROW(());
221 : /** Destructor: Releases sequence handle. Last handle will free memory.
222 : */
223 : inline ~ByteSequence() SAL_THROW(());
224 :
225 : /** Assignment operator: Acquires given sequence handle and releases a previously set handle.
226 :
227 : @param rSeq another byte sequence
228 : @return this sequence
229 : */
230 : inline ByteSequence & SAL_CALL operator = ( const ByteSequence & rSeq ) SAL_THROW(());
231 :
232 : /** Gets the length of sequence.
233 :
234 : @return length of sequence
235 : */
236 3655532 : inline sal_Int32 SAL_CALL getLength() const SAL_THROW(())
237 3655532 : { return _pSequence->nElements; }
238 :
239 : /** Gets a pointer to byte array for READING. If the sequence has a length of 0, then the
240 : returned pointer is undefined.
241 :
242 : @return pointer to byte array
243 : */
244 3655531 : inline const sal_Int8 * SAL_CALL getConstArray() const SAL_THROW(())
245 3655531 : { return (const sal_Int8 *)_pSequence->elements; }
246 : /** Gets a pointer to elements array for READING AND WRITING. In general if the sequence
247 : has a handle acquired by other sequences (reference count > 1), then a new sequence is
248 : created copying all bytes to keep value semantics!
249 : If the sequence has a length of 0, then the returned pointer is undefined.
250 :
251 : @return pointer to elements array
252 : */
253 : inline sal_Int8 * SAL_CALL getArray();
254 :
255 : /** Non-const index operator:
256 : Obtains a reference to byte indexed at given position.
257 : In general if the sequence has a handle acquired by other
258 : sequences (reference count > 1), then a new sequence is created
259 : copying all bytes to keep value semantics!
260 :
261 : @attention
262 : The implementation does NOT check for array bounds!
263 :
264 : @param nIndex index
265 : @return non-const C++ reference to element at index nIndex
266 : */
267 : inline sal_Int8 & SAL_CALL operator [] ( sal_Int32 nIndex );
268 :
269 : /** Const index operator: Obtains a reference to byte indexed at given position.
270 : The implementation does NOT check for array bounds!
271 :
272 : @param nIndex index
273 : @return const C++ reference to byte at element of indenx nIndex
274 : */
275 0 : inline const sal_Int8 & SAL_CALL operator [] ( sal_Int32 nIndex ) const SAL_THROW(())
276 0 : { return getConstArray()[ nIndex ]; }
277 :
278 : /** Equality operator: Compares two sequences.
279 :
280 : @param rSeq another byte sequence (right side)
281 : @return true if both sequences are equal, false otherwise
282 : */
283 : inline bool SAL_CALL operator == ( const ByteSequence & rSeq ) const SAL_THROW(());
284 : /** Unequality operator: Compares two sequences.
285 :
286 : @param rSeq another byte sequence (right side)
287 : @return false if both sequences are equal, true otherwise
288 : */
289 : inline bool SAL_CALL operator != ( const ByteSequence & rSeq ) const SAL_THROW(());
290 :
291 : /** Reallocates sequence to new length. If the sequence has a handle acquired by other sequences
292 : (reference count > 1), then the remaining elements are copied to a new sequence handle to
293 : keep value semantics!
294 :
295 : @param nSize new size of sequence
296 : */
297 : inline void SAL_CALL realloc( sal_Int32 nSize );
298 :
299 : /** Returns the UNnacquired C handle of the sequence
300 :
301 : @return UNacquired handle of the sequence
302 : */
303 42504 : inline sal_Sequence * SAL_CALL getHandle() const SAL_THROW(())
304 42504 : { return _pSequence; }
305 : /** Returns the UNnacquired C handle of the sequence (for compatibility reasons)
306 :
307 : @return UNacquired handle of the sequence
308 : */
309 1 : inline sal_Sequence * SAL_CALL get() const SAL_THROW(())
310 1 : { return _pSequence; }
311 : };
312 :
313 : }
314 : #endif
315 : #endif
316 :
317 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|