summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/kernel/stream.hpp
blob: a4f4d3c95032ec5acde222ad6a890bdd5d8b36f6 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

#ifndef tuscany_stream_hpp
#define tuscany_stream_hpp

/**
 * Basic stream type and functions.
 */

#include <stdarg.h>
#include "config.hpp"
#include "gc.hpp"
#include "string.hpp"

namespace tuscany {

/**
 * Base output stream.
 */
class ostream {
public:
    virtual ostream& vprintf(const char* const fmt, ...) = 0;
    virtual ostream& write(const string& s) = 0;
    virtual ostream& write(const char c) = 0;
    virtual ostream& flush() = 0;
};

/**
 * Flush a stream.
 */
inline ostream& flush(ostream& os) {
    return os.flush();
}

/**
 * Write simple values to a stream.
 */
inline ostream& operator<<(ostream& os, const char* const v) {
    return os.vprintf("%s", v);
}

inline ostream& operator<<(ostream& os, const unsigned char* const v) {
    return os.vprintf("%s", v);
}

inline ostream& operator<<(ostream& os, const char v) {
    return os.write(v);
}

inline ostream& operator<<(ostream& os, const int v) {
    return os.vprintf("%d", v);
}

inline ostream& operator<<(ostream& os, const unsigned int v) {
    return os.vprintf("%u", v);
}

inline ostream& operator<<(ostream& os, const long int v) {
    return os.vprintf("%ld", v);
}

inline ostream& operator<<(ostream& os, const long unsigned int v) {
    return os.vprintf("%lu", v);
}

inline ostream& operator<<(ostream& os, const double v) {
    return os.vprintf("%.16g", v);
}

inline ostream& operator<<(ostream& os, const void* const v) {
    return os.vprintf("%p", v);
}

inline ostream& operator<<(ostream& os, const string& v) {
    return os.write(v);
}

class stream_endl {
} endl;

inline ostream& operator<<(ostream& os, unused const stream_endl e) {
    os.write('\n');
    return os.flush();
}

/*
 * Input stream.
 */
class istream {
public:
    virtual const size_t read(void* const buf, const size_t size) = 0;
    virtual const bool eof() = 0;
    virtual const bool fail() = 0;
    virtual const int get() = 0;
    virtual const int peek() = 0;
};

/**
 * Read from an input stream.
 */
inline const size_t read(istream& is, void* const buf, const size_t size) {
    return is.read(buf, size);
}

/**
 * Return true if the end of an input stream has been reached.
 */
inline const bool eof(istream& is) {
    return is.eof();
}

/**
 * Return true if an input stream can't be accessed.
 */
inline const bool fail(istream& is) {
    return is.fail();
}

/**
 * Read a character from a stream.
 */
inline const int get(istream& is) {
    return is.get();
}

/**
 * Peek a character from a stream.
 */
inline const int peek(istream& is) {
    return is.peek();
}

#ifndef WANT_RAW_PTR

/**
 * Write a pointer.
 */
template<typename T> inline ostream& operator<<(ostream& out, const gc_ptr<T>& p) {
    return out << (T*)p;
}

/**
 * Write a mutable pointer.
 */
template<typename T> inline ostream& operator<<(ostream& out, const gc_mutable_ptr<T>& p) {
    return out << (T*)p;
}

#endif

/**
 * Write a mutable reference.
 */
template<typename T> inline ostream& operator<<(ostream& out, const gc_mutable_ref<T>& r) {
    return out << (T)r;
}

#ifdef WANT_MAINTAINER_LOG

/**
 * Debug stream implementation with no dependencies on anything else.
 */
class odebugstream : public ostream {
public:
    inline odebugstream() : buf() {
    }

    inline odebugstream& vprintf(const char* const fmt, ...) {
        va_list args;
        string s;
        va_start (args, fmt);
        const size_t slen = vsnprintf(NULL, 0, fmt, args);
        char* const sbuf = gc_cnew(slen + 1);
        va_start (args, fmt);
        vsnprintf(sbuf, slen + 1, fmt, args);
        buf = buf + string(sbuf, slen, false);
        va_end (args);
        return *this;
    }

    inline odebugstream& write(const string& s) {
        buf = buf + s;
        return *this;
    }

    inline odebugstream& write(const char c) {
        buf = buf + c;
        return *this;
    }

    inline odebugstream& flush() {
        return *this;
    }

private:
    friend const string str(odebugstream& os);

    gc_mutable_ref<string> buf;
};

const string str(odebugstream& os) {
    return os.buf;
}

#endif

}

#endif /* tuscany_stream_hpp */