summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/bit_stream/bit_stream_kernel_c.h
blob: 1d52bff200f4aae532856cbe84e8f01208e2f7e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (C) 2003  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_BIT_STREAM_KERNEl_C_
#define DLIB_BIT_STREAM_KERNEl_C_

#include "bit_stream_kernel_abstract.h"
#include "../algs.h"
#include "../assert.h"
#include <iosfwd>

namespace dlib
{

    template <
        typename bit_stream_base // implements bit_stream/bit_stream_kernel_abstract.h
        >
    class bit_stream_kernel_c : public bit_stream_base
    {
    public:


        void set_input_stream (
            std::istream& is
        );

        void set_output_stream (
            std::ostream& os
        );

        void close (
        );

        void write (
            int bit
        );

        bool read (
            int& bit
        );

    };

    template <
        typename bit_stream_base
        >
    inline void swap (
        bit_stream_kernel_c<bit_stream_base>& a, 
        bit_stream_kernel_c<bit_stream_base>& b
    ) { a.swap(b); }

// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
    // member function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------

    template <
        typename bit_stream_base
        >
    void bit_stream_kernel_c<bit_stream_base>:: 
    set_input_stream (
        std::istream& is
    )
    {
        // make sure requires clause is not broken
        DLIB_CASSERT(( this->is_in_write_mode() == false ) && ( this->is_in_read_mode() == false ), 
            "\tvoid bit_stream::set_intput_stream"
            << "\n\tbit_stream must not be in write or read mode" 
            << "\n\tthis: " << this
            );

        // call the real function
        bit_stream_base::set_input_stream(is);

    }

// ----------------------------------------------------------------------------------------

    template <
        typename bit_stream_base
        >
    void bit_stream_kernel_c<bit_stream_base>:: 
    set_output_stream (
        std::ostream& os
    )
    {

        // make sure requires clause is not broken
        DLIB_CASSERT(( this->is_in_write_mode() == false ) && ( this->is_in_read_mode() == false ), 
            "\tvoid bit_stream::set_output_stream"
            << "\n\tbit_stream must not be in write or read mode" 
            << "\n\tthis: " << this
            );

        // call the real function
        bit_stream_base::set_output_stream(os);
    }

// ----------------------------------------------------------------------------------------

    template <
        typename bit_stream_base
        >
    void bit_stream_kernel_c<bit_stream_base>:: 
    close (
    )
    {

        // make sure requires clause is not broken
        DLIB_CASSERT(( this->is_in_write_mode() == true ) || ( this->is_in_read_mode() == true ), 
            "\tvoid bit_stream::close"
            << "\n\tyou can't close a bit_stream that isn't open" 
            << "\n\tthis: " << this
            );

        // call the real function
        bit_stream_base::close();
    }

// ----------------------------------------------------------------------------------------

    template <
        typename bit_stream_base
        >
    void bit_stream_kernel_c<bit_stream_base>:: 
    write (
        int bit
    )
    {
        // make sure requires clause is not broken
        DLIB_CASSERT(( this->is_in_write_mode() == true ) && ( bit == 0 || bit == 1 ), 
            "\tvoid bit_stream::write"
            << "\n\tthe bit stream bust be in write mode and bit must be either 1 or 0" 
            << "\n\tis_in_write_mode() == " << this->is_in_write_mode()
            << "\n\tbit == " << bit
            << "\n\tthis: " << this
            );

        // call the real function
        bit_stream_base::write(bit);

    }

// ----------------------------------------------------------------------------------------

    template <
        typename bit_stream_base
        >
    bool bit_stream_kernel_c<bit_stream_base>:: 
    read (
        int& bit
    )
    {

        // make sure requires clause is not broken
        DLIB_CASSERT(( this->is_in_read_mode() == true ), 
            "\tbool bit_stream::read"
            << "\n\tyou can't read from a bit_stream that isn't in read mode" 
            << "\n\tthis: " << this
            );

        // call the real function
        return bit_stream_base::read(bit);

    }

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_BIT_STREAM_KERNEl_C_