meta: add c++ wrapper of core module
This commit is contained in:
0
core-mm/include/blue/core.hpp
Normal file
0
core-mm/include/blue/core.hpp
Normal file
106
core-mm/include/blue/core/encoding.hpp
Normal file
106
core-mm/include/blue/core/encoding.hpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#ifndef BLUE_CORE_ENCODING_HPP_
|
||||
#define BLUE_CORE_ENCODING_HPP_
|
||||
|
||||
#include <blue/core/encoding.h>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace blue::core
|
||||
{
|
||||
class wchar
|
||||
{
|
||||
public:
|
||||
static const int32_t INVALID = B_WCHAR_INVALID;
|
||||
|
||||
wchar();
|
||||
wchar(std::int32_t v)
|
||||
: val_(v)
|
||||
{
|
||||
}
|
||||
static wchar decode(const char *s);
|
||||
|
||||
operator int32_t() const
|
||||
{
|
||||
return val_;
|
||||
}
|
||||
|
||||
bool is_alpha(void) const
|
||||
{
|
||||
return b_wchar_is_alpha(val_);
|
||||
}
|
||||
bool is_number(void) const
|
||||
{
|
||||
return b_wchar_is_number(val_);
|
||||
}
|
||||
bool is_bin_digit(void) const
|
||||
{
|
||||
return b_wchar_is_bin_digit(val_);
|
||||
}
|
||||
bool is_oct_digit(void) const
|
||||
{
|
||||
return b_wchar_is_oct_digit(val_);
|
||||
}
|
||||
bool is_hex_digit(void) const
|
||||
{
|
||||
return b_wchar_is_hex_digit(val_);
|
||||
}
|
||||
bool is_space(void) const
|
||||
{
|
||||
return b_wchar_is_space(val_);
|
||||
}
|
||||
bool is_alnum(void) const
|
||||
{
|
||||
return b_wchar_is_alnum(val_);
|
||||
}
|
||||
bool is_punct(void) const
|
||||
{
|
||||
return b_wchar_is_punct(val_);
|
||||
}
|
||||
|
||||
bool is_valid_utf8_scalar(void) const
|
||||
{
|
||||
return b_wchar_utf8_is_valid_scalar(val_);
|
||||
}
|
||||
unsigned int get_utf8_codepoint_size(void) const
|
||||
{
|
||||
return b_wchar_utf8_codepoint_size(val_);
|
||||
}
|
||||
|
||||
unsigned int encode_utf8(char s[4]) const
|
||||
{
|
||||
return b_wchar_utf8_codepoint_encode(val_, s);
|
||||
}
|
||||
|
||||
private:
|
||||
int32_t val_ = B_WCHAR_INVALID;
|
||||
};
|
||||
|
||||
class utf8
|
||||
{
|
||||
public:
|
||||
static unsigned int decode_header(char c)
|
||||
{
|
||||
return b_wchar_utf8_header_decode(c);
|
||||
}
|
||||
static unsigned int get_codepoint_stride(const char *s)
|
||||
{
|
||||
return b_wchar_utf8_codepoint_stride(s);
|
||||
}
|
||||
static std::size_t get_codepoint_count(const char *s, std::size_t nr_bytes)
|
||||
{
|
||||
return b_wchar_utf8_codepoint_count(s, nr_bytes);
|
||||
}
|
||||
static std::size_t get_string_encoded_size(
|
||||
const wchar *s, std::size_t nr_codepoints)
|
||||
{
|
||||
return b_wchar_utf8_string_encoded_size(
|
||||
reinterpret_cast<const b_wchar *>(s), nr_codepoints);
|
||||
}
|
||||
|
||||
private:
|
||||
utf8() = delete;
|
||||
~utf8() = delete;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
46
core-mm/include/blue/core/misc.hpp
Normal file
46
core-mm/include/blue/core/misc.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef BLUE_CORE_MISC_HPP_
|
||||
#define BLUE_CORE_MISC_HPP_
|
||||
|
||||
#define Z__B_ENUM_CLASS_BITWISE_OPS(enum_name) \
|
||||
inline constexpr enum_name operator&(enum_name x, enum_name y) \
|
||||
{ \
|
||||
return static_cast<enum_name>( \
|
||||
static_cast<int>(x) & static_cast<int>(y)); \
|
||||
} \
|
||||
\
|
||||
inline constexpr enum_name operator|(enum_name x, enum_name y) \
|
||||
{ \
|
||||
return static_cast<enum_name>( \
|
||||
static_cast<int>(x) | static_cast<int>(y)); \
|
||||
} \
|
||||
\
|
||||
inline constexpr enum_name operator^(enum_name x, enum_name y) \
|
||||
{ \
|
||||
return static_cast<enum_name>( \
|
||||
static_cast<int>(x) ^ static_cast<int>(y)); \
|
||||
} \
|
||||
\
|
||||
inline constexpr enum_name operator~(enum_name x) \
|
||||
{ \
|
||||
return static_cast<enum_name>(~static_cast<int>(x)); \
|
||||
} \
|
||||
\
|
||||
inline enum_name &operator&=(enum_name &x, enum_name y) \
|
||||
{ \
|
||||
x = x & y; \
|
||||
return x; \
|
||||
} \
|
||||
\
|
||||
inline enum_name &operator|=(enum_name &x, enum_name y) \
|
||||
{ \
|
||||
x = x | y; \
|
||||
return x; \
|
||||
} \
|
||||
\
|
||||
inline enum_name &operator^=(enum_name &x, enum_name y) \
|
||||
{ \
|
||||
x = x ^ y; \
|
||||
return x; \
|
||||
}
|
||||
|
||||
#endif
|
||||
40
core-mm/include/blue/core/object.hpp
Normal file
40
core-mm/include/blue/core/object.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef BLUE_CORE_OBJECT_HPP_
|
||||
#define BLUE_CORE_OBJECT_HPP_
|
||||
|
||||
#include <blue/core/object.h>
|
||||
|
||||
namespace blue::core
|
||||
{
|
||||
class type;
|
||||
class object
|
||||
{
|
||||
public:
|
||||
static const size_t MAGIC = B_OBJECT_MAGIC;
|
||||
object(object &) = delete;
|
||||
object(object &&);
|
||||
|
||||
~object();
|
||||
|
||||
bool operator!()
|
||||
{
|
||||
return ptr_ == nullptr;
|
||||
}
|
||||
|
||||
void to_string(void) const;
|
||||
bool is_type(const type &type) const;
|
||||
b_object *ptr(void)
|
||||
{
|
||||
return ptr_;
|
||||
}
|
||||
const b_object *ptr(void) const
|
||||
{
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
protected:
|
||||
object() = default;
|
||||
b_object *ptr_ = nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
62
core-mm/include/blue/core/status.hpp
Normal file
62
core-mm/include/blue/core/status.hpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#ifndef BLUE_CORE_STATUS_HPP_
|
||||
#define BLUE_CORE_STATUS_HPP_
|
||||
|
||||
#include <blue/core/status.h>
|
||||
|
||||
namespace blue
|
||||
{
|
||||
class status
|
||||
{
|
||||
public:
|
||||
#define __MM_STATUS_ENUM(name) name = B_ERR_##name
|
||||
enum _status : int {
|
||||
SUCCESS = B_SUCCESS,
|
||||
__MM_STATUS_ENUM(NO_MEMORY),
|
||||
__MM_STATUS_ENUM(OUT_OF_BOUNDS),
|
||||
__MM_STATUS_ENUM(INVALID_ARGUMENT),
|
||||
__MM_STATUS_ENUM(NAME_EXISTS),
|
||||
__MM_STATUS_ENUM(NOT_SUPPORTED),
|
||||
__MM_STATUS_ENUM(BAD_STATE),
|
||||
__MM_STATUS_ENUM(NO_ENTRY),
|
||||
__MM_STATUS_ENUM(NO_DATA),
|
||||
__MM_STATUS_ENUM(NO_SPACE),
|
||||
__MM_STATUS_ENUM(UNKNOWN_FUNCTION),
|
||||
__MM_STATUS_ENUM(BAD_FORMAT),
|
||||
__MM_STATUS_ENUM(IO_FAILURE),
|
||||
__MM_STATUS_ENUM(IS_DIRECTORY),
|
||||
__MM_STATUS_ENUM(NOT_DIRECTORY),
|
||||
__MM_STATUS_ENUM(PERMISSION_DENIED),
|
||||
__MM_STATUS_ENUM(BUSY),
|
||||
__MM_STATUS_ENUM(COMPRESSION_FAILURE),
|
||||
__MM_STATUS_ENUM(TYPE_REGISTRATION_FAILURE),
|
||||
__MM_STATUS_ENUM(CLASS_INIT_FAILURE),
|
||||
};
|
||||
|
||||
status() = default;
|
||||
status(_status v)
|
||||
: v_(static_cast<b_status>(v))
|
||||
{
|
||||
}
|
||||
status(b_status v)
|
||||
: v_(v)
|
||||
{
|
||||
}
|
||||
|
||||
bool operator!() const
|
||||
{
|
||||
return v_ != B_SUCCESS;
|
||||
}
|
||||
|
||||
operator b_status() const
|
||||
{
|
||||
return v_;
|
||||
}
|
||||
const char *to_string(void);
|
||||
const char *description(void);
|
||||
|
||||
private:
|
||||
b_status v_ = B_SUCCESS;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
135
core-mm/include/blue/core/stream.hpp
Normal file
135
core-mm/include/blue/core/stream.hpp
Normal file
@@ -0,0 +1,135 @@
|
||||
#ifndef BLUE_CORE_STREAM_HPP_
|
||||
#define BLUE_CORE_STREAM_HPP_
|
||||
|
||||
#include <blue/core/encoding.hpp>
|
||||
#include <blue/core/misc.hpp>
|
||||
#include <blue/core/object.hpp>
|
||||
#include <blue/core/status.hpp>
|
||||
#include <blue/core/stream.h>
|
||||
#include <blue/core/type.hpp>
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
namespace blue::core
|
||||
{
|
||||
class stream_buffer : public object
|
||||
{
|
||||
};
|
||||
|
||||
class stream : public object
|
||||
{
|
||||
public:
|
||||
enum class mode : int {
|
||||
READ = B_STREAM_READ,
|
||||
WRITE = B_STREAM_WRITE,
|
||||
BINARY = B_STREAM_BINARY,
|
||||
__STATIC = Z__B_STREAM_STATIC,
|
||||
};
|
||||
|
||||
enum class seek_origin : int {
|
||||
START = B_STREAM_SEEK_START,
|
||||
CURRENT = B_STREAM_SEEK_CURRENT,
|
||||
END = B_STREAM_SEEK_END,
|
||||
};
|
||||
|
||||
static type get_type(void);
|
||||
static stream &in(void);
|
||||
static stream &out(void);
|
||||
static stream &err(void);
|
||||
|
||||
stream(stream &) = delete;
|
||||
|
||||
stream(std::FILE *fp);
|
||||
|
||||
status reserve(std::size_t len);
|
||||
status seek(long long offset, seek_origin origin);
|
||||
std::size_t cursor(void) const;
|
||||
|
||||
status push_indent(int indent);
|
||||
status pop_indent(void);
|
||||
|
||||
status read_char(wchar &c);
|
||||
|
||||
status read_bytes(void *buf, std::size_t count, std::size_t &nr_read);
|
||||
status read_bytes(void *buf, std::size_t count)
|
||||
{
|
||||
std::size_t tmp;
|
||||
return read_bytes(buf, count, tmp);
|
||||
}
|
||||
|
||||
status read_line(char *s, std::size_t max);
|
||||
status read_line(std::string &out);
|
||||
status read_line(stream &dest);
|
||||
|
||||
status read_all_bytes(void *p, std::size_t max, std::size_t &nr_read);
|
||||
status read_all_bytes(
|
||||
stream &dest, stream_buffer &buf, std::size_t &nr_read);
|
||||
|
||||
status read_all_bytes(void *p, std::size_t max)
|
||||
{
|
||||
std::size_t tmp;
|
||||
return read_all_bytes(p, max);
|
||||
}
|
||||
|
||||
status read_all_bytes(stream &dest, stream_buffer &buf)
|
||||
{
|
||||
std::size_t tmp;
|
||||
return read_all_bytes(dest, buf);
|
||||
}
|
||||
|
||||
status write_char(wchar c);
|
||||
status write_string(const char *s, std::size_t &nr_written);
|
||||
status write_string(const char *s)
|
||||
{
|
||||
std::size_t tmp;
|
||||
return write_string(s, tmp);
|
||||
}
|
||||
|
||||
status write_string(const std::string &s, std::size_t &nr_written)
|
||||
{
|
||||
return write_string(s.c_str(), nr_written);
|
||||
}
|
||||
status write_string(const std::string &s)
|
||||
{
|
||||
std::size_t tmp;
|
||||
return write_string(s, tmp);
|
||||
}
|
||||
|
||||
status write_bytes(
|
||||
const void *buf, std::size_t count, std::size_t &nr_written);
|
||||
status write_bytes(const void *buf, std::size_t count)
|
||||
{
|
||||
std::size_t tmp;
|
||||
return write_bytes(buf, count, tmp);
|
||||
}
|
||||
|
||||
status write_fmt(std::size_t &nr_written, const char *format, ...);
|
||||
status write_vfmt(
|
||||
std::size_t &nr_written, const char *format, std::va_list arg);
|
||||
|
||||
status write_fmt(const char *format, ...)
|
||||
{
|
||||
std::size_t tmp;
|
||||
std::va_list arg;
|
||||
va_start(arg, format);
|
||||
status s = write_vfmt(tmp, format, arg);
|
||||
va_end(arg);
|
||||
return s;
|
||||
}
|
||||
|
||||
status write_vfmt(const char *format, std::va_list arg)
|
||||
{
|
||||
std::size_t tmp;
|
||||
return write_vfmt(tmp, format, arg);
|
||||
}
|
||||
|
||||
protected:
|
||||
stream() = default;
|
||||
};
|
||||
}
|
||||
|
||||
Z__B_ENUM_CLASS_BITWISE_OPS(blue::core::stream::mode)
|
||||
|
||||
#endif
|
||||
23
core-mm/include/blue/core/stringstream.hpp
Normal file
23
core-mm/include/blue/core/stringstream.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef BLUE_CORE_STRINGSTREAM_HPP_
|
||||
#define BLUE_CORE_STRINGSTREAM_HPP_
|
||||
|
||||
#include <blue/core/stream.hpp>
|
||||
|
||||
namespace blue::core
|
||||
{
|
||||
class stringstream : public stream
|
||||
{
|
||||
public:
|
||||
stringstream();
|
||||
stringstream(char *buf, std::size_t max);
|
||||
|
||||
status reset();
|
||||
status reset(char *buf, std::size_t max);
|
||||
|
||||
const char *get_ptr() const;
|
||||
char *steal_ptr();
|
||||
std::size_t get_length() const;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
24
core-mm/include/blue/core/type.hpp
Normal file
24
core-mm/include/blue/core/type.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef BLUE_CORE_TYPE_HPP_
|
||||
#define BLUE_CORE_TYPE_HPP_
|
||||
|
||||
#include <blue/core/type.h>
|
||||
|
||||
namespace blue::core
|
||||
{
|
||||
class type
|
||||
{
|
||||
public:
|
||||
type(b_type p)
|
||||
{
|
||||
ptr_ = p;
|
||||
}
|
||||
~type() = default;
|
||||
|
||||
type(type &) = default;
|
||||
|
||||
private:
|
||||
const union b_type_id *ptr_;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user