MakerLabs ACM
verified_flatbuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright Paul Reimer, 2018
3  *
4  * This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 Unported License.
5  * To view a copy of this license, visit
6  * https://creativecommons.org/licenses/by-nc-sa/4.0/
7  * or send a letter to
8  * Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
9  */
10 
11 #pragma once
12 
13 #include <memory>
14 
15 #include "flatbuffers/flatbuffers.h"
16 
17 template <typename T>
19 {
20  explicit VerifiedFlatbuffer(const flatbuffers::DetachedBuffer&& _buf)
21  : buf(std::move(_buf))
22  {
23  auto buf_ref = flatbuffers::BufferRef<T>{buf.data(), buf.size()};
24  verified = buf_ref.Verify();
25  }
26 
27  ~VerifiedFlatbuffer() = default;
28 
29  const T* GetRoot() const
30  {
31  return flatbuffers::GetRoot<T>(verified? buf.data() : nullptr);
32  }
33 
34  bool verified = false;
35  const flatbuffers::DetachedBuffer buf;
36 };
Definition: verified_flatbuffer.h:18