1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
//
// Created by tung on 6/23/15.
//
#ifndef IQTREE_OBJECTSTREAM_H
#define IQTREE_OBJECTSTREAM_H
#include "TreeCollection.h"
/**
* This class is used to serialize object. It converts different object to byte stream
* and can also read in byte stream to reconstruct the object
*/
class ObjectStream {
public:
/**
* Constructor
*/
ObjectStream(const char* data, size_t length);
ObjectStream(TreeCollection& trees);
ObjectStream() {
objectData = NULL;
}
virtual ~ObjectStream() {
if (objectData != NULL)
delete [] objectData;
}
/**
* Convert a tree collection into the internal byte stream
* @param[IN] trees
*/
void initFromTreeCollection(TreeCollection &trees);
/**
* Reconstruct TreeCollection from a byte stream
*/
TreeCollection getTreeCollection();
public:
size_t getDataLength() const {
return objectDataSize;
}
public:
char *getObjectData() const {
return objectData;
}
private:
/**
* Byte stream representing the object
*/
char* objectData;
size_t objectDataSize;
/**
* Convert vector of strings to array of chars
* @param [IN] strings the vector strings
* @param [OUT] the char array
* @return size of the char array
*/
size_t serializeStrings(vector<string> &strings, char *&data);
/**
* Convert array of chars to vector of strings
* @param [IN] data byte stream representing vector<string>
* @param [IN] length size of data
* @param [OUT] strings the reconstructed vector<string>
*/
void deserializeStrings(char *data, size_t length, vector<string> &strings);
};
#endif // IQTREE_OBJECTSTREAM_H
|