/* AUTOMATICALY GENERATED by org.flib.net.CPickle.java */
#ifndef FCPICKLE_IMPL_H
#define FCPICKLE_IMPL_H

#include <netinet/in.h>
#include <fcpickle.h>
#include <cpicklehead.h>
#include <cpickleclassdef.h>

static inline int16_t n2hs(int16_t n) {return (int16_t)ntohs(n);}
static inline int16_t h2ns(int16_t n) {return (int16_t)htons(n);}
static inline int32_t n2hl(int32_t n) {return (int32_t)ntohl(n);}
static inline int32_t h2nl(int32_t n) {return (int32_t)htonl(n);}
static inline int64_t n2hll(int64_t n) {return (((int64_t)n2hl(n)) << 32) + n2hl(n >> 32);}
static inline int64_t h2nll(int64_t n) {return (((int64_t)h2nl(n)) << 32) + h2nl(n >> 32);}
static inline double n2hdbl(double d) {*(int64_t*)(void*)&d = n2hll(*(int64_t*)(void*)&d); return *(double*)(void*)&d;}
static inline double h2ndbl(double d) {*(int64_t*)(void*)&d = h2nll(*(int64_t*)(void*)&d); return *(double*)(void*)&d;}

typedef CPickleHead_t fcpickle_packet_head_t;
typedef CPickleClassDef_t fcpickle_class_def_t;

/// get packet head form the buffer
/// @return number of used bytes or negative value if error
static inline int fcpickle_getHead(fcpickle_packet_head_t *head, uint8_t *buffer, int buff_len) {
    return CPickleHead_unpickleObject(head, buffer, buff_len);
}

/// get pickled class definition form the buffer
/// @return number of used bytes or negative value if error
static inline int fcpickle_getClassDef(fcpickle_class_def_t *def, uint8_t *buffer, int buff_len) {
    return CPickleClassDef_unpickleObject(def, buffer, buff_len);
}

// use this function if you want to know pickled class name in incomming data
static inline const char* fcpickle_getClassName(uint8_t *buffer, int buff_len) {
    if(buff_len <= FCPICKLE_PACKET_HEAD_SIZE) return "";
    return (const char*)(buffer + FCPICKLE_PACKET_HEAD_SIZE + 2);
}

#endif
