source: XIOS/trunk/src/dht_data_types.hpp @ 830

Last change on this file since 830 was 830, checked in by mhnguyen, 5 years ago

Implementing pack and unpack function for generic type of dht

+) Replace union with the more generic function for packing data into buffer and unpacking data from buffer
+) Change PairIntInt? from struct to std::pair<int,int>

Test
+) On Curie
+) All tests pass

File size: 1.9 KB
Line 
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
15namespace 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
31template<typename T>
32struct 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
57template<>
58struct 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__
Note: See TracBrowser for help on using the repository browser.