Compare commits
4 Commits
main
...
146db5f6ef
| Author | SHA1 | Date | |
|---|---|---|---|
| 146db5f6ef | |||
| 13136ecbd6 | |||
| e0efbd1ec4 | |||
| e0aea0be19 |
@@ -1,15 +1,19 @@
|
||||
cmake_minimum_required(VERSION 3.25)
|
||||
project(bluelib C)
|
||||
project(bluelib C CXX)
|
||||
|
||||
include (TestBigEndian)
|
||||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_C_EXTENSIONS OFF)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
|
||||
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||
|
||||
set(b_modules core ds serial term cmd io compress)
|
||||
set(b_modules
|
||||
core ds serial term cmd io compress
|
||||
core-mm)
|
||||
|
||||
set(b_system_name ${CMAKE_SYSTEM_NAME})
|
||||
string(TOLOWER ${b_system_name} b_system_name)
|
||||
@@ -37,7 +41,7 @@ foreach (module ${b_modules})
|
||||
|
||||
endif ()
|
||||
|
||||
file(GLOB test_sources test/${module}/*.c)
|
||||
file(GLOB test_sources test/${module}/*.c test/${module}/*.cpp)
|
||||
list(REMOVE_ITEM test_sources "${CMAKE_CURRENT_SOURCE_DIR}/test/${module}/${module}-units.c")
|
||||
|
||||
foreach (test_file ${test_sources})
|
||||
|
||||
@@ -105,3 +105,118 @@ function(add_bluelib_module)
|
||||
install(FILES ${root_header} DESTINATION include/blue)
|
||||
install(FILES ${headers} DESTINATION include/blue/${module_name})
|
||||
endfunction(add_bluelib_module)
|
||||
|
||||
function(add_bluelib_mm_module)
|
||||
set(options)
|
||||
set(one_value_args NAME)
|
||||
set(multi_value_args
|
||||
DEPENDENCIES
|
||||
SUBDIRS
|
||||
EXTRA_SOURCES
|
||||
LIBS
|
||||
INCLUDE_DIRS
|
||||
DEFINES)
|
||||
cmake_parse_arguments(PARSE_ARGV 0 arg
|
||||
"${options}"
|
||||
"${one_value_args}"
|
||||
"${multi_value_args}")
|
||||
|
||||
set(short_module_name ${arg_NAME})
|
||||
set(module_name ${arg_NAME}-mm)
|
||||
|
||||
file(GLOB sources
|
||||
*.c *.h
|
||||
*.cpp *.hpp)
|
||||
|
||||
foreach (dir ${arg_SUBDIRS})
|
||||
file(GLOB dir_sources
|
||||
${dir}/*.c ${dir}/*.h
|
||||
${dir}/*.cpp ${dir}/*.hpp)
|
||||
set(sources ${sources} ${dir_sources})
|
||||
endforeach (dir)
|
||||
|
||||
file(GLOB sys_sources
|
||||
sys/${b_system_name}/*.c sys/${b_system_name}/*.h
|
||||
sys/${b_system_name}/*.cpp sys/${b_system_name}/*.hpp)
|
||||
set(root_header include/blue/${short_module_name}.hpp)
|
||||
file(GLOB headers include/blue/${short_module_name}/*.hpp)
|
||||
|
||||
string(REPLACE "-" "_" module_preproc_token ${short_module_name})
|
||||
string(TOUPPER ${module_preproc_token} module_preproc_token)
|
||||
set(module_preproc_token BLUELIB_${module_preproc_token})
|
||||
|
||||
message(STATUS "Building mm module ${short_module_name} (shared)")
|
||||
add_library(blue-${module_name} SHARED
|
||||
${sources}
|
||||
${sys_sources}
|
||||
${root_header}
|
||||
${headers}
|
||||
${arg_EXTRA_SOURCES})
|
||||
message(STATUS "Building mm module ${short_module_name} (static)")
|
||||
add_library(blue-${module_name}-s STATIC
|
||||
${sources}
|
||||
${sys_sources}
|
||||
${root_header}
|
||||
${headers}
|
||||
${arg_EXTRA_SOURCES})
|
||||
|
||||
target_include_directories(blue-${module_name} PUBLIC include/)
|
||||
target_include_directories(blue-${module_name}-s PUBLIC include/)
|
||||
|
||||
target_compile_definitions(blue-${module_name} PUBLIC
|
||||
${module_preproc_token}
|
||||
BLUELIB_EXPORT=1)
|
||||
target_compile_definitions(blue-${module_name}-s PUBLIC
|
||||
${module_preproc_token}
|
||||
BLUELIB_EXPORT=1
|
||||
BLUELIB_STATIC=1)
|
||||
|
||||
set_target_properties(blue-${module_name}
|
||||
PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
foreach (dep ${arg_DEPENDENCIES})
|
||||
target_link_libraries(blue-${module_name} blue-${dep})
|
||||
target_link_libraries(blue-${module_name}-s blue-${dep}-s)
|
||||
endforeach (dep)
|
||||
|
||||
foreach (lib ${arg_LIBS})
|
||||
target_link_libraries(blue-${module_name} ${lib})
|
||||
target_link_libraries(blue-${module_name}-s ${lib})
|
||||
endforeach (lib)
|
||||
|
||||
foreach (dir ${arg_INCLUDE_DIRS})
|
||||
target_include_directories(blue-${module_name} PRIVATE
|
||||
${dir})
|
||||
target_include_directories(blue-${module_name}-s PRIVATE
|
||||
${dir})
|
||||
endforeach (dir)
|
||||
|
||||
foreach (def ${arg_DEFINES})
|
||||
target_compile_definitions(blue-${module_name} PRIVATE
|
||||
${def})
|
||||
target_compile_definitions(blue-${module_name}-s PRIVATE
|
||||
${def})
|
||||
endforeach (def)
|
||||
|
||||
set_target_properties(blue-${module_name} PROPERTIES
|
||||
FOLDER "mm/Shared/${short_module_name}")
|
||||
set_target_properties(blue-${module_name}-s PROPERTIES
|
||||
FOLDER "mm/Static/${short_module_name}")
|
||||
|
||||
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
|
||||
if(IS_BIG_ENDIAN)
|
||||
target_compile_definitions(blue-${module_name} PRIVATE
|
||||
BIG_ENDIAN)
|
||||
target_compile_definitions(blue-${module_name}-s PRIVATE
|
||||
BIG_ENDIAN)
|
||||
else()
|
||||
target_compile_definitions(blue-${module_name} PRIVATE
|
||||
LITTLE_ENDIAN)
|
||||
target_compile_definitions(blue-${module_name}-s PRIVATE
|
||||
LITTLE_ENDIAN)
|
||||
endif()
|
||||
|
||||
install(TARGETS blue-${module_name} blue-${module_name}-s)
|
||||
install(FILES ${root_header} DESTINATION include/blue)
|
||||
install(FILES ${headers} DESTINATION include/blue/${short_module_name})
|
||||
endfunction(add_bluelib_mm_module)
|
||||
|
||||
6
core-mm/CMakeLists.txt
Normal file
6
core-mm/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
include(../cmake/Templates.cmake)
|
||||
|
||||
add_bluelib_mm_module(
|
||||
NAME core
|
||||
DEPENDENCIES core
|
||||
SUBDIRS hash)
|
||||
1
core-mm/encoding.cpp
Normal file
1
core-mm/encoding.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include <blue/core/encoding.hpp>
|
||||
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
|
||||
20
core-mm/object.cpp
Normal file
20
core-mm/object.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <blue/core/object.hpp>
|
||||
|
||||
namespace blue::core
|
||||
{
|
||||
|
||||
object::object(object &&other)
|
||||
{
|
||||
ptr_ = other.ptr_;
|
||||
other.ptr_ = nullptr;
|
||||
}
|
||||
|
||||
object::~object()
|
||||
{
|
||||
if (ptr_) {
|
||||
b_object_unref(ptr_);
|
||||
ptr_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
169
core-mm/stream.cpp
Normal file
169
core-mm/stream.cpp
Normal file
@@ -0,0 +1,169 @@
|
||||
#include <blue/core/stream.h>
|
||||
#include <blue/core/stream.hpp>
|
||||
|
||||
namespace blue::core
|
||||
{
|
||||
type stream::get_type(void)
|
||||
{
|
||||
return b_stream_get_type();
|
||||
}
|
||||
|
||||
stream &stream::in(void)
|
||||
{
|
||||
static stream s;
|
||||
s.ptr_ = b_stdin;
|
||||
return s;
|
||||
}
|
||||
|
||||
stream &stream::out(void)
|
||||
{
|
||||
static stream s;
|
||||
s.ptr_ = b_stdout;
|
||||
return s;
|
||||
}
|
||||
|
||||
stream &stream::err(void)
|
||||
{
|
||||
static stream s;
|
||||
s.ptr_ = b_stderr;
|
||||
return s;
|
||||
}
|
||||
|
||||
stream::stream(std::FILE *fp)
|
||||
{
|
||||
ptr_ = b_stream_open_fp(fp);
|
||||
}
|
||||
|
||||
status stream::reserve(std::size_t len)
|
||||
{
|
||||
return b_stream_reserve(ptr_, len);
|
||||
}
|
||||
|
||||
status stream::seek(long long offset, seek_origin origin)
|
||||
{
|
||||
return b_stream_seek(
|
||||
ptr_, offset, static_cast<b_stream_seek_origin>(origin));
|
||||
}
|
||||
|
||||
std::size_t stream::cursor(void) const
|
||||
{
|
||||
return b_stream_cursor(ptr_);
|
||||
}
|
||||
|
||||
status stream::push_indent(int indent)
|
||||
{
|
||||
return b_stream_push_indent(ptr_, indent);
|
||||
}
|
||||
|
||||
status stream::pop_indent(void)
|
||||
{
|
||||
return b_stream_pop_indent(ptr_);
|
||||
}
|
||||
|
||||
status stream::read_char(wchar &c)
|
||||
{
|
||||
b_wchar tmp;
|
||||
status s = b_stream_read_char(ptr_, &tmp);
|
||||
c = tmp;
|
||||
return s;
|
||||
}
|
||||
|
||||
status stream::read_bytes(void *buf, std::size_t count, std::size_t &nr_read)
|
||||
{
|
||||
std::size_t r;
|
||||
status s = b_stream_read_bytes(ptr_, buf, count, &r);
|
||||
nr_read = r;
|
||||
return s;
|
||||
}
|
||||
|
||||
status stream::read_line(char *s, std::size_t max)
|
||||
{
|
||||
return b_stream_read_line(ptr_, s, max);
|
||||
}
|
||||
|
||||
status stream::read_line(std::string &out)
|
||||
{
|
||||
while (1) {
|
||||
wchar c = wchar::INVALID;
|
||||
status s = read_char(c);
|
||||
if (!s) {
|
||||
return s;
|
||||
}
|
||||
|
||||
if (c == '\n') {
|
||||
break;
|
||||
}
|
||||
|
||||
char str[5] = {0};
|
||||
c.encode_utf8(str);
|
||||
out.append(str);
|
||||
}
|
||||
|
||||
return status::SUCCESS;
|
||||
}
|
||||
|
||||
status stream::read_line(stream &dest)
|
||||
{
|
||||
return b_stream_read_line_s(ptr_, dest.ptr_);
|
||||
}
|
||||
|
||||
status stream::read_all_bytes(void *p, std::size_t max, std::size_t &nr_read)
|
||||
{
|
||||
std::size_t tmp;
|
||||
status s = b_stream_read_all_bytes(ptr_, p, max, &tmp);
|
||||
nr_read = tmp;
|
||||
return s;
|
||||
}
|
||||
|
||||
status stream::read_all_bytes(stream &dest, stream_buffer &buf, std::size_t &nr_read)
|
||||
{
|
||||
std::size_t tmp;
|
||||
status s = b_stream_read_all_bytes_s(ptr_, dest.ptr_, buf.ptr(), &tmp);
|
||||
nr_read = tmp;
|
||||
return s;
|
||||
}
|
||||
|
||||
status stream::write_char(wchar c)
|
||||
{
|
||||
return b_stream_write_char(ptr_, c);
|
||||
}
|
||||
|
||||
status stream::write_string(const char *str, std::size_t &nr_written)
|
||||
{
|
||||
std::size_t tmp;
|
||||
status s = b_stream_write_string(ptr_, str, &tmp);
|
||||
nr_written = tmp;
|
||||
return s;
|
||||
}
|
||||
|
||||
status stream::write_bytes(
|
||||
const void *buf, std::size_t count, std::size_t &nr_written)
|
||||
{
|
||||
std::size_t tmp;
|
||||
status s = b_stream_write_bytes(ptr_, buf, count, &tmp);
|
||||
nr_written = tmp;
|
||||
return s;
|
||||
}
|
||||
|
||||
status stream::write_fmt(std::size_t &nr_written, const char *format, ...)
|
||||
{
|
||||
size_t tmp;
|
||||
std::va_list arg;
|
||||
va_start(arg, format);
|
||||
status s = b_stream_write_vfmt(ptr_, &tmp, format, arg);
|
||||
va_end(arg);
|
||||
|
||||
nr_written = tmp;
|
||||
return s;
|
||||
}
|
||||
|
||||
status stream::write_vfmt(std::size_t &nr_written, const char *format, va_list arg)
|
||||
{
|
||||
size_t tmp;
|
||||
status s = b_stream_write_vfmt(ptr_, &tmp, format, arg);
|
||||
|
||||
nr_written = tmp;
|
||||
return s;
|
||||
}
|
||||
|
||||
}
|
||||
42
core-mm/stringstream.cpp
Normal file
42
core-mm/stringstream.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <blue/core/stringstream.hpp>
|
||||
|
||||
namespace blue::core
|
||||
{
|
||||
|
||||
stringstream::stringstream()
|
||||
{
|
||||
ptr_ = b_stringstream_create();
|
||||
}
|
||||
|
||||
stringstream::stringstream(char *buf, std::size_t max)
|
||||
{
|
||||
ptr_ = b_stringstream_create_with_buffer(buf, max);
|
||||
}
|
||||
|
||||
status stringstream::reset()
|
||||
{
|
||||
return b_stringstream_reset(ptr_);
|
||||
}
|
||||
|
||||
status stringstream::reset(char *buf, std::size_t max)
|
||||
{
|
||||
return b_stringstream_reset_with_buffer(ptr_, buf, max);
|
||||
}
|
||||
|
||||
const char *stringstream::get_ptr() const
|
||||
{
|
||||
return b_stringstream_ptr(ptr_);
|
||||
}
|
||||
|
||||
char *stringstream::steal_ptr()
|
||||
{
|
||||
return b_stringstream_steal(ptr_);
|
||||
}
|
||||
|
||||
std::size_t stringstream::get_length() const
|
||||
{
|
||||
return b_stringstream_get_length(ptr_);
|
||||
}
|
||||
|
||||
}
|
||||
1
core-mm/type.cpp
Normal file
1
core-mm/type.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include <blue/core/type.hpp>
|
||||
@@ -28,7 +28,7 @@ const char *b_class_get_name(const struct _b_class *c)
|
||||
return c->c_type->r_info->t_name;
|
||||
}
|
||||
|
||||
void *b_class_get_interface(const struct _b_class *c, const union b_type *id)
|
||||
void *b_class_get_interface(const struct _b_class *c, const union b_type_id *id)
|
||||
{
|
||||
if (!c) {
|
||||
return NULL;
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
B_DECLS_BEGIN;
|
||||
|
||||
#define B_WCHAR_INVALID ((b_wchar) - 1)
|
||||
|
||||
typedef int32_t b_wchar;
|
||||
@@ -38,4 +40,6 @@ BLUE_API size_t b_wchar_utf8_codepoint_count(const char *s, size_t nr_bytes);
|
||||
BLUE_API size_t b_wchar_utf8_string_encoded_size(
|
||||
const b_wchar *s, size_t nr_codepoints);
|
||||
|
||||
B_DECLS_END;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define BLUE_CORE_MACROS_H_
|
||||
|
||||
#include <blue/core/class.h>
|
||||
#include <blue/core/misc.h>
|
||||
#include <blue/core/object.h>
|
||||
#include <blue/core/thread.h>
|
||||
#include <blue/core/type.h>
|
||||
@@ -10,6 +11,8 @@
|
||||
#define __B_IFACE_I0(p, x) p##x
|
||||
#define __B_IFACE_I1(p, x) __B_IFACE_I0(p, x)
|
||||
|
||||
#define B_STRUCT_EMPTY char _x
|
||||
|
||||
/* Type definitions macros (for use in .c source file) */
|
||||
|
||||
#define B_TYPE_CLASS_DEFINITION_BEGIN(type_name) \
|
||||
@@ -186,12 +189,4 @@
|
||||
func_name(priv); \
|
||||
} while (0)
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define B_DECLS_BEGIN extern "C" {
|
||||
#define B_DECLS_END }
|
||||
#else
|
||||
#define B_DECLS_BEGIN
|
||||
#define B_DECLS_END
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -8,6 +8,14 @@
|
||||
#define _Nonnull
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define B_DECLS_BEGIN extern "C" {
|
||||
#define B_DECLS_END }
|
||||
#else
|
||||
#define B_DECLS_BEGIN
|
||||
#define B_DECLS_END
|
||||
#endif
|
||||
|
||||
#define B_NPOS ((size_t)-1)
|
||||
|
||||
#define b_min(type, x, y) (z__b_min_##type(x, y))
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <blue/core/misc.h>
|
||||
#include <blue/core/type.h>
|
||||
|
||||
B_DECLS_BEGIN;
|
||||
|
||||
#define B_OBJECT_MAGIC 0xDECAFC0C0ABEEF13ULL
|
||||
|
||||
#define B_OBJECT(p) ((b_object *)(p))
|
||||
@@ -36,4 +38,6 @@ BLUE_API b_object *b_object_create(b_type type);
|
||||
BLUE_API void b_object_to_string(const b_object *p, B_TYPE_FWDREF(b_stream) * out);
|
||||
BLUE_API bool b_object_is_type(const b_object *p, b_type type);
|
||||
|
||||
B_DECLS_END;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -48,6 +48,7 @@ B_TYPE_CLASS_DECLARATION_BEGIN(b_stream)
|
||||
B_TYPE_CLASS_DECLARATION_END(b_stream)
|
||||
|
||||
B_TYPE_CLASS_DECLARATION_BEGIN(b_stream_buffer)
|
||||
B_STRUCT_EMPTY;
|
||||
B_TYPE_CLASS_DECLARATION_END(b_stream_buffer)
|
||||
|
||||
BLUE_API b_type b_stream_get_type();
|
||||
|
||||
@@ -14,6 +14,7 @@ B_DECLS_BEGIN;
|
||||
B_DECLARE_TYPE(b_stringstream);
|
||||
|
||||
B_TYPE_CLASS_DECLARATION_BEGIN(b_stringstream)
|
||||
B_STRUCT_EMPTY;
|
||||
B_TYPE_CLASS_DECLARATION_END(b_stringstream)
|
||||
|
||||
BLUE_API b_type b_stringstream_get_type(void);
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
B_DECLS_BEGIN;
|
||||
|
||||
#define B_TYPE_MAX_INTERFACES 64
|
||||
|
||||
struct _b_class;
|
||||
@@ -16,7 +18,7 @@ typedef void (*b_class_init_function)(struct _b_class *, void *);
|
||||
typedef void (*b_instance_init_function)(struct _b_object *, void *);
|
||||
typedef void (*b_instance_fini_function)(struct _b_object *, void *);
|
||||
|
||||
typedef const union b_type {
|
||||
typedef const union b_type_id {
|
||||
struct {
|
||||
uint64_t p00, p01;
|
||||
} a;
|
||||
@@ -29,11 +31,11 @@ typedef enum b_type_flags {
|
||||
} b_type_flags;
|
||||
|
||||
typedef struct b_type_info {
|
||||
union b_type t_id;
|
||||
union b_type t_parent_id;
|
||||
union b_type_id t_id;
|
||||
union b_type_id t_parent_id;
|
||||
const char *t_name;
|
||||
b_type_flags t_flags;
|
||||
union b_type t_interfaces[B_TYPE_MAX_INTERFACES];
|
||||
union b_type_id t_interfaces[B_TYPE_MAX_INTERFACES];
|
||||
size_t t_nr_interfaces;
|
||||
size_t t_class_size;
|
||||
b_class_init_function t_class_init;
|
||||
@@ -44,9 +46,9 @@ typedef struct b_type_info {
|
||||
} b_type_info;
|
||||
|
||||
BLUE_API void b_type_id_init(
|
||||
union b_type *out, uint32_t a, uint16_t b, uint16_t c, uint16_t d,
|
||||
union b_type_id *out, uint32_t a, uint16_t b, uint16_t c, uint16_t d,
|
||||
uint64_t e);
|
||||
static inline void b_type_id_copy(b_type src, union b_type *dest)
|
||||
static inline void b_type_id_copy(b_type src, union b_type_id *dest)
|
||||
{
|
||||
dest->a.p00 = src->a.p00;
|
||||
dest->a.p01 = src->a.p01;
|
||||
@@ -58,9 +60,11 @@ static inline int b_type_id_compare(b_type a, b_type b)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return memcmp(a, b, sizeof(union b_type));
|
||||
return memcmp(a, b, sizeof(union b_type_id));
|
||||
}
|
||||
|
||||
BLUE_API b_result b_type_register(b_type_info *info);
|
||||
|
||||
B_DECLS_END;
|
||||
|
||||
#endif
|
||||
|
||||
10
core/type.c
10
core/type.c
@@ -12,7 +12,7 @@
|
||||
#include <string.h>
|
||||
|
||||
static struct b_btree type_list = B_BTREE_INIT;
|
||||
static union b_type zero_id = {0};
|
||||
static union b_type_id zero_id = {0};
|
||||
|
||||
struct type_init_ctx {
|
||||
size_t ctx_class_offset;
|
||||
@@ -39,7 +39,7 @@ B_BTREE_DEFINE_INSERT(
|
||||
put_type_component, component_compare)
|
||||
|
||||
static struct b_type_registration *get_type(
|
||||
const b_btree *tree, const union b_type *key)
|
||||
const b_btree *tree, const union b_type_id *key)
|
||||
{
|
||||
b_btree_node *cur = tree->b_root;
|
||||
while (cur) {
|
||||
@@ -60,7 +60,7 @@ static struct b_type_registration *get_type(
|
||||
}
|
||||
|
||||
struct b_type_component *b_type_get_component(
|
||||
const b_btree *tree, const union b_type *key)
|
||||
const b_btree *tree, const union b_type_id *key)
|
||||
{
|
||||
b_btree_node *cur = tree->b_root;
|
||||
while (cur) {
|
||||
@@ -96,7 +96,7 @@ static struct b_type_component *create_type_component(
|
||||
}
|
||||
|
||||
void b_type_id_init(
|
||||
union b_type *out, uint32_t a, uint16_t b, uint16_t c, uint16_t d,
|
||||
union b_type_id *out, uint32_t a, uint16_t b, uint16_t c, uint16_t d,
|
||||
uint64_t e)
|
||||
{
|
||||
b_i32 x_a = b_i32_htob(a);
|
||||
@@ -157,7 +157,7 @@ static b_result locate_interface(
|
||||
}
|
||||
|
||||
static b_result locate_interfaces(
|
||||
const union b_type *interfaces, size_t nr_interfaces,
|
||||
const union b_type_id *interfaces, size_t nr_interfaces,
|
||||
struct b_type_registration *dest, struct type_init_ctx *init_ctx)
|
||||
{
|
||||
b_result result = B_RESULT_SUCCESS;
|
||||
|
||||
@@ -35,6 +35,6 @@ struct b_type_registration {
|
||||
|
||||
extern struct b_type_registration *b_type_get_registration(b_type id);
|
||||
extern struct b_type_component *b_type_get_component(
|
||||
const b_btree *tree, const union b_type *key);
|
||||
const b_btree *tree, const union b_type_id *key);
|
||||
|
||||
#endif
|
||||
|
||||
18
test/core-mm/streams.cpp
Normal file
18
test/core-mm/streams.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <blue/core/stream.hpp>
|
||||
#include <blue/core/stringstream.hpp>
|
||||
#include <iostream>
|
||||
|
||||
using namespace blue::core;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
stream &out = stream::out();
|
||||
out.write_fmt("Hello, %d\n", 32);
|
||||
|
||||
stringstream s;
|
||||
s.write_fmt("Hello, %d\n", 64);
|
||||
|
||||
std::cout << s.get_ptr();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user