1 | /* ************************************************************************** * |
---|
2 | * Copyright © IPSL/LSCE, XMLIOServer, Avril 2010 - Octobre 2011 * |
---|
3 | * ************************************************************************** */ |
---|
4 | #ifndef __BUFFER_IMPL_HPP__ |
---|
5 | #define __BUFFER_IMPL_HPP__ |
---|
6 | |
---|
7 | // /////////////////////////////// Définitions ////////////////////////////// // |
---|
8 | |
---|
9 | #define DATA_HEADER_SIZE sizeof(CBufferDataType) + 2 * sizeof(std::size_t) + sizeof(bool) |
---|
10 | |
---|
11 | namespace xmlioserver { |
---|
12 | namespace comm { |
---|
13 | |
---|
14 | // ------------------------------- Accesseurs ------------------------------- |
---|
15 | |
---|
16 | template <class T> |
---|
17 | std::size_t CBuffer::getRequestedSize(T _data) |
---|
18 | { |
---|
19 | return (DATA_HEADER_SIZE + sizeof (T)); |
---|
20 | } |
---|
21 | |
---|
22 | template <class T> |
---|
23 | std::size_t CBuffer::getRequestedSize(boost::multi_array<T, 1> & _data) |
---|
24 | { |
---|
25 | return (DATA_HEADER_SIZE + _data.num_elements() * sizeof (T)); |
---|
26 | } |
---|
27 | |
---|
28 | template <> |
---|
29 | std::size_t CBuffer::getRequestedSize(std::string _data); |
---|
30 | |
---|
31 | template <class T> |
---|
32 | void CBuffer::getData(T & data, std::size_t position) const |
---|
33 | { |
---|
34 | if (this->size < (position + this->getRequestedSize(data))) |
---|
35 | XIOS_ERROR("CBuffer::getData(data, position)", |
---|
36 | << " Buffer size < size + position !"); |
---|
37 | CBufferData bufdata; |
---|
38 | this->getBufferData(bufdata, position); |
---|
39 | if (bufdata.type != this->getBufferDataType<T>()) |
---|
40 | XIOS_ERROR("CBuffer::getData(data, position)", |
---|
41 | << "[ BufferDataType Read : " << bufdata.type << ", " |
---|
42 | << ", BufferDataType T : " << this->getBufferDataType<T>() << "] " |
---|
43 | << " invalid type !"); |
---|
44 | if (bufdata.isArray != false) |
---|
45 | XIOS_ERROR("CBuffer::getData(data, position)", |
---|
46 | << " type should be an array !"); |
---|
47 | |
---|
48 | this->getData(reinterpret_cast<char*>(&data), bufdata.size , bufdata.pos); |
---|
49 | } |
---|
50 | |
---|
51 | template <class T> |
---|
52 | void CBuffer::getDataArray(boost::multi_array<T, 1> & _data, std::size_t _position) const |
---|
53 | { |
---|
54 | CBufferData bufdata; |
---|
55 | this->getBufferData(bufdata, _position); |
---|
56 | if (bufdata.type != this->getBufferDataType<T>()) |
---|
57 | XIOS_ERROR("CBuffer::getDataArray(data, position)", << " invalid type !"); |
---|
58 | if (bufdata.isArray != true) |
---|
59 | XIOS_ERROR("CBuffer::getDataArray(data, position)", |
---|
60 | << " type should not be an array !"); |
---|
61 | if (this->size < (_position + (DATA_HEADER_SIZE + bufdata.size))) |
---|
62 | XIOS_ERROR("CBuffer::getData<T>(data, size, position)", |
---|
63 | << " Buffer size < size + position !"); |
---|
64 | _data.resize(boost::extents[bufdata.size/sizeof(T)]); |
---|
65 | this->getData(reinterpret_cast<char*>(_data.data()), bufdata.size , bufdata.pos); |
---|
66 | } |
---|
67 | |
---|
68 | // ------------------------------- Mutateurs -------------------------------- |
---|
69 | |
---|
70 | template <class T> |
---|
71 | CBuffer::CBufferData CBuffer::setData(const T & data, std::size_t position) |
---|
72 | { |
---|
73 | if (this->size < (position + this->getRequestedSize(data))) |
---|
74 | XIOS_ERROR("CBuffer::setData<T>(data, size, position)", |
---|
75 | << " Buffer size < size + position !"); |
---|
76 | CBufferData bufdata; |
---|
77 | bufdata.type = this->getBufferDataType<T>(); |
---|
78 | bufdata.isArray = false; |
---|
79 | bufdata.size = sizeof(T); |
---|
80 | bufdata.pos = position + DATA_HEADER_SIZE; |
---|
81 | this->setBufferData(bufdata, position); |
---|
82 | this->setData(reinterpret_cast<const char*>(&data), bufdata.size , bufdata.pos); |
---|
83 | return (bufdata); |
---|
84 | } |
---|
85 | |
---|
86 | template <class T> |
---|
87 | CBuffer::CBufferData CBuffer::setDataArray |
---|
88 | (const boost::multi_array<T, 1> & _data, std::size_t _position) |
---|
89 | { |
---|
90 | if (this->size < (_position + this->getRequestedSize(_data))) |
---|
91 | XIOS_ERROR("CBuffer::setDataArray<T>(data, size, position)", |
---|
92 | << " Buffer size < size + position !"); |
---|
93 | CBufferData bufdata; |
---|
94 | bufdata.type = this->getBufferDataType<T>(); |
---|
95 | bufdata.isArray = true; |
---|
96 | bufdata.size = _data.num_elements() * sizeof (T); |
---|
97 | bufdata.pos = _position + DATA_HEADER_SIZE; |
---|
98 | |
---|
99 | this->setBufferData(bufdata, _position); |
---|
100 | this->setData(reinterpret_cast<const char*>(_data.data()), bufdata.size , bufdata.pos); |
---|
101 | return (bufdata); |
---|
102 | } |
---|
103 | |
---|
104 | } // namespace comm |
---|
105 | } // namespace xmlioserver |
---|
106 | |
---|
107 | #endif //__BUFFER_IMPL_HPP__ |
---|