4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
< |
#include <iostream> |
8 |
< |
#include <string> |
9 |
< |
#include <vector> |
7 |
> |
#ifndef _Binary_hpp_ |
8 |
> |
#define _Binary_hpp_ |
9 |
|
|
10 |
< |
#include <foreach.hpp> |
10 |
> |
#include "Represent.hpp" |
11 |
> |
|
12 |
> |
#ifdef MENES_PRAGMA_ONCE |
13 |
> |
#pragma once |
14 |
> |
#endif |
15 |
|
|
16 |
|
class Binary |
17 |
|
{ |
18 |
|
private: |
19 |
< |
std::vector<char> bytes; |
19 |
> |
ext::Vector<byte_t> bytes; |
20 |
|
public: |
21 |
< |
Binary(std::string& string, bool signed_ = false); |
22 |
< |
template <typename Type> |
23 |
< |
Binary(const Type& type); |
24 |
< |
template <typename Type> |
25 |
< |
Type convert(bool signed_ = false); |
23 |
< |
operator std::string() const; |
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 |
|
|
26 |
– |
Binary::Binary(std::string& string, bool signed_) : bytes(string.size() / 8, 0) |
27 |
– |
{ |
28 |
– |
if (string.find_first_not_of("01") != std::string::npos) throw; |
29 |
– |
|
30 |
– |
std::string::size_type off(string.size() % 8); |
31 |
– |
|
32 |
– |
if (off != 0) |
33 |
– |
{ |
34 |
– |
bytes.push_back(0); |
35 |
– |
string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0'); |
36 |
– |
} |
37 |
– |
|
38 |
– |
std::string::size_type index(0); |
39 |
– |
|
40 |
– |
_rmforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) *byte |= (string[index++] == '1') << bit; |
41 |
– |
} |
42 |
– |
|
28 |
|
template <typename Type> |
29 |
|
Binary::Binary(const Type& type) : bytes(sizeof (type)) |
30 |
|
{ |
31 |
< |
char* type_(reinterpret_cast<char*>(const_cast<Type*>(&type))); |
31 |
> |
byte_t* type_(reinterpret_cast<byte_t*>(const_cast<Type*>(&type))); |
32 |
|
|
33 |
< |
_mforeach (std::vector<char>, byte, bytes) *byte = *type_++; |
33 |
> |
_mforeach (ext::Vector<byte_t>, byte, bytes) *byte = *type_++; |
34 |
|
} |
35 |
|
|
36 |
|
template <typename Type> |
38 |
|
{ |
39 |
|
Type type; |
40 |
|
|
41 |
< |
if (sizeof (type) != bytes.size()) bytes.resize(sizeof (type), signed_ && bytes.back() >> 7 ? ~0 : 0); |
41 |
> |
if (sizeof (type) != bytes.GetSize()) bytes.SetSize(sizeof (type), signed_ && bytes.Last() >> 7 ? ~0 : 0); |
42 |
|
|
43 |
< |
char* type_(reinterpret_cast<char*>(&type)); |
43 |
> |
byte_t* type_(reinterpret_cast<byte_t*>(&type)); |
44 |
|
|
45 |
< |
_foreach (std::vector<char>, byte, bytes) *type_++ = *byte; |
45 |
> |
_foreach (ext::Vector<byte_t>, byte, bytes) *type_++ = *byte; |
46 |
|
|
47 |
|
return type; |
48 |
|
} |
49 |
|
|
50 |
< |
Binary::operator std::string() const |
66 |
< |
{ |
67 |
< |
std::string string; |
68 |
< |
|
69 |
< |
_rforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) string +=1 & *byte >> bit ? '1' : '0'; |
70 |
< |
|
71 |
< |
return string; |
72 |
< |
} |
73 |
< |
|
74 |
< |
inline std::ostream& operator<<(std::ostream& out, const Binary& binary) |
75 |
< |
{ |
76 |
< |
return out << std::string(binary); |
77 |
< |
} |
78 |
< |
|
79 |
< |
int main(int argc, char* argv[]) |
80 |
< |
{ |
81 |
< |
std::string hello("Hello, World!"); |
82 |
< |
|
83 |
< |
_foreach (std::string, atom, hello) std::cout << Binary(*atom) << " = " << *atom << std::endl; |
84 |
< |
|
85 |
< |
_fori (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl; |
86 |
< |
|
87 |
< |
_foru (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl; |
88 |
< |
|
89 |
< |
for (float index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl; |
90 |
< |
|
91 |
< |
for (double index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl; |
92 |
< |
|
93 |
< |
std::string string("101001101001"); |
94 |
< |
Binary binary(string, true); |
95 |
< |
|
96 |
< |
std::cout << binary << " = " << string << std::endl; |
97 |
< |
|
98 |
< |
float value(binary.convert<float>()); |
99 |
< |
|
100 |
< |
std::cout << binary << " = " << value << std::endl << Binary(value) << " = " << value << std::endl; |
101 |
< |
|
102 |
< |
return 0; |
103 |
< |
} |
50 |
> |
#endif // _Binary_hpp_ |