Skip to content

Commit

Permalink
more extensive type documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed May 31, 2024
1 parent fa09681 commit ed631cd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
6 changes: 1 addition & 5 deletions crates/cpp/helper-types/wit-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace wit {
#if __cplusplus > 202001L
using std::span;
#else
// minimal implementation to get things going
/// Minimal span (vector view) implementation for older C++ environments
template <class T> class span {
T const *address;
size_t length;
Expand All @@ -32,8 +32,4 @@ template <class T> class span {
span(std::vector<U> const&vec) : address(vec.data()), length(vec.size()) {}
};
#endif

template <typename T> struct Owned {
T *ptr;
};
} // namespace wit
15 changes: 15 additions & 0 deletions crates/cpp/helper-types/wit-guest.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "wit-common.h"

namespace wit {
/// A string in linear memory, freed unconditionally using free
///
/// A normal C++ string makes no guarantees about where the characters
/// are stored and how this is freed.
class string {
uint8_t const *data_;
size_t length;
Expand Down Expand Up @@ -43,6 +47,10 @@ class string {
}
};

/// A vector in linear memory, freed unconditionally using free
///
/// You can't detach the data memory from a vector, nor create one
/// in a portable way from a buffer and lenght without copying.
template <class T>
class vector {
T *data_;
Expand Down Expand Up @@ -81,6 +89,10 @@ class vector {
}
};

/// @brief A Resource defined within the guest (guest side)
///
/// It registers with the host and should remain in a static location.
/// Typically referenced by the Owned type
template <class R> class ResourceExportBase {
public:
struct Deleter {
Expand All @@ -102,6 +114,9 @@ template <class R> class ResourceExportBase {
int32_t into_handle() { int32_t result = handle; handle=invalid; return result; }
};

/// @brief A Resource imported from the host (guest side)
///
/// Wraps the identifier and can be forwarded but not duplicated
class ResourceImportBase {
public:
static const int32_t invalid = -1;
Expand Down
8 changes: 7 additions & 1 deletion crates/cpp/helper-types/wit-host.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ typedef guest_address (*guest_alloc_t)(WASMExecEnv *, guest_size size,
guest_size align);
#endif

/// A string in linear memory (host-side handle)
// host code never de-allocates directly
class string {
guest_address data_;
Expand Down Expand Up @@ -102,6 +103,7 @@ class string {
#endif
};

/// A vector in linear memory (host-side handle)
template <class T>
class vector {
guest_address data_;
Expand All @@ -124,6 +126,7 @@ class vector {
guest_size size() const { return length; }
};

/// Wrapper for specialized de-allocation of a returned type (calling cabi_post_*)
template <class T> class guest_owned : public T {
guest_address data_;
#ifdef WIT_HOST_WAMR
Expand Down Expand Up @@ -183,6 +186,8 @@ template <class T> class guest_owned : public T {
#endif
};

/// @brief Helper class to map between IDs and resources
/// @tparam R Type of the Resource
template <class R> class ResourceTable {
static std::map<int32_t, R> resources;

Expand All @@ -208,7 +213,7 @@ template <class R> class ResourceTable {
}
};

// guest exported resource
/// Guest exported resource (host side handle)
class ResourceExportBase : public ResourceTable<guest_address> {
protected:
guest_address rep;
Expand All @@ -230,6 +235,7 @@ class ResourceExportBase : public ResourceTable<guest_address> {
guest_address take_rep() { guest_address res = rep; rep=0; return res; }
};

/// Host defined resource (host side definition)
template <class R>
class ResourceImportBase : public ResourceTable<R*> {
int32_t index;
Expand Down

0 comments on commit ed631cd

Please sign in to comment.