Compare commits

...

4 Commits

3 changed files with 31 additions and 24 deletions

View File

@ -162,18 +162,10 @@ classes in question.
### Error Handling ### Error Handling
Exceptions are clean and nice in userland code running in environments with The GBA build has exceptions disabled.
expansive system resources, but they are a bit of a pain in small bare metal Instead of throwing exceptions, all engine code must return ```ox::Error```s.
environments. For the sake of consistency, try to stick to ```ox::Error``` in non-engine code
The GBA build has them disabled. as well, but non-engine code is free to use exceptions when they make sense.
Exceptions cause also the compiler to generate a great deal of extra code that
inflates the size of the binary.
The binary size bloat is often cited as one of the main reasons why many
embedded developers prefer C to C++.
Instead of throwing exceptions, all engine code must return Ox error codes.
For the sake of consistency, try to stick to Ox error codes in non-engine code
as well.
Nostalgia and Ox both use ```ox::Error``` to report errors. ```ox::Error``` is Nostalgia and Ox both use ```ox::Error``` to report errors. ```ox::Error``` is
a struct that has overloaded operators to behave like an integer error code, a struct that has overloaded operators to behave like an integer error code,
plus some extra fields to enhance debuggability. plus some extra fields to enhance debuggability.
@ -219,6 +211,22 @@ int caller2() {
std::cout << val << '\n'; std::cout << val << '\n';
return 0; return 0;
} }
ox::Error caller3(int &i) {
return foo(i).moveTo(i);
}
ox::Error caller4(int &i) {
return foo(i).copyTo(i);
}
int caller5(int i) {
return foo(i).unwrap(); // unwrap will kill the program if there is an error
}
int caller6(int i) {
return foo(i).unwrapThrow(); // unwrap will throw if there is an error
}
``` ```
Lastly, there are a few macros available to help in passing ```ox::Error```s Lastly, there are a few macros available to help in passing ```ox::Error```s
@ -275,6 +283,7 @@ ox::Error engineCode() noexcept {
return OxError(0); return OxError(0);
} }
``` ```
Ox also has the ```oxRequire``` macro, which will initialize a value if there is no error, and return if there is. Ox also has the ```oxRequire``` macro, which will initialize a value if there is no error, and return if there is.
It aims to somewhat emulate the ```?``` operator in Rust and Swift. It aims to somewhat emulate the ```?``` operator in Rust and Swift.
@ -309,6 +318,9 @@ ox::Result<int> f2() noexcept {
* ```oxRequireT``` - oxRequire Throw * ```oxRequireT``` - oxRequire Throw
* ```oxRequireMT``` - oxRequire Mutable Throw * ```oxRequireMT``` - oxRequire Mutable Throw
The throw variants of ```oxRequire``` are generally legacy code.
```ox::Result::unwrapThrow``` is generally preferred now.
### Logging and Output ### Logging and Output
Ox provides for logging and debug prints via the ```oxTrace```, ```oxDebug```, and ```oxError``` macros. Ox provides for logging and debug prints via the ```oxTrace```, ```oxDebug```, and ```oxError``` macros.

View File

@ -17,19 +17,14 @@ class BaseClipboardObject {
virtual ~BaseClipboardObject() noexcept = default; virtual ~BaseClipboardObject() noexcept = default;
[[nodiscard]] [[nodiscard]]
virtual ox::String typeId() const noexcept = 0; virtual ox::StringView typeId() const noexcept = 0;
[[nodiscard]]
constexpr auto typeMatch(ox::StringView name, int version) const noexcept {
return typeId() == ox::buildTypeId(name, version);
}
}; };
template<typename T> template<typename T>
class ClipboardObject: public BaseClipboardObject { class ClipboardObject: public BaseClipboardObject {
[[nodiscard]] [[nodiscard]]
ox::String typeId() const noexcept final { ox::StringView typeId() const noexcept final {
return ox::String(ox::ModelTypeId_v<T>); return ox::ModelTypeId_v<T>;
} }
}; };
@ -39,11 +34,11 @@ void setClipboardText(Context &ctx, ox::CRStringView text) noexcept;
void setClipboardObject(Context &ctx, ox::UPtr<BaseClipboardObject> &&obj) noexcept; void setClipboardObject(Context &ctx, ox::UPtr<BaseClipboardObject> &&obj) noexcept;
ox::Result<BaseClipboardObject*> getClipboardData(Context &ctx, ox::StringView typeName, int typeVersion) noexcept; ox::Result<BaseClipboardObject*> getClipboardData(Context &ctx, ox::StringView typeId) noexcept;
template<typename T> template<typename T>
ox::Result<T*> getClipboardObject(Context &ctx) noexcept { ox::Result<T*> getClipboardObject(Context &ctx) noexcept {
oxRequire(p, getClipboardData(ctx, T::TypeName, T::TypeVersion)); oxRequire(p, getClipboardData(ctx, ox::ModelTypeId_v<T>));
return dynamic_cast<T*>(p); return dynamic_cast<T*>(p);
} }

View File

@ -26,8 +26,8 @@ void setClipboardObject(Context &ctx, ox::UPtr<BaseClipboardObject> &&obj) noexc
ctx.clipboard = std::move(obj); ctx.clipboard = std::move(obj);
} }
ox::Result<BaseClipboardObject*> getClipboardData(Context &ctx, ox::StringView typeName, int typeVersion) noexcept { ox::Result<BaseClipboardObject*> getClipboardData(Context &ctx, ox::StringView typeId) noexcept {
if (ctx.clipboard && ctx.clipboard->typeMatch(typeName, typeVersion)) { if (ctx.clipboard && ctx.clipboard->typeId() == typeId) {
return ctx.clipboard.get(); return ctx.clipboard.get();
} }
return OxError(1); return OxError(1);