PByteArray.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2018 Digital Barriers plc. All rights reserved.
3  * Contact: http://www.digitalbarriers.com/
4  *
5  * This file is part of the Papillon SDK.
6  *
7  * You can't use, modify or distribute any part of this file without
8  * the explicit written agreements of Digital Barriers.
9  */
10 
15 #pragma once
16 #include <PObject.h>
17 #include <type_traits>
18 #include <cassert>
19 
20 #ifdef _MSC_VER
21 # pragma warning(push)
22 # pragma warning(disable: 4996) // 4996 = "this function or variable may be unsafe"
23 #endif
24 #include <vector>
25 #ifdef _MSC_VER
26 # pragma warning(pop)
27 #endif
28 
29 PAPILLON_BEGIN_NAMESPACE
30 
40 class PAPILLON_API_CORE PByteArray : public PObject
41 {
42 public:
46  PByteArray();
47 
51  explicit PByteArray(papillon::int32 capacity);
52 
56  explicit PByteArray(const void* buffer, papillon::int32 numBytes);
57 
61  explicit PByteArray(const std::vector<papillon::uint8>& array);
62 
68  PByteArray(const PByteArray& other);
69 
73  virtual ~PByteArray();
74 
80  PByteArray& operator=(const PByteArray& other);
81 
87  PByteArray Clone() const;
88 
98  static PResult FromString(const PString& s, PByteArray& a);
99 
104  bool IsEmpty() const;
105 
109  papillon::int32 Size() const;
110 
119  papillon::int32 Capacity() const;
120 
129  void Clear();
130 
134  void Fill(papillon::uint8 value);
135 
163  PResult Resize(papillon::int32 n);
164 
183  PResult Reserve(papillon::int32 n);
184 
190  bool operator==(const PByteArray& other) const;
191 
197  bool operator!=(const PByteArray& other) const;
198 
208  PByteArray& Append(const PByteArray& other);
209 
217  void PushBack(papillon::uint8 value);
218 
226  void PushBackInt32(papillon::int32 value);
227 
235  void PushBackFloat(float value);
236 
244  void PushBackDouble(double value);
245 
253  papillon::uint8 PopBack();
254 
260  papillon::int32 Get(papillon::int32 index) const;
261 
268  PResult Set(papillon::int32 index, papillon::int32 value);
269 
274  PResult Insert(papillon::int32 index, const PByteArray& ba);
275 
287  PByteArray Zip(papillon::int32 compressionLevel = -1);
288 
293  PByteArray Unzip();
294 
299  PByteArray EncodeToBase64() const;
300 
305  PString ToStringBase64() const;
306 
314  static PByteArray DecodeFromBase64(const PByteArray& base64);
315 
320  papillon::uint16 CRC16() const;
321 
329  PString MD5() const;
330 
337  PString SHA256() const;
338 
343  PResult ToFile(const PString& filename) const;
344 
350  PResult FromFile(const PString& filename);
351 
352  //*********************************************************************
353  //
354  //
355  // U N S A F E M E T H O D S
356  //
357  //
358  //*********************************************************************
359 
367  template<typename T>
368  T* AsPtr(papillon::int32 offsetInBytes = 0)
369  {
370  assert(((size_t(offsetInBytes)%std::alignment_of<T>())==0));
371  return reinterpret_cast<T*>(rawPointer(offsetInBytes));
372  }
373 
381  template<typename T>
382  const T* AsConstPtr(papillon::int32 offsetInBytes = 0) const
383  {
384  assert(((size_t(offsetInBytes)%std::alignment_of<T>())==0));
385  return reinterpret_cast<T*>(rawPointer(offsetInBytes));
386  }
387 
398  const char* c_str() const;
399 
404  papillon::PResult CopyToRawBuffer(void* destinationBuffer, int32 sourceOffset, papillon::int32 numBytes);
405 
410  PResult CopyFromRawBuffer(const void* sourceBuffer, papillon::int32 destinationOffset, papillon::int32 numBytes);
411 
412 private:
413  void* rawPointer(int32 offsetInBytes) const;
414 };
415 
416 PAPILLON_END_NAMESPACE
The PString class provides an Unicode character strings.
Definition: PString.h:37
The PResult class represents the result returned by most of Papillon functions; this class is used to...
Definition: PResult.h:159
PResult FromString(const PString &s)
Builds a PObject from a string (destringify).
The PByteArray class represents a resizable array of bytes.
Definition: PByteArray.h:40
The PObject class is the root class of all Papillon classes; all PObject behaves like shared referenc...
Definition: PObject.h:27
T * AsPtr(papillon::int32 offsetInBytes=0)
UNSAFE: returns a raw pointer to read/write values stored in this array, starting at the specified of...
Definition: PByteArray.h:368
const T * AsConstPtr(papillon::int32 offsetInBytes=0) const
UNSAFE: returns a raw pointer to read values stored in this array, starting at the specified offset...
Definition: PByteArray.h:382
Class PObject: the root class of all Papillon classes.
PObject & operator=(const PObject &obj)
Performs a shared copy of other to this object.
Definition: PObject.h:45