Skip to content

Commit

Permalink
[wpiutil] Struct: Add info template parameter pack (#6086)
Browse files Browse the repository at this point in the history
This allows using Struct in a dynamically typed environment by passing
additional information to the Struct serialization functions.
  • Loading branch information
PeterJohnson authored Dec 27, 2023
1 parent e07de37 commit 6a2d3c3
Show file tree
Hide file tree
Showing 9 changed files with 996 additions and 317 deletions.
6 changes: 4 additions & 2 deletions ntcore/src/main/native/include/networktables/NetworkTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ class ProtobufTopic;
class RawTopic;
class StringArrayTopic;
class StringTopic;
template <wpi::StructSerializable T>
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
class StructArrayTopic;
template <wpi::StructSerializable T>
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
class StructTopic;
class Topic;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ class ProtobufTopic;
class RawTopic;
class StringArrayTopic;
class StringTopic;
template <wpi::StructSerializable T>
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
class StructArrayTopic;
template <wpi::StructSerializable T>
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
class StructTopic;
class Subscriber;
class Topic;
Expand Down Expand Up @@ -260,19 +262,24 @@ class NetworkTableInstance final {
* Gets a raw struct serialized value topic.
*
* @param name topic name
* @param info optional struct type info
* @return Topic
*/
template <wpi::StructSerializable T>
StructTopic<T> GetStructTopic(std::string_view name) const;
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
StructTopic<T, I...> GetStructTopic(std::string_view name, I... info) const;

/**
* Gets a raw struct serialized array topic.
*
* @param name topic name
* @param info optional struct type info
* @return Topic
*/
template <wpi::StructSerializable T>
StructArrayTopic<T> GetStructArrayTopic(std::string_view name) const;
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
StructArrayTopic<T, I...> GetStructArrayTopic(std::string_view name,
I... info) const;

/**
* Get Published Topics.
Expand Down Expand Up @@ -818,10 +825,12 @@ class NetworkTableInstance final {
* Registers a struct schema. Duplicate calls to this function with the same
* name are silently ignored.
*
* @param T struct serializable type
* @tparam T struct serializable type
* @param info optional struct type info
*/
template <wpi::StructSerializable T>
void AddStructSchema();
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
void AddStructSchema(const I&... info);

/**
* Equality operator. Returns true if both instances refer to the same
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ inline ProtobufTopic<T> NetworkTableInstance::GetProtobufTopic(
return ProtobufTopic<T>{GetTopic(name)};
}

template <wpi::StructSerializable T>
inline StructTopic<T> NetworkTableInstance::GetStructTopic(
std::string_view name) const {
return StructTopic<T>{GetTopic(name)};
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
inline StructTopic<T, I...> NetworkTableInstance::GetStructTopic(
std::string_view name, I... info) const {
return StructTopic<T, I...>{GetTopic(name), std::move(info)...};
}

template <wpi::StructSerializable T>
inline StructArrayTopic<T> NetworkTableInstance::GetStructArrayTopic(
std::string_view name) const {
return StructArrayTopic<T>{GetTopic(name)};
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
inline StructArrayTopic<T, I...> NetworkTableInstance::GetStructArrayTopic(
std::string_view name, I... info) const {
return StructArrayTopic<T, I...>{GetTopic(name), std::move(info)...};
}

inline std::vector<Topic> NetworkTableInstance::GetTopics() {
Expand Down Expand Up @@ -272,11 +274,14 @@ void NetworkTableInstance::AddProtobufSchema(wpi::ProtobufMessage<T>& msg) {
});
}

template <wpi::StructSerializable T>
void NetworkTableInstance::AddStructSchema() {
wpi::ForEachStructSchema<T>([this](auto typeString, auto schema) {
AddSchema(typeString, "structschema", schema);
});
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
void NetworkTableInstance::AddStructSchema(const I&... info) {
wpi::ForEachStructSchema<T>(
[this](auto typeString, auto schema) {
AddSchema(typeString, "structschema", schema);
},
info...);
}

#ifdef __clang__
Expand Down
Loading

0 comments on commit 6a2d3c3

Please sign in to comment.