-
Notifications
You must be signed in to change notification settings - Fork 5
Fields
Fields are the simplest serialized data object. It has a name, and stores a single data type. The valid data types are: bool
, char
, byte
, short
, int
, long long
, float
, double
and std::string
(C++) / string
(C#)
They are a good choice if you only need to store the contents of a single variable.
To use fields, you only need to include Cereal.h
.
Now, type the following in your code:
Cereal::Field* myField = new Cereal::Field("Field name", 'A');
That will create a field with the name "Field name"
and will store the character A
. Now, you can use the function Field::write(Buffer& buffer)
to serialize its contents to a buffer:
myField->write(buffer); // Write the contents of a field to the buffer
Instead, if you need to deserialize the contents of a field, you will have to read a buffer containing a field, and then call Field::getValue<dataType>()
:
Cereal::Field* myField = new Cereal::Field; // We create an empty field because we will overwrite it anyway
myField->read(buffer); // Now the field is full of data
char deserializedData = myField->getValue<char>(); // We store the contents of the field
For more information about how to use fields with objects, check out objects.
You might want to retrieve the field name. For such operation, just call Field::getName()
:
std::string fieldName = myField->getName();
If you want to know how many bytes your field is using, call Field::getSize()
. It returns an unsigned int with the byte count for this particular field.
Finally, if you want to know what kind of data your field is storing, call Field::getDataType()
like so:
DataType type = myField->getDataType();
if(type == DataType::DATA_INT)
{
// Do something if the data contained is an int
// ...
}
#include <Cereal.h> // Include the library
int main()
{
Cereal::Buffer buffer(1024); // Create a buffer with 1024 bytes
Cereal::Field* myField = new Cereal::Field("Field name", 'A');
// Writing a field
myField->write(buffer); // Write the field to the buffer
// Reading a field
Cereal::Field* otherField = new Cereal::Field; // Create another field
buffer.setOffset(0); // Move the buffer back to the beginning
otherField->read(buffer); // Read the field from the buffer
char data = otherField->getValue<char>(); // Now data should be equal to 'A'
// Free dynamic memory
delete myField;
delete otherField;
return 0;
}
To use fields, you only have to add the following code:
Cereal.Field myField = new Cereal.Field("Field name", 'A');
That will create a field with the name "Field name"
and will store the character A
. Now, you can use the function Field::write(Buffer& buffer)
to serialize its contents to a buffer:
myField.write(ref buffer); // Write the contents of a field to the buffer
Instead, if you need to deserialize the contents of a field, you will have to read a buffer containing a field, and then call a different function for each data type. Use the following table to find out the function you have to use:
Data Type | Function | Data Type | Function |
---|---|---|---|
bool | getBool() |
int | getInt32() |
char | getChar() |
float | getFloat() |
byte | getByte() |
Int64 | getInt64() |
short | getShort() |
double | getDouble() |
string | getString() |
Cereal.Field myField = new Cereal.Field(); // We create an empty field because we will overwrite it anyway
myField.read(ref buffer); // Now the field is full of data
char deserializedData = myField.getChar(); // We store the contents of the field
Because C# supports this cool feature, we took advantage of it. We removed some getters and setters and replaced them with properties:
Property | Accessor | C++ getter | C++ setter |
---|---|---|---|
Name | get |
getName() |
- |
Size | get |
getSize() |
- |
DataType | get |
getDataType() |
- |
namespace MyProgram
{
class Program
{
static void Main(string[] args)
{
Cereal.Buffer buffer = new Cereal.Buffer(1024); // Create a buffer with 1024 bytes
Cereal.Field myField = new Cereal.Field("Field name", 'A');
// Writing a field
myField.write(ref buffer); // Write the field to the buffer
// Reading a field
Cereal.Field otherField = new Cereal.Field(); // Create another field
buffer.Position = 0; // Move the buffer back to the beginning
otherField.read(ref buffer); // Read the field from the buffer
char data = otherField.getChar(); // Now data should be equal to 'A'
}
}
}
Home | About Cereal | Setup Guide | Technical Docs | API Reference | Copyright © 2016 - 2019 The Cereal Team