1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_RTL_BYTESEQ_H
20 #define INCLUDED_RTL_BYTESEQ_H
22 #include <sal/config.h>
24 #include <rtl/alloc.h>
25 #include <rtl/ustring.h>
26 #include <sal/saldllapi.h>
27 #include <sal/types.h>
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.
37 @param ppSequence sequence
39 SAL_DLLPUBLIC
void SAL_CALL
rtl_byte_sequence_reference2One(
40 sal_Sequence
** ppSequence
)
43 /** Reallocates length of byte sequence.
45 @param ppSequence sequence
46 @param nSize new size of sequence
48 SAL_DLLPUBLIC
void SAL_CALL
rtl_byte_sequence_realloc(
49 sal_Sequence
** ppSequence
, sal_Int32 nSize
)
52 /** Acquires the byte sequence
54 @param pSequence sequence, that is to be acquired
56 SAL_DLLPUBLIC
void SAL_CALL
rtl_byte_sequence_acquire(
57 sal_Sequence
*pSequence
)
60 /** Releases the byte sequence. If the refcount drops to zero, the sequence is freed.
62 @param pSequence sequence, that is to be released; invalid after call
64 SAL_DLLPUBLIC
void SAL_CALL
rtl_byte_sequence_release(
65 sal_Sequence
*pSequence
)
68 /** Constructs a bytes sequence with length nLength. All bytes are set to zero.
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
74 SAL_DLLPUBLIC
void SAL_CALL
rtl_byte_sequence_construct(
75 sal_Sequence
**ppSequence
, sal_Int32 nLength
)
78 /** Constructs a bytes sequence with length nLength. The data is not initialized.
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
84 SAL_DLLPUBLIC
void SAL_CALL
rtl_byte_sequence_constructNoDefault(
85 sal_Sequence
**ppSequence
, sal_Int32 nLength
)
88 /** Constructs a byte sequence with length nLength and copies nLength bytes from pData.
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
95 SAL_DLLPUBLIC
void SAL_CALL
rtl_byte_sequence_constructFromArray(
96 sal_Sequence
**ppSequence
, const sal_Int8
*pData
, sal_Int32 nLength
)
99 /** Assigns the byte sequence pSequence to *ppSequence.
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
105 SAL_DLLPUBLIC
void SAL_CALL
rtl_byte_sequence_assign(
106 sal_Sequence
**ppSequence
, sal_Sequence
*pSequence
)
107 SAL_THROW_EXTERN_C();
109 /** Compares two byte sequences.
111 @return true, if the data within the sequences are identical; false otherwise
113 SAL_DLLPUBLIC sal_Bool SAL_CALL
rtl_byte_sequence_equals(
114 sal_Sequence
*pSequence1
, sal_Sequence
*pSequence2
)
115 SAL_THROW_EXTERN_C();
117 /** Returns the data array pointer of the sequence.
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
123 SAL_DLLPUBLIC
const sal_Int8
*SAL_CALL
rtl_byte_sequence_getConstArray(
124 sal_Sequence
*pSequence
)
125 SAL_THROW_EXTERN_C();
127 /** Returns the length of the sequence
129 @param pSequence sequence handle
130 @return length of the sequence
132 SAL_DLLPUBLIC sal_Int32 SAL_CALL
rtl_byte_sequence_getLength(
133 sal_Sequence
*pSequence
)
134 SAL_THROW_EXTERN_C();
141 enum __ByteSequence_NoDefault
143 /** This enum value can be used to create a bytesequence with uninitalized data
145 BYTESEQ_NODEFAULT
= 0xcafe
148 enum __ByteSequence_NoAcquire
150 /** This enum value can be used to create a bytesequence from a C-Handle without
151 acquiring the handle.
160 /** C++ class representing a SAL byte sequence.
161 C++ Sequences are reference counted and shared, so the sequence keeps a handle to its data.
162 To keep value semantics, copies are only generated if the sequence is to be modified
165 class SAL_WARN_UNUSED ByteSequence
169 sal_Sequence
* _pSequence
;
173 // these are here to force memory de/allocation to sal lib.
174 inline static void * SAL_CALL
operator new ( size_t nSize
)
175 { return ::rtl_allocateMemory( nSize
); }
176 inline static void SAL_CALL
operator delete ( void * pMem
)
177 { ::rtl_freeMemory( pMem
); }
178 inline static void * SAL_CALL
operator new ( size_t, void * pMem
)
180 inline static void SAL_CALL
operator delete ( void *, void * )
184 /** Default constructor: Creates an empty sequence.
186 inline ByteSequence();
187 /** Copy constructor: Creates a copy of given sequence.
189 @param rSeq another byte sequence
191 inline ByteSequence( const ByteSequence
& rSeq
);
192 /** Copy constructor Creates a copy from the C-Handle.
194 @param pSequence another byte sequence handle
196 inline ByteSequence( sal_Sequence
*pSequence
);
197 /** Constructor: Creates a copy of given data bytes.
199 @param pElements an array of bytes
200 @param len number of bytes
202 inline ByteSequence( const sal_Int8
* pElements
, sal_Int32 len
);
203 /** Constructor: Creates sequence of given length and initializes all bytes to 0.
205 @param len initial sequence length
207 inline ByteSequence( sal_Int32 len
);
208 /** Constructor: Creates sequence of given length and does NOT initialize data.
209 Use this ctor for performance optimization only.
211 @param len initial sequence length
212 @param nodefault dummy parameter forcing explicit BYTESEQ_NODEFAULT
214 inline ByteSequence( sal_Int32 len
, enum __ByteSequence_NoDefault nodefault
);
216 Creates a sequence from a C-Handle without acquiring the handle, thus taking
217 over owenership. Eitherway the handle is release by the destructor.
218 This ctor is useful, when working with a c-interface (it safes a pair of
219 acquire and release call and is thus a performance optimization only).
221 @param pSequence sequence handle to be taken over
222 @param noacquire dummy parameter forcing explicit BYTESEQ_NOACQUIRE
224 inline ByteSequence( sal_Sequence
*pSequence
, enum __ByteSequence_NoAcquire noacquire
);
225 /** Destructor: Releases sequence handle. Last handle will free memory.
227 inline ~ByteSequence();
229 /** Assignment operator: Acquires given sequence handle and releases a previously set handle.
231 @param rSeq another byte sequence
232 @return this sequence
234 inline ByteSequence
& SAL_CALL
operator = ( const ByteSequence
& rSeq
);
236 /** Gets the length of sequence.
238 @return length of sequence
240 inline sal_Int32 SAL_CALL
getLength() const
241 { return _pSequence
->nElements
; }
243 /** Gets a pointer to byte array for READING. If the sequence has a length of 0, then the
244 returned pointer is undefined.
246 @return pointer to byte array
248 inline const sal_Int8
* SAL_CALL
getConstArray() const
249 { return reinterpret_cast<sal_Int8
*>(_pSequence
->elements
); }
250 /** Gets a pointer to elements array for READING AND WRITING. In general if the sequence
251 has a handle acquired by other sequences (reference count > 1), then a new sequence is
252 created copying all bytes to keep value semantics!
253 If the sequence has a length of 0, then the returned pointer is undefined.
255 @return pointer to elements array
257 inline sal_Int8
* SAL_CALL
getArray();
259 /** Non-const index operator:
260 Obtains a reference to byte indexed at given position.
261 In general if the sequence has a handle acquired by other
262 sequences (reference count > 1), then a new sequence is created
263 copying all bytes to keep value semantics!
266 The implementation does NOT check for array bounds!
269 @return non-const C++ reference to element at index nIndex
271 inline sal_Int8
& SAL_CALL
operator [] ( sal_Int32 nIndex
);
273 /** Const index operator: Obtains a reference to byte indexed at given position.
274 The implementation does NOT check for array bounds!
277 @return const C++ reference to byte at element of index nIndex
279 inline const sal_Int8
& SAL_CALL
operator [] ( sal_Int32 nIndex
) const
280 { return getConstArray()[ nIndex
]; }
282 /** Equality operator: Compares two sequences.
284 @param rSeq another byte sequence (right side)
285 @return true if both sequences are equal, false otherwise
287 inline bool SAL_CALL
operator == ( const ByteSequence
& rSeq
) const;
288 /** Unequality operator: Compares two sequences.
290 @param rSeq another byte sequence (right side)
291 @return false if both sequences are equal, true otherwise
293 inline bool SAL_CALL
operator != ( const ByteSequence
& rSeq
) const;
295 /** Reallocates sequence to new length. If the sequence has a handle acquired by other sequences
296 (reference count > 1), then the remaining elements are copied to a new sequence handle to
297 keep value semantics!
299 @param nSize new size of sequence
301 inline void SAL_CALL
realloc( sal_Int32 nSize
);
303 /** Returns the UNnacquired C handle of the sequence
305 @return UNacquired handle of the sequence
307 inline sal_Sequence
* SAL_CALL
getHandle() const
308 { return _pSequence
; }
309 /** Returns the UNnacquired C handle of the sequence (for compatibility reasons)
311 @return UNacquired handle of the sequence
313 inline sal_Sequence
* SAL_CALL
get() const
314 { return _pSequence
; }
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */