@@ -206,6 +206,64 @@ static void FromFloat(VprFloatEntry::Builder* out, const float& in) {
206
206
out->setValue (in);
207
207
}
208
208
209
+ void SimpleDelayModel::read (const std::string& file) {
210
+ // MmapFile object creates an mmap of the specified path, and will munmap
211
+ // when the object leaves scope.
212
+ MmapFile f (file);
213
+
214
+ /* Increase reader limit to 1G words to allow for large files. */
215
+ ::capnp::ReaderOptions opts = default_large_capnp_opts ();
216
+
217
+ // FlatArrayMessageReader is used to read the message from the data array
218
+ // provided by MmapFile.
219
+ ::capnp::FlatArrayMessageReader reader (f.getData (), opts);
220
+
221
+ // When reading capnproto files the Reader object to use is named
222
+ // <schema name>::Reader.
223
+ //
224
+ // Initially this object is an empty VprDeltaDelayModel.
225
+ VprDeltaDelayModel::Reader model;
226
+
227
+ // The reader.getRoot performs a cast from the generic capnproto to fit
228
+ // with the specified schema.
229
+ //
230
+ // Note that capnproto does not validate that the incoming data matches the
231
+ // schema. If this property is required, some form of check would be
232
+ // required.
233
+ model = reader.getRoot <VprDeltaDelayModel>();
234
+
235
+ // ToNdMatrix is a generic function for converting a Matrix capnproto
236
+ // to a vtr::NdMatrix.
237
+ //
238
+ // The use must supply the matrix dimension (2 in this case), the source
239
+ // capnproto type (VprFloatEntry),
240
+ // target C++ type (flat), and a function to convert from the source capnproto
241
+ // type to the target C++ type (ToFloat).
242
+ //
243
+ // The second argument should be of type Matrix<X>::Reader where X is the
244
+ // capnproto element type.
245
+ ToNdMatrix<5 , VprFloatEntry, float >(&delays_, model.getDelays (), ToFloat);
246
+ }
247
+
248
+ void SimpleDelayModel::write (const std::string& file) const {
249
+ // MallocMessageBuilder object is the generate capnproto message builder,
250
+ // using malloc for buffer allocation.
251
+ ::capnp::MallocMessageBuilder builder;
252
+
253
+ // initRoot<X> returns a X::Builder object that can be used to set the
254
+ // fields in the message.
255
+ auto model = builder.initRoot <VprDeltaDelayModel>();
256
+
257
+ // FromNdMatrix is a generic function for converting a vtr::NdMatrix to a
258
+ // Matrix message. It is the mirror function of ToNdMatrix described in
259
+ // read above.
260
+ auto delay_values = model.getDelays ();
261
+ FromNdMatrix<5 , VprFloatEntry, float >(&delay_values, delays_, FromFloat);
262
+
263
+ // writeMessageToFile writes message to the specified file.
264
+ writeMessageToFile (file, &builder);
265
+ }
266
+
209
267
void DeltaDelayModel::read (const std::string& file) {
210
268
// MmapFile object creates an mmap of the specified path, and will munmap
211
269
// when the object leaves scope.
0 commit comments