1 | /*! |
---|
2 | \file dht_data_types.hpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \since 17 Mars 2016 |
---|
5 | \date 17 Mars 2016 |
---|
6 | |
---|
7 | \brief Customized data type for dht |
---|
8 | */ |
---|
9 | |
---|
10 | #ifndef __XIOS_DHT_DATATYPES_HPP__ |
---|
11 | #define __XIOS_DHT_DATATYPES_HPP__ |
---|
12 | |
---|
13 | #include "utils.hpp" |
---|
14 | |
---|
15 | namespace xios |
---|
16 | { |
---|
17 | typedef std::pair<int,int> PairIntInt; |
---|
18 | //struct PairIntInt |
---|
19 | //{ |
---|
20 | //// PairIntInt(int f, int s) : first(f), second(s) {} |
---|
21 | //// PairIntInt(const PairIntInt& p) |
---|
22 | //// { |
---|
23 | //// this->first = p.first; |
---|
24 | //// this->second=p.second; |
---|
25 | //// } |
---|
26 | // |
---|
27 | // int first; |
---|
28 | // int second; |
---|
29 | //}; |
---|
30 | |
---|
31 | template<typename T> |
---|
32 | struct ProcessDHTElement |
---|
33 | { |
---|
34 | typedef T Type; |
---|
35 | static int typeSize() |
---|
36 | { |
---|
37 | return sizeof(Type); |
---|
38 | } |
---|
39 | |
---|
40 | static void packElement(const T& inputElement, unsigned char* packedElement, int& index) |
---|
41 | { |
---|
42 | if (NULL == packedElement) index += sizeof(Type); |
---|
43 | else |
---|
44 | { |
---|
45 | *(T *)(&packedElement[index]) = inputElement; |
---|
46 | index+=sizeof(Type); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | static void unpackElement(T& outputElement, unsigned char* unpackedElement, int& index) |
---|
51 | { |
---|
52 | outputElement = *(Type*)(&unpackedElement[index]); |
---|
53 | index+=sizeof(Type); |
---|
54 | } |
---|
55 | }; |
---|
56 | |
---|
57 | template<> |
---|
58 | struct ProcessDHTElement<PairIntInt> |
---|
59 | { |
---|
60 | typedef PairIntInt Type; |
---|
61 | static int typeSize() |
---|
62 | { |
---|
63 | return (2*sizeof(int)); |
---|
64 | } |
---|
65 | |
---|
66 | static void packElement(const PairIntInt& inputElement, unsigned char* packedElement, int& index) |
---|
67 | { |
---|
68 | if (NULL == packedElement) index +=(2*sizeof(int)); |
---|
69 | else |
---|
70 | { |
---|
71 | *(int *)(&packedElement[index]) = inputElement.first; |
---|
72 | index+=sizeof(int); |
---|
73 | *(int *)(&packedElement[index]) = inputElement.second; |
---|
74 | index+=sizeof(int); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | static void unpackElement(PairIntInt& outputElement, unsigned char* unpackedElement, int& index) |
---|
79 | { |
---|
80 | outputElement.first=*(int *)(&unpackedElement[index]); |
---|
81 | index+=sizeof(int); |
---|
82 | outputElement.second=*(int *)(&unpackedElement[index]); |
---|
83 | index+=sizeof(int); |
---|
84 | } |
---|
85 | }; |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | #endif // __XIOS_DHT_DATATYPES_HPP__ |
---|