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