ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Binary.hpp
Revision: 370
Committed: 2004-12-21T22:04:06-08:00 (20 years, 6 months ago) by douglas
File size: 1006 byte(s)
Log Message:
Menes!

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id