Skip to content

Commit

Permalink
[wpiutil] Struct: Add info template parameter pack
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 committed Dec 23, 2023
1 parent aeb1a4a commit 1dd95f2
Show file tree
Hide file tree
Showing 9 changed files with 826 additions and 297 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 @@ -262,17 +264,19 @@ class NetworkTableInstance final {
* @param name topic name
* @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) const;

/**
* Gets a raw struct serialized array topic.
*
* @param name topic name
* @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) const;

/**
* Get Published Topics.
Expand Down Expand Up @@ -818,10 +822,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 @@ -11,6 +11,7 @@
#include "networktables/NetworkTableInstance.h"
#include "networktables/Topic.h"
#include "ntcore_c.h"
#include "wpi/struct/Struct.h"

namespace nt {

Expand Down Expand Up @@ -44,16 +45,18 @@ inline ProtobufTopic<T> NetworkTableInstance::GetProtobufTopic(
return ProtobufTopic<T>{GetTopic(name)};
}

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

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

inline std::vector<Topic> NetworkTableInstance::GetTopics() {
Expand Down Expand Up @@ -272,11 +275,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 1dd95f2

Please sign in to comment.