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
,DCMotorProto
,DifferentialDriveKinematicsProto
,DifferentialDriveWheelPositionsProto
,DifferentialDriveWheelSpeedsProto
,DifferentialDriveWheelVoltagesProto
,ElevatorFeedforwardProto
,Ellipse2dProto
,MecanumDriveKinematicsProto
,MecanumDriveWheelPositionsProto
,MecanumDriveWheelSpeedsProto
,Pose2dProto
,Pose3dProto
,QuaternionProto
,Rectangle2dProto
,Rotation2dProto
,Rotation3dProto
,SwerveModulePositionProto
,SwerveModuleStateProto
,TrajectoryProto
,TrajectoryStateProto
,Transform2dProto
,Transform3dProto
,Translation2dProto
,Translation3dProto
,Twist2dProto
,Twist3dProto
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.
-
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.default Protobuf<?,
?>[] Gets the list of protobuf types referenced by this protobuf.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.unpack
(MessageType msg) Deserializes an object from a protobuf 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
-
getNested
Gets the list of protobuf types referenced by this protobuf.- Returns:
- list of protobuf types
-
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. Clonable objects must also be comparable using the equals() method. Default implementation returns false.- Returns:
- True if object is clonable
-
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
-