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 AtomicShort = Atomic<short>;
using AtomicInt = Atomic<int>; using AtomicInt = Atomic<int>;
using AtomicLong = Atomic<long>; using AtomicLong = Atomic<long>;
using AtomicSchar = Atomic<octa::schar>; using AtomicSbyte = Atomic<octa::sbyte>;
using AtomicUchar = Atomic<octa::uchar>; using AtomicByte = Atomic<octa::byte>;
using AtomicUshort = Atomic<octa::ushort>; using AtomicUshort = Atomic<octa::ushort>;
using AtomicUint = Atomic<octa::uint>; using AtomicUint = Atomic<octa::uint>;
using AtomicUlong = Atomic<octa::ulong>; using AtomicUlong = Atomic<octa::ulong>;

View File

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

View File

@ -84,14 +84,11 @@ struct FileStream: Stream {
bool flush() { return !fflush(p_f); } bool flush() { return !fflush(p_f); }
using Stream::read; octa::Size read_bytes(void *buf, octa::Size count) {
using Stream::write;
octa::Size read(void *buf, octa::Size count) {
return fread(buf, 1, count, p_f); 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); return fwrite(buf, 1, count, p_f);
} }
@ -144,8 +141,7 @@ static inline void writeln(const octa::String &s) {
template<typename T> template<typename T>
static inline void writeln(const T &v) { static inline void writeln(const T &v) {
octa::write(v); octa::writeln(octa::to_string(v));
putc('\n', ::stdout);
} }
template<typename T, typename ...A> 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); } Size max_size() const { return Size(~0) / sizeof(T); }
Pointer allocate(Size n, Allocator<void>::ConstPointer = nullptr) { 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> template<typename U, typename ...A>
void construct(U *p, A &&...args) { 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); } Size max_size() const { return Size(~0) / sizeof(T); }
Pointer allocate(Size n, Allocator<void>::ConstPointer = nullptr) { 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> template<typename U, typename ...A>
void construct(U *p, A &&...args) { void construct(U *p, A &&...args) {

View File

@ -52,26 +52,35 @@ struct Stream {
virtual bool flush() { return true; } virtual bool flush() { return true; }
virtual octa::Size read(void *, octa::Size) { return 0; } virtual octa::Size read_bytes(void *, octa::Size) { return 0; }
virtual octa::Size write(const void *, octa::Size) { return 0; } virtual octa::Size write_bytes(const void *, octa::Size) { return 0; }
virtual int read() { virtual int getchar() {
octa::uchar c; octa::byte c;
return (read(&c, 1) == 1) ? c : -1; return (read_bytes(&c, 1) == 1) ? c : -1;
} }
virtual bool write(int c) { virtual bool putchar(int c) {
octa::uchar wc = octa::uchar(c); octa::byte wc = octa::byte(c);
return write(&wc, 1) == 1; return write_bytes(&wc, 1) == 1;
} }
virtual bool write(const char *s) { virtual bool write(const char *s) {
octa::Size len = strlen(s); octa::Size len = strlen(s);
return write(s, len) == len; return write_bytes(s, len) == len;
} }
virtual bool write(const octa::String &s) { 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) { virtual bool writeln(const octa::String &s) {
@ -82,23 +91,32 @@ struct Stream {
return write(s) && write('\n'); 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> template<typename T = char>
StreamRange<T> iter(); StreamRange<T> iter();
template<typename T> octa::Size put(const T *v, octa::Size count) { 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) { 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) { 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) { 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() { template<typename T> T get() {
@ -122,12 +140,12 @@ struct StreamRange<T, true>: InputRange<
bool pop_front() { bool pop_front() {
if (empty()) return false; if (empty()) return false;
T val; T val;
return !!p_stream->read(&val, sizeof(T)); return !!p_stream->read_bytes(&val, sizeof(T));
} }
T front() const { T front() const {
T val; 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; return val;
} }
@ -136,7 +154,7 @@ struct StreamRange<T, true>: InputRange<
} }
void put(T val) { void put(T val) {
p_size += p_stream->write(&val, sizeof(T)); p_size += p_stream->write_bytes(&val, sizeof(T));
} }
private: 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(int &, "%d") OCTA_TOSTR_NUM(int &, "%d")
OCTA_TOSTR_NUM(long, "%ld") OCTA_TOSTR_NUM(long, "%ld")
OCTA_TOSTR_NUM(float, "%f") OCTA_TOSTR_NUM(float, "%f")
OCTA_TOSTR_NUM(double, "%f") OCTA_TOSTR_NUM(double, "%f")
OCTA_TOSTR_NUM(octa::byte, "%u")
OCTA_TOSTR_NUM(octa::uint, "%u") OCTA_TOSTR_NUM(octa::uint, "%u")
OCTA_TOSTR_NUM(octa::ulong, "%lu") OCTA_TOSTR_NUM(octa::ulong, "%lu")
OCTA_TOSTR_NUM(octa::llong, "%lld") OCTA_TOSTR_NUM(octa::llong, "%lld")

View File

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

View File

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