-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unnecessary files and add CoreContext and BufferStream
Removed multiple unused files as part of code cleanup for unused functionality/modules. Two new files, CoreContext and BufferStream, were added to handle buffer streams and core contexts. A change in DLL interface was done: pointers are now more generally typed with `c_void` instead of `u8`. This increases the safety of the code by making it less
- Loading branch information
Showing
31 changed files
with
319 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "BufferStream.hpp" | ||
|
||
|
||
|
||
|
||
BufferStream::~BufferStream() { | ||
delete[] data; | ||
} | ||
|
||
|
||
template<> | ||
[[maybe_unused]] std::string BufferStream::read<std::string>() { | ||
uint32_t str_len = read<uint32_t>(); | ||
if (pos + str_len > len) { | ||
throw std::runtime_error("BufferStream: out of bounds"); | ||
} | ||
|
||
std::string str(reinterpret_cast<char *>(data + pos), str_len); | ||
pos += str_len; | ||
return str; | ||
} | ||
|
||
std::string BufferStream::read_cstr() { | ||
std::string str; | ||
while (pos < len) { | ||
char c = *reinterpret_cast<char *>(data + pos); | ||
pos += 1; | ||
if (c == '\0') { | ||
break; | ||
} | ||
str.push_back(c); | ||
} | ||
return str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <type_traits> | ||
#include <stdexcept> | ||
#include <vector> | ||
#include <string> | ||
|
||
|
||
|
||
class BufferStream { | ||
private: | ||
uint8_t *data = nullptr; | ||
uint32_t len = 0; | ||
uint32_t pos = 0; | ||
|
||
public: | ||
|
||
~BufferStream(); | ||
|
||
[[nodiscard]] uint32_t get_len() const { | ||
return len; | ||
} | ||
|
||
[[nodiscard]] void *get_data() const { | ||
return data; | ||
} | ||
|
||
template<typename T> | ||
[[maybe_unused]] T read() { | ||
static_assert(std::is_trivially_copyable<T>::value, "T must be trivially copyable"); | ||
if (pos + sizeof(T) > len) { | ||
throw std::runtime_error("BufferStream: out of bounds"); | ||
} | ||
|
||
T val = *reinterpret_cast<T *>(data + pos); | ||
pos += sizeof(T); | ||
return val; | ||
} | ||
|
||
|
||
// partial implementation for vector<T> | ||
template<typename T> | ||
[[maybe_unused]] std::vector<T> read_vec() { | ||
uint32_t vec_len = read<uint32_t>(); | ||
if (pos + vec_len * sizeof(T) > len) { | ||
throw std::runtime_error("BufferStream: out of bounds"); | ||
} | ||
|
||
std::vector<T> vec; | ||
vec.reserve(vec_len); | ||
for (uint32_t i = 0; i < vec_len; i++) { | ||
vec.push_back(read<T>()); | ||
} | ||
return vec; | ||
} | ||
|
||
std::string read_cstr(); | ||
|
||
std::string read_str_buf(); | ||
}; | ||
|
||
|
||
template<> | ||
[[maybe_unused]] std::string BufferStream::read<std::string>(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include "CoreContext.hpp" | ||
|
||
#include "BufferStream.hpp" | ||
|
||
#include <cassert> | ||
|
||
extern "C" { | ||
struct FfiBuf { | ||
uint8_t *data; | ||
uint32_t len; | ||
uint32_t cap; | ||
|
||
}; | ||
|
||
__declspec(dllimport) void *create_context(); | ||
__declspec(dllimport) void destroy_context(void *ctx); | ||
__declspec(dllimport) void destroy_ffi_buf(FfiBuf buf); | ||
__declspec(dllimport) FfiBuf get_funcs(void *ctx); | ||
|
||
|
||
} | ||
|
||
struct Buf { | ||
FfiBuf ffi_buf; | ||
|
||
explicit Buf(FfiBuf ffi_buf) : ffi_buf(ffi_buf) {} | ||
|
||
~Buf() { | ||
destroy_ffi_buf(ffi_buf); | ||
} | ||
|
||
// Buf(const Buf &other) = delete; | ||
// | ||
// Buf &operator=(const Buf &other) = delete; | ||
|
||
[[nodiscard]] uint32_t len() const { | ||
return ffi_buf.len; | ||
} | ||
|
||
[[nodiscard]] uint8_t *data() const { | ||
return ffi_buf.data; | ||
} | ||
|
||
[[nodiscard]] std::string to_string() const { | ||
return std::string(reinterpret_cast<char *>(data()), len()); | ||
} | ||
}; | ||
|
||
struct FfiFunc { | ||
Buf id; | ||
Buf name; | ||
Buf category; | ||
uint32_t behaviour; | ||
bool output; | ||
Buf inputs; | ||
Buf outputs; | ||
Buf events; | ||
}; | ||
|
||
|
||
Ctx::Ctx() { | ||
this->ctx = create_context(); | ||
} | ||
|
||
Ctx::~Ctx() { | ||
destroy_context(this->ctx); | ||
this->ctx = nullptr; | ||
} | ||
|
||
std::vector<Func> Ctx::get_funcs() const { | ||
Buf buf = Buf{::get_funcs(this->ctx)}; | ||
|
||
assert(buf.len() % sizeof(FfiFunc) == 0); // check that the buffer is a multiple of the size of FfiFunc | ||
auto len = buf.len() / sizeof(FfiFunc); | ||
auto funcs = static_cast <FfiFunc *>( static_cast <void *>(buf.data())); | ||
|
||
std::vector<Func> result; | ||
result.reserve(len); | ||
|
||
for (uint32_t i = 0; i < len; i++) { | ||
auto ffi_func = funcs[i]; | ||
Func func{ | ||
ffi_func.id.to_string(), | ||
ffi_func.name.to_string(), | ||
ffi_func.category.to_string(), | ||
ffi_func.behaviour, | ||
ffi_func.output, | ||
{}, | ||
{}, | ||
{} | ||
// BufferStream{ffi_func->inputs.data(), ffi_func->inputs.len()}.read_strings(), | ||
// BufferStream{ffi_func->outputs.data(), ffi_func->outputs.len()}.read_strings(), | ||
// BufferStream{ffi_func->events.data(), ffi_func->events.len()}.read_strings() | ||
}; | ||
result.push_back(func); | ||
} | ||
|
||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <cstdint> | ||
#include <memory> | ||
#include <string> | ||
#include <cstdlib> | ||
|
||
|
||
struct Func { | ||
std::string id; | ||
std::string name; | ||
std::string category; | ||
uint32_t behaviour; | ||
bool output; | ||
std::vector<std::string> inputs; | ||
std::vector<std::string> outputs; | ||
std::vector<std::string> events; | ||
|
||
}; | ||
|
||
|
||
struct Ctx { | ||
void *ctx = nullptr; | ||
|
||
Ctx(); | ||
|
||
~Ctx(); | ||
|
||
Ctx(const Ctx &other) = delete; | ||
|
||
Ctx &operator=(const Ctx &other) = delete; | ||
|
||
|
||
[[nodiscard]] std::vector<Func> get_funcs() const; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.