ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Binary.hpp
(Generate patch)

Comparing:
Represent/represent.cpp (file contents), Revision 365 by douglas, 2004-12-21T03:21:00-08:00 vs.
Represent/Binary.hpp (file contents), Revision 422 by douglas, 2005-03-09T19:08:17-08:00

# Line 4 | Line 4
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;
18 > protected:
19 >        ext::Vector<byte_t> bytes;
20 >        Binary(size_t size, byte_t value) : bytes(size, value) {}
21   public:
22 <        Binary(std::string& string, bool signed_ = false);
23 <        template <typename Type>
24 <        Binary(const Type& type);
25 <        template <typename Type>
26 <        Type convert(bool signed_ = false);
23 <        operator std::string() const;
22 >        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 >        virtual operator ext::String() const;
27   };
28  
26 Binary::Binary(std::string& string, bool signed_) : bytes(string.size() / 8, 0)
27 {
28        std::string::size_type off(string.size() % 8);
29
30        if (off != 0)
31        {
32                bytes.push_back(0);
33                string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0');
34        }
35
36        std::string::size_type index(0);
37
38        _rmforeach (std::vector<char>, byte, bytes)
39        {
40                _rfor (char, bit, 0, 8)
41                {
42                        *byte |= (string[index++] == '1') << bit;
43                }
44        }
45 }
46
29   template <typename Type>
30   Binary::Binary(const Type& type) : bytes(sizeof (type))
31   {
32 <        char* type_(reinterpret_cast<char*>(const_cast<Type*>(&type)));
32 >        byte_t* type_(reinterpret_cast<byte_t*>(const_cast<Type*>(&type)));
33  
34 <        _mforeach (std::vector<char>, byte, bytes) *byte = *type_++;
34 >        _foreach (ext::Vector<byte_t>, byte, bytes) *byte = *type_++;
35   }
36  
37   template <typename Type>
# Line 57 | Line 39 | Type Binary::convert(bool signed_)
39   {
40          Type type;
41  
42 <        if (sizeof (type) != bytes.size()) bytes.resize(sizeof (type), signed_ && bytes.back() >> 7 ? ~0 : 0);
42 >        if (sizeof (type) != bytes.GetSize()) bytes.SetSize(sizeof (type), signed_ && bytes.Last() >> 7 ? ~0 : 0);
43  
44 <        char* type_(reinterpret_cast<char*>(&type));
44 >        byte_t* type_(reinterpret_cast<byte_t*>(&type));
45  
46 <        _foreach (std::vector<char>, byte, bytes) *type_++ = *byte;
46 >        _foreach (ext::Vector<byte_t>, byte, bytes) *type_++ = *byte;
47  
48          return type;
49   }
50  
51 < Binary::operator std::string() const
70 < {
71 <        std::string string;
72 <
73 <        _rforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) string +=1 & *byte >> bit ? '1' : '0';
74 <
75 <        return string;
76 < }
77 <
78 < inline std::ostream& operator<<(std::ostream& out, const Binary& binary)
79 < {
80 <        return out << std::string(binary);
81 < }
82 <
83 < int main(int argc, char* argv[])
84 < {
85 <        std::string hello("Hello, World!");
86 <
87 <        _foreach (std::string, atom, hello)
88 <        {
89 <                std::cout << Binary(*atom) << " = " << *atom << std::endl;
90 <        }
91 <
92 <        _fori (index, -10, 11)
93 <        {
94 <                std::cout << Binary(index) << " = " << index << std::endl;
95 <        }
96 <
97 <        _foru (index, -10, 11)
98 <        {
99 <                std::cout << Binary(index) << " = " << index << std::endl;
100 <        }
101 <
102 <        for (float index(-1.0); index < 1.0; index += 0.1)
103 <        {
104 <                std::cout << Binary(index) << " = " << index << std::endl;
105 <        }
106 <
107 <        for (double index(-1.0); index < 1.0; index += 0.1)
108 <        {
109 <                std::cout << Binary(index) << " = " << index << std::endl;
110 <        }
111 <
112 <        std::string string("101001101001");
113 <        Binary binary(string, true);
114 <
115 <        std::cout << binary << " = " << string << std::endl;
116 <
117 <        float value(binary.convert<float>());
118 <
119 <        std::cout << binary << " = " << value << std::endl << Binary(value) << " = " << value << std::endl;
120 <
121 <        return 0;
122 < }
51 > #endif // _Binary_hpp_

Comparing:
Represent/represent.cpp (property svn:eol-style), Revision 365 by douglas, 2004-12-21T03:21:00-08:00 vs.
Represent/Binary.hpp (property svn:eol-style), Revision 422 by douglas, 2005-03-09T19:08:17-08:00

# Line 0 | Line 1
1 + native

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines