Skip to content

Commit

Permalink
feat(libcxx): add ostream
Browse files Browse the repository at this point in the history
Signed-off-by: Zone.N <[email protected]>
  • Loading branch information
MRNIU committed May 31, 2024
1 parent 95d3906 commit 4f17eb2
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/kernel/libcxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enable_language(CXX)
# 生成对象库
add_library(${PROJECT_NAME} OBJECT
${PROJECT_SOURCE_DIR}/libcxx.cpp
${PROJECT_SOURCE_DIR}/iostream.cpp
)

# 添加定义
Expand All @@ -35,3 +36,8 @@ target_compile_options(${PROJECT_NAME} PRIVATE
target_link_options(${PROJECT_NAME} PRIVATE
${DEFAULT_KERNEL_LINK_OPTIONS}
)

# 添加要链接的库
target_link_libraries(${PROJECT_NAME} PRIVATE
${DEFAULT_KERNEL_LINK_LIB}
)
55 changes: 55 additions & 0 deletions src/kernel/libcxx/include/iostream
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

/**
* @file iostream
* @brief C++ 输入输出
* @author Zone.N ([email protected])
* @version 1.0
* @date 2021-09-18
* @copyright MIT LICENSE
* https://github.com/Simple-XX/SimpleKernel
* Based on https://github.com/MRNIU/MiniCRT
* @par change log:
* <table>
* <tr><th>Date<th>Author<th>Description
* <tr><td>2021-09-18<td>digmouse233<td>迁移到 doxygen
* </table>
*/

#ifndef SIMPLEKERNEL_SRC_KERNEL_LIBCXX_INCLUDE_IOSTREAM_
#define SIMPLEKERNEL_SRC_KERNEL_LIBCXX_INCLUDE_IOSTREAM_

#include <cstdint>

namespace std {
class ostream {
public:
enum openmode : uint8_t {
in = 1,
out = 2,
binary = 4,
trunc = 8,
};

/// @name 构造/析构函数
/// @{
ostream() = default;
ostream(const ostream&) = default;
ostream(ostream&&) = default;
auto operator=(const ostream&) -> ostream& = default;
auto operator=(ostream&&) -> ostream& = default;
~ostream() = default;
/// @}

ostream& operator<<(char c);
ostream& operator<<(int n);
ostream& operator<<(const char* lhs);
ostream& operator<<(ostream& (*)(ostream&));
};

inline ostream& endl(ostream& lhs) { return lhs << '\n'; }

static ostream cout [[maybe_unused]];

}; // namespace std

#endif /* SIMPLEKERNEL_SRC_KERNEL_LIBCXX_INCLUDE_IOSTREAM_ */
43 changes: 43 additions & 0 deletions src/kernel/libcxx/iostream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

/**
* @file iostream
* @brief C++ 输入输出
* @author Zone.N ([email protected])
* @version 1.0
* @date 2021-09-18
* @copyright MIT LICENSE
* https://github.com/Simple-XX/SimpleKernel
* Based on https://github.com/MRNIU/MiniCRT
* @par change log:
* <table>
* <tr><th>Date<th>Author<th>Description
* <tr><td>2021-09-18<td>digmouse233<td>迁移到 doxygen
* </table>
*/

#include "iostream"

#include "cstdio"

namespace std {

ostream& ostream::operator<<(char c) {
printf("%c", c);
return *this;
}

ostream& ostream::operator<<(int n) {
printf("%d", n);
return *this;
}

ostream& ostream::operator<<(const char* lhs) {
printf("%s", lhs);
return *this;
}

ostream& ostream::operator<<(ostream& (*manip)(ostream&)) {
return manip(*this);
}

}; // namespace std
4 changes: 4 additions & 0 deletions src/kernel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "arch.h"
#include "cstdio"
#include "iostream"
#include "kernel.h"
#include "libcxx.h"

Expand All @@ -27,6 +28,9 @@ uint32_t main(uint32_t argc, uint8_t* argv) {
// 架构相关初始化
[[maybe_unused]] auto arch_init_ret = ArchInit(argc, argv);

printf("Hello printf\n");
std::cout << "Hello ostream" << std::endl;

// 进入死循环
while (1) {
;
Expand Down

0 comments on commit 4f17eb2

Please sign in to comment.