Support operator*/-> in new flatbuffers API
This was suggested during the initial reviews, and does seem to reduce a
fair amount of overhead.
Change-Id: Icb22106612c3a8430afa0b9dbe0a85f835e5b821
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/events/event_loop.h b/aos/events/event_loop.h
index d0e3f2a..1ff0fb7 100644
--- a/aos/events/event_loop.h
+++ b/aos/events/event_loop.h
@@ -375,6 +375,8 @@
}
T *get() { return builder()->get(); }
+ T &operator*() { return *get(); }
+ T *operator->() { return get(); }
RawSender::Error Send() {
const auto err = sender_->Send(builder_.value().buffer().size());
diff --git a/aos/events/event_loop_param_test.cc b/aos/events/event_loop_param_test.cc
index bfce034..5ec494f 100644
--- a/aos/events/event_loop_param_test.cc
+++ b/aos/events/event_loop_param_test.cc
@@ -119,7 +119,7 @@
aos::Sender<TestMessageStatic>::StaticBuilder msg =
sender.MakeStaticBuilder();
- msg.get()->set_value(200);
+ msg->set_value(200);
CHECK(msg.builder()->Verify());
msg.CheckOk(msg.Send());
});
diff --git a/aos/events/ping_lib.cc b/aos/events/ping_lib.cc
index 23de0db..cd36378 100644
--- a/aos/events/ping_lib.cc
+++ b/aos/events/ping_lib.cc
@@ -40,9 +40,9 @@
++count_;
aos::Sender<examples::PingStatic>::StaticBuilder builder =
sender_.MakeStaticBuilder();
- examples::PingStatic *ping = builder.get();
- ping->set_value(count_);
- ping->set_send_time(event_loop_->monotonic_now().time_since_epoch().count());
+ builder->set_value(count_);
+ builder->set_send_time(
+ event_loop_->monotonic_now().time_since_epoch().count());
builder.CheckOk(builder.Send());
VLOG(2) << "Sending ping";
}
diff --git a/aos/events/pong_lib.cc b/aos/events/pong_lib.cc
index 7fb07c2..8fc3f62 100644
--- a/aos/events/pong_lib.cc
+++ b/aos/events/pong_lib.cc
@@ -42,9 +42,8 @@
last_send_time_ = ping.send_time();
aos::Sender<examples::PongStatic>::StaticBuilder builder =
sender_.MakeStaticBuilder();
- examples::PongStatic *pong = builder.get();
- pong->set_value(ping.value());
- pong->set_initial_send_time(ping.send_time());
+ builder->set_value(ping.value());
+ builder->set_initial_send_time(ping.send_time());
builder.CheckOk(builder.Send());
}
diff --git a/aos/flatbuffers/builder.h b/aos/flatbuffers/builder.h
index 6999649..75e0fe2 100644
--- a/aos/flatbuffers/builder.h
+++ b/aos/flatbuffers/builder.h
@@ -84,6 +84,8 @@
// Returns the actual object for you to operate on and construct the
// flatbuffer. Unlike AsFlatbufferSpan(), this will be stable.
T *get() { return &flatbuffer_.t; }
+ T &operator*() { return *get(); }
+ T *operator->() { return get(); }
private:
size_t Alignment() const override { return flatbuffer_.t.Alignment(); }
diff --git a/documentation/aos/docs/flatbuffers.md b/documentation/aos/docs/flatbuffers.md
index 83a7f92..132348d 100644
--- a/documentation/aos/docs/flatbuffers.md
+++ b/documentation/aos/docs/flatbuffers.md
@@ -534,7 +534,7 @@
loop->OnRun([&]() {
aos::Sender<TestMessageStatic>::StaticBuilder msg =
sender.MakeStaticBuilder();
- msg.get()->set_value(200);
+ msg->set_value(200);
msg.CheckOk(msg.Send());
});
```
@@ -545,5 +545,3 @@
* A `add_or_get_subtable` generated method that avoids the need for the user to
check `has_subtable()` before calling `add_subtable()`.
-* `operator->()` in places to reduce syntactic overhead.
-* Make naming of `StaticVector` methods more consistent with `std::vector`.