schar/uchar -> sbyte/byte, extend write/writeln on Stream, rename Stream::read/write to read/write_bytes to clear ambiguities and overloads, fixes

master
Daniel Kolesa 2015-06-30 23:07:28 +01:00
parent 5ace1c4777
commit 06eeec1ca7
8 changed files with 62 additions and 46 deletions

View File

@ -922,8 +922,8 @@ using AtomicChar = Atomic<char>;
using AtomicShort = Atomic<short>;
using AtomicInt = Atomic<int>;
using AtomicLong = Atomic<long>;
using AtomicSchar = Atomic<octa::schar>;
using AtomicUchar = Atomic<octa::uchar>;
using AtomicSbyte = Atomic<octa::sbyte>;
using AtomicByte = Atomic<octa::byte>;
using AtomicUshort = Atomic<octa::ushort>;
using AtomicUint = Atomic<octa::uint>;
using AtomicUlong = Atomic<octa::ulong>;

View File

@ -203,8 +203,8 @@ OCTA_HASH_BASIC(short)
OCTA_HASH_BASIC(int)
OCTA_HASH_BASIC(long)
OCTA_HASH_BASIC(octa::schar)
OCTA_HASH_BASIC(octa::uchar)
OCTA_HASH_BASIC(octa::sbyte)
OCTA_HASH_BASIC(octa::byte)
OCTA_HASH_BASIC(octa::ushort)
OCTA_HASH_BASIC(octa::uint)
OCTA_HASH_BASIC(octa::ulong)
@ -217,7 +217,7 @@ OCTA_HASH_BASIC(octa::Wchar)
namespace detail {
static inline Size mem_hash(const void *p, octa::Size l) {
const octa::uchar *d = (const octa::uchar *)p;
const octa::byte *d = (const octa::byte *)p;
octa::Size h = 5381;
for (Size i = 0; i < l; ++i) h = ((h << 5) + h) ^ d[i];
return h;

View File

@ -84,14 +84,11 @@ struct FileStream: Stream {
bool flush() { return !fflush(p_f); }
using Stream::read;
using Stream::write;
octa::Size read(void *buf, octa::Size count) {
octa::Size read_bytes(void *buf, octa::Size count) {
return fread(buf, 1, count, p_f);
}
octa::Size write(const void *buf, octa::Size count) {
octa::Size write_bytes(const void *buf, octa::Size count) {
return fwrite(buf, 1, count, p_f);
}
@ -144,8 +141,7 @@ static inline void writeln(const octa::String &s) {
template<typename T>
static inline void writeln(const T &v) {
octa::write(v);
putc('\n', ::stdout);
octa::writeln(octa::to_string(v));
}
template<typename T, typename ...A>

View File

@ -554,10 +554,10 @@ template<typename T> struct Allocator {
Size max_size() const { return Size(~0) / sizeof(T); }
Pointer allocate(Size n, Allocator<void>::ConstPointer = nullptr) {
return (Pointer) ::new octa::uchar[n * sizeof(T)];
return (Pointer) ::new octa::byte[n * sizeof(T)];
}
void deallocate(Pointer p, Size) { ::delete[] (octa::uchar *) p; }
void deallocate(Pointer p, Size) { ::delete[] (octa::byte *) p; }
template<typename U, typename ...A>
void construct(U *p, A &&...args) {
@ -588,10 +588,10 @@ template<typename T> struct Allocator<const T> {
Size max_size() const { return Size(~0) / sizeof(T); }
Pointer allocate(Size n, Allocator<void>::ConstPointer = nullptr) {
return (Pointer) ::new octa::uchar[n * sizeof(T)];
return (Pointer) ::new octa::byte[n * sizeof(T)];
}
void deallocate(Pointer p, Size) { ::delete[] (octa::uchar *) p; }
void deallocate(Pointer p, Size) { ::delete[] (octa::byte *) p; }
template<typename U, typename ...A>
void construct(U *p, A &&...args) {

View File

@ -52,26 +52,35 @@ struct Stream {
virtual bool flush() { return true; }
virtual octa::Size read(void *, octa::Size) { return 0; }
virtual octa::Size write(const void *, octa::Size) { return 0; }
virtual octa::Size read_bytes(void *, octa::Size) { return 0; }
virtual octa::Size write_bytes(const void *, octa::Size) { return 0; }
virtual int read() {
octa::uchar c;
return (read(&c, 1) == 1) ? c : -1;
virtual int getchar() {
octa::byte c;
return (read_bytes(&c, 1) == 1) ? c : -1;
}
virtual bool write(int c) {
octa::uchar wc = octa::uchar(c);
return write(&wc, 1) == 1;
virtual bool putchar(int c) {
octa::byte wc = octa::byte(c);
return write_bytes(&wc, 1) == 1;
}
virtual bool write(const char *s) {
octa::Size len = strlen(s);
return write(s, len) == len;
return write_bytes(s, len) == len;
}
virtual bool write(const octa::String &s) {
return write(s.data(), s.size()) == s.size();
return write_bytes(s.data(), s.size()) == s.size();
}
template<typename T> bool write(const T &v) {
return write(octa::to_string(v));
}
template<typename T, typename ...A>
bool write(const T &v, const A &...args) {
return write(v) && write(args...);
}
virtual bool writeln(const octa::String &s) {
@ -82,23 +91,32 @@ struct Stream {
return write(s) && write('\n');
}
template<typename T> bool writeln(const T &v) {
return writeln(octa::to_string(v));
}
template<typename T, typename ...A>
bool writeln(const T &v, const A &...args) {
return write(v) && write(args...) && write('\n');
}
template<typename T = char>
StreamRange<T> iter();
template<typename T> octa::Size put(const T *v, octa::Size count) {
return write(v, count * sizeof(T)) / sizeof(T);
return write_bytes(v, count * sizeof(T)) / sizeof(T);
}
template<typename T> bool put(T v) {
return write(&v, sizeof(T)) == sizeof(T);
return write_bytes(&v, sizeof(T)) == sizeof(T);
}
template<typename T> octa::Size get(T *v, octa::Size count) {
return read(v, count * sizeof(T)) / sizeof(T);
return read_bytes(v, count * sizeof(T)) / sizeof(T);
}
template<typename T> bool get(T &v) {
return read(&v, sizeof(T)) == sizeof(T);
return read_bytes(&v, sizeof(T)) == sizeof(T);
}
template<typename T> T get() {
@ -122,12 +140,12 @@ struct StreamRange<T, true>: InputRange<
bool pop_front() {
if (empty()) return false;
T val;
return !!p_stream->read(&val, sizeof(T));
return !!p_stream->read_bytes(&val, sizeof(T));
}
T front() const {
T val;
p_stream->seek(-p_stream->read(&val, sizeof(T)), StreamSeek::cur);
p_stream->seek(-p_stream->read_bytes(&val, sizeof(T)), StreamSeek::cur);
return val;
}
@ -136,7 +154,7 @@ struct StreamRange<T, true>: InputRange<
}
void put(T val) {
p_size += p_stream->write(&val, sizeof(T));
p_size += p_stream->write_bytes(&val, sizeof(T));
}
private:

View File

@ -486,12 +486,14 @@ template<> struct ToString<T> { \
} \
};
OCTA_TOSTR_NUM(octa::sbyte, "%d")
OCTA_TOSTR_NUM(int, "%d")
OCTA_TOSTR_NUM(int &, "%d")
OCTA_TOSTR_NUM(long, "%ld")
OCTA_TOSTR_NUM(float, "%f")
OCTA_TOSTR_NUM(double, "%f")
OCTA_TOSTR_NUM(octa::byte, "%u")
OCTA_TOSTR_NUM(octa::uint, "%u")
OCTA_TOSTR_NUM(octa::ulong, "%lu")
OCTA_TOSTR_NUM(octa::llong, "%lld")

View File

@ -98,8 +98,8 @@ namespace detail {
template<> struct IsIntegralBase<int >: True {};
template<> struct IsIntegralBase<long >: True {};
template<> struct IsIntegralBase<octa::uchar >: True {};
template<> struct IsIntegralBase<octa::schar >: True {};
template<> struct IsIntegralBase<octa::sbyte >: True {};
template<> struct IsIntegralBase<octa::byte >: True {};
template<> struct IsIntegralBase<octa::ushort>: True {};
template<> struct IsIntegralBase<octa::uint >: True {};
template<> struct IsIntegralBase<octa::ulong >: True {};
@ -868,13 +868,13 @@ namespace detail {
~TlNat() = delete;
};
using Stypes = TypeList<octa::schar,
using Stypes = TypeList<octa::sbyte,
TypeList<short,
TypeList<int,
TypeList<long,
TypeList<octa::llong, TlNat>>>>>;
using Utypes = TypeList<octa::uchar,
using Utypes = TypeList<octa::byte,
TypeList<octa::ushort,
TypeList<octa::uint,
TypeList<octa::ulong,
@ -953,8 +953,8 @@ namespace detail {
template<> struct MakeSigned<int , true> { using Type = int; };
template<> struct MakeSigned<long , true> { using Type = long; };
template<> struct MakeSigned<octa::schar , true> { using Type = octa::schar; };
template<> struct MakeSigned<octa::uchar , true> { using Type = octa::schar; };
template<> struct MakeSigned<octa::sbyte , true> { using Type = octa::sbyte; };
template<> struct MakeSigned<octa::byte , true> { using Type = octa::sbyte; };
template<> struct MakeSigned<octa::ushort, true> { using Type = short; };
template<> struct MakeSigned<octa::uint , true> { using Type = int; };
template<> struct MakeSigned<octa::ulong , true> { using Type = long; };
@ -966,8 +966,8 @@ namespace detail {
template<> struct MakeUnsigned<int , true> { using Type = octa::uint; };
template<> struct MakeUnsigned<long , true> { using Type = octa::ulong; };
template<> struct MakeUnsigned<octa::schar , true> { using Type = octa::uchar; };
template<> struct MakeUnsigned<octa::uchar , true> { using Type = octa::uchar; };
template<> struct MakeUnsigned<octa::sbyte , true> { using Type = octa::byte; };
template<> struct MakeUnsigned<octa::byte , true> { using Type = octa::byte; };
template<> struct MakeUnsigned<octa::ushort, true> { using Type = octa::ushort; };
template<> struct MakeUnsigned<octa::uint , true> { using Type = octa::uint; };
template<> struct MakeUnsigned<octa::ulong , true> { using Type = octa::ulong; };
@ -1117,14 +1117,14 @@ using CommonType = typename octa::detail::CommonTypeBase<T, U, V...>::Type;
namespace detail {
template<octa::Size N> struct AlignedTest {
union Type {
octa::uchar data[N];
octa::byte data[N];
octa::MaxAlign align;
};
};
template<octa::Size N, octa::Size A> struct AlignedStorageBase {
struct Type {
alignas(A) octa::uchar data[N];
alignas(A) octa::byte data[N];
};
};
}
@ -1157,7 +1157,7 @@ namespace detail {
= AlignMax<alignof(T)...>::value;
struct type {
alignas(alignment_value) octa::uchar data[AlignMax<N,
alignas(alignment_value) octa::byte data[AlignMax<N,
sizeof(T)...>::value];
};
};

View File

@ -13,8 +13,8 @@ namespace octa {
/* "builtin" types */
using schar = signed char;
using uchar = unsigned char;
using sbyte = signed char;
using byte = unsigned char;
using ushort = unsigned short;
using uint = unsigned int;
using ulong = unsigned long;