1 |
douglas |
362 |
// Represent |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
douglas |
370 |
#ifndef _Binary_hpp_ |
8 |
|
|
#define _Binary_hpp_ |
9 |
douglas |
362 |
|
10 |
douglas |
370 |
#include "Represent.hpp" |
11 |
douglas |
362 |
|
12 |
douglas |
370 |
#ifdef MENES_PRAGMA_ONCE |
13 |
|
|
#pragma once |
14 |
|
|
#endif |
15 |
|
|
|
16 |
douglas |
362 |
class Binary |
17 |
|
|
{ |
18 |
douglas |
371 |
protected: |
19 |
douglas |
370 |
ext::Vector<byte_t> bytes; |
20 |
douglas |
371 |
Binary(size_t size, byte_t value) : bytes(size, value) {} |
21 |
douglas |
362 |
public: |
22 |
douglas |
370 |
Binary() : bytes(size_t(1), 0) {} |
23 |
|
|
Binary(const ext::String& string, bool signed_); |
24 |
|
|
template <typename Type> Binary(const Type& type); |
25 |
|
|
template <typename Type> Type convert(bool signed_); |
26 |
douglas |
371 |
virtual operator ext::String() const; |
27 |
douglas |
362 |
}; |
28 |
|
|
|
29 |
|
|
template <typename Type> |
30 |
|
|
Binary::Binary(const Type& type) : bytes(sizeof (type)) |
31 |
|
|
{ |
32 |
douglas |
370 |
byte_t* type_(reinterpret_cast<byte_t*>(const_cast<Type*>(&type))); |
33 |
douglas |
362 |
|
34 |
douglas |
422 |
_foreach (ext::Vector<byte_t>, byte, bytes) *byte = *type_++; |
35 |
douglas |
362 |
} |
36 |
|
|
|
37 |
douglas |
365 |
template <typename Type> |
38 |
|
|
Type Binary::convert(bool signed_) |
39 |
|
|
{ |
40 |
|
|
Type type; |
41 |
|
|
|
42 |
douglas |
370 |
if (sizeof (type) != bytes.GetSize()) bytes.SetSize(sizeof (type), signed_ && bytes.Last() >> 7 ? ~0 : 0); |
43 |
douglas |
365 |
|
44 |
douglas |
370 |
byte_t* type_(reinterpret_cast<byte_t*>(&type)); |
45 |
douglas |
365 |
|
46 |
douglas |
370 |
_foreach (ext::Vector<byte_t>, byte, bytes) *type_++ = *byte; |
47 |
douglas |
365 |
|
48 |
|
|
return type; |
49 |
|
|
} |
50 |
|
|
|
51 |
douglas |
370 |
#endif // _Binary_hpp_ |