1 #ifndef RTABMAP_SERIALIZATION_H_ 2 #define RTABMAP_SERIALIZATION_H_ 26 namespace serialization
31 template<
typename Archive,
typename T>
39 template<
typename Archive,
typename T>
48 template<
typename InputArchive>
53 template<
typename OutputArchive>
60 #define BASIC_TYPE_SERIALIZER(type)\ 62 struct Serializer<type> \ 64 template<typename InputArchive>\ 65 static inline void load(InputArchive& ar, type& val)\ 69 template<typename OutputArchive>\ 70 static inline void save(OutputArchive& ar, const type& val)\ 76 #define ENUM_SERIALIZER(type)\ 78 struct Serializer<type>\ 80 template<typename InputArchive>\ 81 static inline void load(InputArchive& ar, type& val)\ 85 val = (type) int_val;\ 87 template<typename OutputArchive>\ 88 static inline void save(OutputArchive& ar, const type& val)\ 90 int int_val = (int)val;\ 122 template<
typename InputArchive>
128 for (
size_t i=0;i<size;++i) {
133 template<
typename OutputArchive>
137 for (
size_t i=0;i<val.size();++i) {
144 template<
typename K,
typename V>
147 template<
typename InputArchive>
152 for (
size_t i = 0; i < size; ++i)
158 map_val[key] = value;
162 template<
typename OutputArchive>
166 for (
typename std::map<K,V>::const_iterator i=map_val.begin(); i!=map_val.end(); ++i) {
176 template<
typename InputArchive>
182 template<
typename OutputArchive>
189 template<
typename T,
int N>
192 template<
typename InputArchive>
198 template<
typename OutputArchive>
236 template<
typename InputArchive>
239 ar.load_binary(const_cast<void *>(b.
ptr_), b.
size_);
242 template<
typename OutputArchive>
252 template<
typename InputArchive>
255 ar.load_binary(const_cast<void *>(b.
ptr_), b.
size_);
258 template<
typename OutputArchive>
269 static const bool value = C_;
286 template<
typename Archive>
299 return *
static_cast<Archive*
>(
this);
304 template<
typename Archive>
317 return *
static_cast<Archive*
>(
this);
335 size_ +=
sizeof(val);
374 #define BLOCK_BYTES (1024 * 64) 398 buffer_ = buffer_blocks_ = (
char *)malloc(
BLOCK_BYTES*2);
400 if (buffer_ ==
NULL || compressed_buffer_ ==
NULL) {
405 lz4Stream = &lz4Stream_body;
426 lz4Stream, buffer_+headSz, compressed_buffer_+headSz, offset_-headSz,
435 memcpy(compressed_buffer_, buffer_, headSz);
438 first_block_ =
false;
440 size_t headSz =
sizeof(compSz);
444 lz4Stream, buffer_, compressed_buffer_+headSz, offset_,
452 memcpy(compressed_buffer_, &compSz, headSz);
457 fwrite(compressed_buffer_, compSz, 1, stream_);
460 if (buffer_ == buffer_blocks_)
463 buffer_ = buffer_blocks_;
470 free(buffer_blocks_);
471 buffer_blocks_ =
NULL;
473 free(compressed_buffer_);
474 compressed_buffer_ =
NULL;
478 fwrite(&z,
sizeof(z), 1, stream_);
484 stream_ = fopen(filename,
"wb");
513 memcpy(buffer_+offset_, &val,
sizeof(val));
514 offset_ +=
sizeof(val);
543 memcpy(buffer_+offset_, ptr, size);
573 size_t pos = ftell(stream);
574 fseek(stream, 0, SEEK_END);
575 size_t fileSize = ftell(stream)-pos;
576 fseek(stream, pos, SEEK_SET);
580 char *compBuffer = (
char *)malloc(fileSize);
581 if (compBuffer ==
NULL) {
584 if (fread(compBuffer, fileSize, 1, stream) != 1) {
586 throw FLANNException(
"Invalid index file, cannot read from disk (compressed)");
593 size_t compressedSz = fileSize-headSz;
603 ptr_ = buffer_ = (
char *)malloc(uncompressedSz+headSz);
604 if (buffer_ ==
NULL) {
606 throw FLANNException(
"Error (re)allocating decompression buffer");
616 if (usedSz != uncompressedSz) {
622 memcpy(buffer_, compBuffer, headSz);
626 if (compressedSz+headSz+pos != fileSize)
627 fseek(stream, compressedSz+headSz+pos, SEEK_SET);
628 block_sz_ = uncompressedSz+headSz;
633 size_t pos = ftell(stream);
635 buffer_blocks_ =
NULL;
636 compressed_buffer_ =
NULL;
644 if (fread(head, headSz, 1, stream) != 1) {
646 throw FLANNException(
"Invalid index file, cannot read from disk (header)");
652 fseek(stream, pos, SEEK_SET);
653 return decompressAndLoadV10(stream);
658 buffer_ = buffer_blocks_ = (
char *)malloc(
BLOCK_BYTES*2);
660 if (buffer_ ==
NULL || compressed_buffer_ ==
NULL) {
666 lz4StreamDecode = &lz4StreamDecode_body;
670 memcpy(buffer_, head, headSz);
677 void loadBlock(
char* buffer_,
size_t compSz, FILE* stream)
684 if (fread(compressed_buffer_, compSz, 1, stream) != 1) {
685 throw FLANNException(
"Invalid index file, cannot read from disk (block)");
690 lz4StreamDecode, compressed_buffer_, buffer_, compSz,
BLOCK_BYTES);
692 throw FLANNException(
"Invalid index file, cannot decompress block");
694 block_sz_ = decBytes;
700 if (ptr_+size <= buffer_+block_sz_)
704 if (buffer_ == buffer_blocks_)
707 buffer_ = buffer_blocks_;
711 size_t readCnt = fread(&cmpSz,
sizeof(cmpSz), 1, stream_);
712 if(cmpSz <= 0 || readCnt != 1) {
713 throw FLANNException(
"Requested to read next block past end of file");
717 loadBlock(buffer_, cmpSz, stream_);
724 if (buffer_blocks_ !=
NULL) {
727 if (fread(&zero,
sizeof(zero), 1, stream_) != 1) {
728 throw FLANNException(
"Invalid index file, cannot read from disk (end)");
731 throw FLANNException(
"Invalid index file, last block not zero length");
736 if (buffer_blocks_ !=
NULL) {
737 free(buffer_blocks_);
738 buffer_blocks_ =
NULL;
740 if (compressed_buffer_ !=
NULL) {
741 free(compressed_buffer_);
742 compressed_buffer_ =
NULL;
751 stream_ = fopen(filename,
"rb");
776 preparePtr(
sizeof(val));
777 memcpy(&val, ptr_,
sizeof(val));
806 memcpy(ptr, ptr_, size);
813 #endif // SERIALIZATION_H_
binary_object & operator=(const binary_object &rhs)
GLM_FUNC_DECL tvec1< T, P > operator&(tvec1< T, P > const &v, T const &s)
SaveArchive(FILE *stream)
static void load(InputArchive &ar, std::vector< T > &val)
static void load(InputArchive &ar, T &val)
static void save(OutputArchive &ar, T *const &val)
static void load(InputArchive &ar, T(&val)[N])
int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, const char *dictionary, int dictSize)
static void load(InputArchive &ar, binary_object &b)
void loadBlock(char *buffer_, size_t compSz, FILE *stream)
void LZ4_resetStreamHC(LZ4_streamHC_t *LZ4_streamHCPtr, int compressionLevel)
binary_object(const binary_object &rhs)
void setObject(void *object)
int LZ4_decompress_safe(const char *source, char *dest, int compressedSize, int maxDecompressedSize)
static void serialize(Archive &ar, T &type)
void decompressAndLoadV10(FILE *stream)
void initBlock(FILE *stream)
int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, int compressedSize, int maxOutputSize)
static void save(OutputArchive &ar, const std::vector< T > &val)
void save_binary(T *ptr, size_t size)
binary_object(void *const ptr, size_t size)
void serialize(Archive &ar, T &type)
static void save(OutputArchive &ar, const std::map< K, V > &map_val)
#define BASIC_TYPE_SERIALIZER(type)
const binary_object make_binary_object(void *t, size_t size)
static void load(InputArchive &ar, const binary_object &b)
LZ4_streamDecode_t lz4StreamDecode_body
SaveArchive(const char *filename)
void load_binary(T *ptr, size_t size)
static void save(OutputArchive &ar, T const (&val)[N])
char * compressed_buffer_
LoadArchive(FILE *stream)
LZ4_streamHC_t * lz4Stream
static void save(OutputArchive &ar, const binary_object &b)
static void load(InputArchive &ar, T *&val)
void save_binary(T *ptr, size_t size)
static void load(InputArchive &ar, std::map< K, V > &map_val)
char * compressed_buffer_
int LZ4_compress_HC_continue(LZ4_streamHC_t *LZ4_streamHCPtr, const char *source, char *dest, int inputSize, int maxOutputSize)
static void save(OutputArchive &ar, const binary_object &b)
static void save(OutputArchive &ar, const T &val)
GLM_FUNC_DECL genType zero()
void preparePtr(size_t size)
LoadArchive(const char *filename)
#define LZ4_COMPRESSBOUND(isize)
LZ4_streamHC_t lz4Stream_body
LZ4_streamDecode_t * lz4StreamDecode