Main Page · Modules · All Classes · Class Hierarchy
MCBinaryData.hpp
1 /*
2  * This file is part of the AiBO+ project
3  *
4  * Copyright (C) 2005-2016 Csaba Kertész (csaba.kertesz@gmail.com)
5  *
6  * AiBO+ is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * AiBO+ is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
19  *
20  */
21 
22 #pragma once
23 
24 #include <stdint.h>
25 
26 #include <string>
27 #include <vector>
28 
29 #include <boost/serialization/access.hpp>
30 #include <boost/shared_ptr.hpp>
31 
32 class QString;
33 
39 class MCBinaryData;
40 
41 namespace MC
42 {
44 typedef boost::shared_ptr<MCBinaryData> BinaryDataSPtr;
45 }
46 
50 class MCBinaryData // -V690 (PVS Studio suppression)
51 {
52  friend class boost::serialization::access;
53 
54 public:
56  MCBinaryData();
57 
65  explicit MCBinaryData(int size);
66 
75  MCBinaryData(int size, unsigned char value);
76 
84  MCBinaryData(const MCBinaryData& other);
85 
93  MCBinaryData(MCBinaryData&& other);
94 
95 
96  virtual ~MCBinaryData();
97 
105  void Allocate(int size);
106 
116  void Shrink(int size);
117 
123  void Free();
124 
132  void Clear();
133 
141  bool IsEmpty() const;
142 
153  void Set(unsigned char* data, int size);
154 
162  void Clone(const MCBinaryData& other);
163 
169  MCBinaryData* Clone();
170 
183  bool WriteData(MCBinaryData& target, int capacity = -1);
184 
198  unsigned int CopyData(MCBinaryData& target, int capacity = -1) const;
199 
209  bool LoadFromFile(const std::string& file_name);
210 
211 #ifndef __AIBO_BUILD__
212 
221  bool LoadFromQtResource(const QString& resource_str);
222 #endif
223 
233  bool SaveToFile(const std::string& file_name) const;
234 
242  MCBinaryData GenerateMd5Hash() const;
243 
251  void AddUChar(unsigned char new_char);
252 
261  void AddInt16(int16_t new_int, bool reverse_order = false);
262 
271  void AddInt32(int32_t new_int, bool reverse_order = false);
272 
280  void AddString(const std::string& str);
281 
289  unsigned char GetUChar();
290 
300  int16_t GetInt16(bool reverse_order = false);
301 
311  int32_t GetInt32(bool reverse_order = false);
312 
326  bool ValidateString(unsigned int length, const std::string& additional_chars = "");
327 
341  std::string GetString(unsigned int length, const std::string& additional_chars = "");
342 
350  unsigned char* GetData() const;
351 
359  int GetSize() const;
360 
368  const std::vector<unsigned char>& GetVectorData() const;
369 
380  int GetPosition() const;
381 
389  bool IsPositionAtEnd() const;
390 
403  unsigned int GetRemainingCapacity(unsigned int capacity) const;
404 
412  void SetPosition(unsigned int position);
413 
421  void IncrementPosition(unsigned int position = 1);
422 
428  void ResetPosition();
429 
437  std::string ToBase64() const;
438 
446  void FromBase64(const std::string& base64_str);
447 
455  MCBinaryData& operator=(MCBinaryData&& other);
456 
464  MCBinaryData& operator=(const MCBinaryData& other);
465 
475  bool operator==(const MCBinaryData& other) const;
476 
486  bool operator!=(const MCBinaryData& other) const;
487 
488 private:
489  template<class Archive>
490  void serialize(Archive& archive, const unsigned int version);
491 
492 protected:
494  std::vector<unsigned char> Data;
496  int Size;
498  int Position;
499 };
500 
std::vector< unsigned char > Data
Pointer to the encapsulated data.
Binary data class.
int Size
Size of the data.
int Position
Internal position index.