Package edu.wpi.first.util.protobuf
Interface Protobuf<T,MessageType extends us.hebi.quickbuf.ProtoMessage<?>>
- Type Parameters:
T
- object typeMessageType
- protobuf message type
- All Known Implementing Classes:
ArmFeedforwardProto
,ChassisSpeedsProto
,CubicHermiteSplineProto
,DCMotorProto
,DifferentialDriveFeedforwardProto
,DifferentialDriveKinematicsProto
,DifferentialDriveWheelPositionsProto
,DifferentialDriveWheelSpeedsProto
,DifferentialDriveWheelVoltagesProto
,ElevatorFeedforwardProto
,Ellipse2dProto
,LinearSystemProto
,MatrixProto
,MecanumDriveKinematicsProto
,MecanumDriveWheelPositionsProto
,MecanumDriveWheelSpeedsProto
,Pose2dProto
,Pose3dProto
,QuaternionProto
,QuinticHermiteSplineProto
,Rectangle2dProto
,Rotation2dProto
,Rotation3dProto
,SimpleMotorFeedforwardProto
,SwerveDriveKinematicsProto
,SwerveModulePositionProto
,SwerveModuleStateProto
,TrajectoryProto
,TrajectoryStateProto
,Transform2dProto
,Transform3dProto
,Translation2dProto
,Translation3dProto
,Twist2dProto
,Twist3dProto
,VectorProto
public interface Protobuf<T,MessageType extends us.hebi.quickbuf.ProtoMessage<?>>
Interface for Protobuf serialization.
This is designed for serialization of more complex data structures including forward/backwards compatibility and repeated/nested/variable length members, etc. Serialization and deserialization code is auto-generated from .proto interface descriptions (the MessageType generic parameter).
Idiomatically, classes that support protobuf serialization should provide a static final member named "proto" that provides an instance of an implementation of this interface, or a static final method named "getProto" if the class is generic.
-
Method Summary
Modifier and TypeMethodDescriptiondefault T
Creates a (deep) clone of the object.Creates protobuf message.default void
forEachDescriptor
(Predicate<String> exists, BiConsumer<String, byte[]> fn) Loops over all protobuf descriptors including nested/referenced descriptors.us.hebi.quickbuf.Descriptors.Descriptor
Gets the protobuf descriptor.Gets the Class object for the stored value.default String
Gets the type string (e.g.default boolean
Returns whether or not objects are cloneable using the clone() method.default boolean
Returns whether or not objects are immutable.void
pack
(MessageType msg, T value) Copies the object contents into a protobuf message.static void
packArray
(us.hebi.quickbuf.RepeatedDouble msg, double[] arr) Pack a serialized protobuf double array message.static <T,
MessageType extends us.hebi.quickbuf.ProtoMessage<MessageType>>
voidpackArray
(us.hebi.quickbuf.RepeatedMessage<MessageType> msg, T[] arr, Protobuf<T, MessageType> proto) Pack a serialized protobuf array message.unpack
(MessageType msg) Deserializes an object from a protobuf message.static double[]
unpackArray
(us.hebi.quickbuf.RepeatedDouble msg) Unpack a serialized protobuf double array message.static <T,
MessageType extends us.hebi.quickbuf.ProtoMessage<MessageType>>
T[]unpackArray
(us.hebi.quickbuf.RepeatedMessage<MessageType> msg, Protobuf<T, MessageType> proto) Unpack a serialized protobuf array message.default void
unpackInto
(T out, MessageType msg) Updates the object contents from a protobuf message.
-
Method Details
-
getTypeClass
Class<T> getTypeClass()Gets the Class object for the stored value.- Returns:
- Class
-
getTypeString
Gets the type string (e.g. for NetworkTables). This should be globally unique and start with "proto:".- Returns:
- type string
-
getDescriptor
us.hebi.quickbuf.Descriptors.Descriptor getDescriptor()Gets the protobuf descriptor.- Returns:
- descriptor
-
createMessage
Creates protobuf message.- Returns:
- protobuf message
-
unpack
Deserializes an object from a protobuf message.- Parameters:
msg
- protobuf message- Returns:
- New object
-
pack
Copies the object contents into a protobuf message. Implementations should call either msg.setMember(member) or member.copyToProto(msg.getMutableMember()) for each member.- Parameters:
msg
- protobuf messagevalue
- object to serialize
-
unpackInto
Updates the object contents from a protobuf message. Implementations should call msg.getMember(member), MemberClass.makeFromProto(msg.getMember()), or member.updateFromProto(msg.getMember()) for each member.Immutable classes cannot and should not implement this function. The default implementation throws UnsupportedOperationException.
- Parameters:
out
- object to updatemsg
- protobuf message- Throws:
UnsupportedOperationException
- if the object is immutable
-
isImmutable
Returns whether or not objects are immutable. Immutable objects must also be comparable using the equals() method. Default implementation returns false.- Returns:
- True if object is immutable
-
isCloneable
Returns whether or not objects are cloneable using the clone() method. Cloneable objects must also be comparable using the equals() method. Default implementation returns false.- Returns:
- True if object is cloneable
-
clone
Creates a (deep) clone of the object. May also return the object directly if the object is immutable. Default implementation throws CloneNotSupportedException. Typically this should be implemented by implementing clone() on the object itself, and calling it from here.- Parameters:
obj
- object to clone- Returns:
- Clone of object (if immutable, may be same object)
- Throws:
CloneNotSupportedException
- if clone not supported
-
forEachDescriptor
Loops over all protobuf descriptors including nested/referenced descriptors.- Parameters:
exists
- function that returns false if fn should be called for the given type stringfn
- function to call for each descriptor
-
unpackArray
static <T,MessageType extends us.hebi.quickbuf.ProtoMessage<MessageType>> T[] unpackArray(us.hebi.quickbuf.RepeatedMessage<MessageType> msg, Protobuf<T, MessageType> proto) Unpack a serialized protobuf array message.- Type Parameters:
T
- object typeMessageType
- element type of the protobuf array- Parameters:
msg
- protobuf array messageproto
- protobuf implementation- Returns:
- Deserialized array
-
unpackArray
Unpack a serialized protobuf double array message.- Parameters:
msg
- protobuf double array message- Returns:
- Deserialized array
-
packArray
static <T,MessageType extends us.hebi.quickbuf.ProtoMessage<MessageType>> void packArray(us.hebi.quickbuf.RepeatedMessage<MessageType> msg, T[] arr, Protobuf<T, MessageType> proto) Pack a serialized protobuf array message.- Type Parameters:
T
- object typeMessageType
- element type of the protobuf array- Parameters:
msg
- protobuf array messagearr
- array of objectsproto
- protobuf implementation
-
packArray
Pack a serialized protobuf double array message.- Parameters:
msg
- protobuf double array messagearr
- array of objects
-