add a Buffer and WASI build support
This commit is contained in:
parent
95df0a0321
commit
3dcb5197cb
8 changed files with 110 additions and 11 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,2 +1,2 @@
|
|||
.cache
|
||||
/build
|
||||
/build*
|
||||
|
|
8
.vscode/launch.json
vendored
8
.vscode/launch.json
vendored
|
@ -11,6 +11,14 @@
|
|||
"program": "${command:cmake.launchTargetPath}",
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}",
|
||||
},
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug (WASM)",
|
||||
"program": "wasmtime",
|
||||
"args": ["run", "-g", "${command:cmake.launchTargetPath}"],
|
||||
"cwd": "${workspaceFolder}",
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
option(BUILD_WITH_MODULES "Build with C++ Modules" ON)
|
||||
option(BUILD_WITH_EXCEPTIONS "Build with C++ Exceptions" ON)
|
||||
|
||||
if(BUILD_WITH_MODULES)
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
||||
|
@ -23,7 +24,10 @@ if(BUILD_WITH_MODULES)
|
|||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
endif()
|
||||
|
||||
if(NOT BUILD_WITH_EXCEPTIONS)
|
||||
add_compile_options(-fno-exceptions)
|
||||
endif()
|
||||
|
||||
set(CMAKE_EXECUTABLE_SUFFIX ${EXECUTABLE_SUFFIX})
|
||||
|
||||
add_subdirectory(src)
|
||||
|
||||
|
|
14
arch-webassembly-toolchain.cmake
Normal file
14
arch-webassembly-toolchain.cmake
Normal file
|
@ -0,0 +1,14 @@
|
|||
set(CMAKE_SYSTEM_NAME Generic)
|
||||
set(CMAKE_SYSTEM_VERSION 1)
|
||||
set(CMAKE_SYSTEM_PROCESSOR wasm32)
|
||||
set(triple wasm32-wasi)
|
||||
set(EXECUTABLE_SUFFIX .wasm)
|
||||
|
||||
set(CMAKE_C_COMPILER clang)
|
||||
set(CMAKE_CXX_COMPILER clang++)
|
||||
set(CMAKE_C_COMPILER_TARGET ${triple})
|
||||
set(CMAKE_CXX_COMPILER_TARGET ${triple})
|
||||
|
||||
set(CMAKE_SYSROOT /usr/share/wasi-sysroot)
|
||||
set(BUILD_WITH_EXCEPTIONS false)
|
||||
add_compile_options(-fexperimental-library)
|
|
@ -11,9 +11,12 @@ add_executable(inheritance inheritance.cc)
|
|||
add_executable(enumerations enumerations.cc)
|
||||
add_executable(namespaces namespaces.cc)
|
||||
add_executable(FunctionArguments FunctionArguments.cc)
|
||||
add_executable(exceptions exceptions.cc)
|
||||
if (BUILD_WITH_EXCEPTIONS)
|
||||
add_executable(exceptions exceptions.cc)
|
||||
endif()
|
||||
add_executable(HeapMemory HeapMemory.cc)
|
||||
add_executable(AbstractClasses AbstractClasses.cc)
|
||||
add_executable(vector vector.cc)
|
||||
add_executable(Vector2 Vector2.cc)
|
||||
add_executable(TemplateVector TemplateVector.cc)
|
||||
add_executable(buffer buffer.cc)
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include <format>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
template <typename T>
|
||||
class Vector {
|
||||
private:
|
||||
|
@ -22,11 +23,11 @@ public:
|
|||
T& operator[](int i)
|
||||
{
|
||||
if (i < 0 || size() <= i) {
|
||||
#if __cpp_exceptions == 199711
|
||||
#if __cpp_exceptions == 199711
|
||||
throw std::out_of_range { "Vector::operator[]" };
|
||||
#else
|
||||
#else
|
||||
std::exit(1);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
return elem[i];
|
||||
|
|
60
src/buffer.cc
Normal file
60
src/buffer.cc
Normal file
|
@ -0,0 +1,60 @@
|
|||
#include <format>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
template <typename T, int S>
|
||||
class Buffer {
|
||||
public:
|
||||
constexpr int size()
|
||||
{
|
||||
return S;
|
||||
}
|
||||
|
||||
T& operator[](int i)
|
||||
{
|
||||
if (i < 0 || size() <= i) {
|
||||
#if __cpp_exceptions == 199711
|
||||
throw std::out_of_range { "Vector::operator[]" };
|
||||
#else
|
||||
std::exit(1);
|
||||
#endif
|
||||
}
|
||||
return elem[i];
|
||||
}
|
||||
|
||||
T* begin()
|
||||
{
|
||||
return &elem[0];
|
||||
}
|
||||
|
||||
T* end()
|
||||
{
|
||||
return &elem[0] + size();
|
||||
}
|
||||
|
||||
private:
|
||||
T elem[S];
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Buffer<double, 5> doubleBuffer;
|
||||
Buffer<std::string, 5> stringBuffer;
|
||||
|
||||
for (int i = 0; i < doubleBuffer.size(); i++) {
|
||||
doubleBuffer[i] = i;
|
||||
}
|
||||
|
||||
for (int i = 0; i < stringBuffer.size(); i++) {
|
||||
stringBuffer[i] = std::format("this is element {}", i);
|
||||
}
|
||||
|
||||
for (double i : doubleBuffer) {
|
||||
std::cout << i << std::endl;
|
||||
}
|
||||
|
||||
for (std::string i : stringBuffer) {
|
||||
std::cout << i << std::endl;
|
||||
}
|
||||
}
|
|
@ -22,14 +22,15 @@ public:
|
|||
: elem(new double[a.sz])
|
||||
, sz { a.sz }
|
||||
{
|
||||
for (int i = 0; i!=sz;++i) {
|
||||
for (int i = 0; i != sz; ++i) {
|
||||
elem[i] = a.elem[i];
|
||||
}
|
||||
}
|
||||
|
||||
Vector& operator=(const Vector& a) {
|
||||
Vector& operator=(const Vector& a)
|
||||
{
|
||||
double* p = new double[a.sz];
|
||||
for (int i =0; i!=sz; ++i) {
|
||||
for (int i = 0; i != sz; ++i) {
|
||||
p[i] = a.elem[i];
|
||||
}
|
||||
delete[] elem;
|
||||
|
@ -40,16 +41,24 @@ public:
|
|||
|
||||
double& operator[](int i)
|
||||
{
|
||||
if (i < 0 || i > size() - 1) {
|
||||
if (i < 0 || size() <= i) {
|
||||
#if __cpp_exceptions == 199711
|
||||
throw std::out_of_range { "Vector::operator[]" };
|
||||
#else
|
||||
std::exit(1);
|
||||
#endif
|
||||
}
|
||||
return elem[i];
|
||||
}
|
||||
|
||||
void print_element(int i)
|
||||
{
|
||||
if (i < 0 || i > size() - 1) {
|
||||
if (i < 0 || size() <= i) {
|
||||
#if __cpp_exceptions == 199711
|
||||
throw std::out_of_range { "Vector::print_element(int i)" };
|
||||
#else
|
||||
std::exit(1);
|
||||
#endif
|
||||
}
|
||||
std::cout << std::format("element[{}] : {}", i, elem[i]) << std::endl;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue