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 363 by douglas, 2004-12-21T01:15:40-08:00 vs.
Represent/Binary.hpp (file contents), Revision 373 by douglas, 2004-12-22T17:27:28-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(const std::string& string);
23 <        template <typename Type>
24 <        Binary(const Type& type);
25 <        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  
24 Binary::Binary(const std::string& string)
25 {
26        throw;
27 }
28
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 >        _mforeach (ext::Vector<byte_t>, byte, bytes) *byte = *type_++;
35   }
36  
37 < Binary::operator std::string() const
37 > template <typename Type>
38 > Type Binary::convert(bool signed_)
39   {
40 <        std::string string;
40 >        Type type;
41  
42 <        _rforeach (std::vector<char>, byte, bytes)
42 <        {
43 <                _rfor (char, bit, 0, 8)
44 <                {
45 <                        string += 1 & *byte >> bit ? '1' : '0';
46 <                }
47 <        }
42 >        if (sizeof (type) != bytes.GetSize()) bytes.SetSize(sizeof (type), signed_ && bytes.Last() >> 7 ? ~0 : 0);
43  
44 <        return string;
50 < }
44 >        byte_t* type_(reinterpret_cast<byte_t*>(&type));
45  
46 < std::ostream& operator<<(std::ostream& out, const Binary& binary)
47 < {
48 <        return out << std::string(binary);
46 >        _foreach (ext::Vector<byte_t>, byte, bytes) *type_++ = *byte;
47 >
48 >        return type;
49   }
50  
51 < int main(int argc, char* argv[])
51 > inline ios::PrintWriter& operator<<(ios::PrintWriter& out, const Binary& binary)
52   {
53 <        std::string hello("Hello, World!");
60 <
61 <        _foreach (std::string, atom, hello)
62 <        {
63 <                std::cout << Binary(*atom) << " = " << *atom << std::endl;
64 <        }
65 <
66 <        _fori (index, -10, 11)
67 <        {
68 <                std::cout << Binary(index) << " = " << index << std::endl;
69 <        }
70 <
71 <        _foru (index, 4294967286, 11)
72 <        {
73 <                std::cout << Binary(index) << " = " << index << std::endl;
74 <        }
75 <
76 <        for (float index(-1.0); index < 1.0; index += 0.1)
77 <        {
78 <                std::cout << Binary(index) << " = " << index << std::endl;
79 <        }
80 <
81 <        for (double index(-1.0); index < 1.0; index += 0.1)
82 <        {
83 <                std::cout << Binary(index) << " = " << index << std::endl;
84 <        }
85 <
86 <        return 0;
53 >        return out << binary.operator ext::String();
54   }
55 +
56 + #endif // _Binary_hpp_

Comparing:
Represent/represent.cpp (property svn:eol-style), Revision 363 by douglas, 2004-12-21T01:15:40-08:00 vs.
Represent/Binary.hpp (property svn:eol-style), Revision 373 by douglas, 2004-12-22T17:27:28-08:00

# Line 0 | Line 1
1 + native

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines