Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/basic/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ static const char* mode = "off";

// --------- getters/setters (C function pointers) ----------
// Note: your attribute classes use function pointers: T(*)(void*), not lambdas.
static int32_t getSpeed(void*) { return speed; }
static std::optional<int32_t> getSpeed(void*) { return speed; }
static SlvCtrlParseError setSpeed(void*, int32_t v) { speed = v; return SlvCtrlParseError::Ok; }

static int32_t getMaxSpeed(void*) { return maxSpeed; }
static std::optional<int32_t> getMaxSpeed(void*) { return maxSpeed; }
static SlvCtrlParseError setMaxSpeed(void*, int32_t v) { maxSpeed = v; return SlvCtrlParseError::Ok; }

static int32_t getState(void*) { return state; }
static std::optional<int32_t> getState(void*) { return ready ? std::make_optional(state) : std::nullopt; }
static SlvCtrlParseError setState(void*, int32_t v) { state = v; return SlvCtrlParseError::Ok; }

static bool getReady(void*) { return ready; }
static std::optional<bool> getReady(void*) { return ready; }

static int32_t getBaud(void*) { return baud; }
static std::optional<int32_t> getBaud(void*) { return baud; }

static const char* getMode(void*) { return mode; }
static std::optional<const char*> getMode(void*) { return mode; }
static SlvCtrlParseError setMode(void*, const char* v) { mode = v; return SlvCtrlParseError::Ok; }

// --------- attributes ----------
Expand Down
29 changes: 15 additions & 14 deletions src/SlvCtrlProtocol.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include <optional>
#include <type_traits>
#include <stdint.h>
#include <stddef.h>
#include <type_traits>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
Expand Down Expand Up @@ -79,7 +80,7 @@ struct IAttribute {
template <typename T>
class BaseAttribute : public IAttribute {
public:
using Getter = T (*)(void* ctx);
using Getter = std::optional<T> (*)(void* ctx);
using Setter = SlvCtrlParseError (*)(void* ctx, T value);

BaseAttribute(const char* name, Getter g, Setter s, void* ctx = nullptr) : name_(name), ctx_(ctx), getter_(g), setter_(s) {
Expand All @@ -97,8 +98,8 @@ class BaseAttribute : public IAttribute {
return SlvCtrlAccess::INVALID;
}

T getValue() const {
return getter_ ? getter_(ctx_) : T{};
std::optional<T> getValue() const {
return getter_ ? getter_(ctx_) : std::nullopt;
}

SlvCtrlParseError setValue(T value) {
Expand All @@ -107,7 +108,10 @@ class BaseAttribute : public IAttribute {
}

void writeValue(ISlvCtrlOut& out) const override {
out.write(this->getValue());
auto val = getValue();
if (val.has_value()) {
out.write(val.value());
}
}
Comment thread
heavyrubberslave marked this conversation as resolved.

private:
Expand All @@ -120,7 +124,7 @@ class BaseAttribute : public IAttribute {
class IntAttribute : public BaseAttribute<int32_t> {
public:
IntAttribute(const char* name, Getter getter, Setter setter, void* ctx = nullptr)
: BaseAttribute<int32_t>(name, getter, setter, ctx) {}
: BaseAttribute<int32_t>(name, getter, setter, ctx) {}

SlvCtrlParseError setFromCString(const char* s) override {
if (!s) return SlvCtrlParseError::MissingValue;
Expand All @@ -140,7 +144,7 @@ class IntAttribute : public BaseAttribute<int32_t> {
class FloatAttribute : public BaseAttribute<float> {
public:
FloatAttribute(const char* name, Getter getter, Setter setter, void* ctx = nullptr)
: BaseAttribute<float>(name, getter, setter, ctx) {}
: BaseAttribute<float>(name, getter, setter, ctx) {}

SlvCtrlParseError setFromCString(const char* s) override {
if (!s) return SlvCtrlParseError::MissingValue;
Expand All @@ -159,7 +163,7 @@ class FloatAttribute : public BaseAttribute<float> {
class BoolAttribute : public BaseAttribute<bool> {
public:
BoolAttribute(const char* name, Getter getter, Setter setter, void* ctx = nullptr)
: BaseAttribute<bool>(name, getter, setter, ctx) {}
: BaseAttribute<bool>(name, getter, setter, ctx) {}

SlvCtrlParseError setFromCString(const char* s) override {
if (!s) return SlvCtrlParseError::MissingValue;
Expand All @@ -174,7 +178,9 @@ class BoolAttribute : public BaseAttribute<bool> {
}

void writeValue(ISlvCtrlOut& out) const override {
out.write(this->getValue() ? "true" : "false");
auto val = this->getValue();
if (!val.has_value()) return;
out.write(val.value() ? "true" : "false");
}
};

Expand Down Expand Up @@ -340,11 +346,6 @@ class StrAttribute : public BaseAttribute<const char*> {
out.write(slvCtrlAccessToString(access()));
out.write("[str]");
}

void writeValue(ISlvCtrlOut& out) const override {
const char* s = this->getValue();
out.write(s ? s : "");
}
};

// ---- Protocol ----
Expand Down