FlatbuffersHelper.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: FlatbuffersHelper.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Helper functions for Flatbuffers.
6 > Created Time: 2017/05/01
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_FLATBUFFERS_HELPER_H
10 #define CUBBYFLOW_FLATBUFFERS_HELPER_H
11 
12 #include <Core/Size/Size2.h>
13 #include <Core/Size/Size3.h>
14 #include <Core/Vector/Vector2.h>
15 #include <Core/Vector/Vector3.h>
16 
17 #include <Flatbuffers/generated/BasicTypes_generated.h>
18 
19 namespace CubbyFlow
20 {
22  {
23  return fbs::Size2(vec.x, vec.y);
24  }
25 
27  {
28  return fbs::Size3(vec.x, vec.y, vec.z);
29  }
30 
32  {
33  return fbs::Vector2D(vec.x, vec.y);
34  }
35 
37  {
38  return fbs::Vector3D(vec.x, vec.y, vec.z);
39  }
40 
42  {
43  return Size2({ vec.x(), vec.y() });
44  }
45 
47  {
48  return Size3({ vec.x(), vec.y(), vec.z() });
49  }
50 
52  {
53  return Vector2D(vec.x(), vec.y());
54  }
55 
57  {
58  return Vector3D(vec.x(), vec.y(), vec.z());
59  }
60 
61  template <typename GridType, typename FbsFactoryFunc, typename FbsGridType>
62  void SerializeGrid(flatbuffers::FlatBufferBuilder* builder, const std::vector<GridType>& gridList, FbsFactoryFunc func, std::vector<flatbuffers::Offset<FbsGridType>>* fbsGridList)
63  {
64  for (const auto& grid : gridList)
65  {
66  auto type = builder->CreateString(grid->TypeName());
67 
68  std::vector<uint8_t> gridSerialized;
69  grid->Serialize(&gridSerialized);
70 
71  auto fbsGrid = func(*builder, type, builder->CreateVector(gridSerialized.data(), gridSerialized.size()));
72  fbsGridList->push_back(fbsGrid);
73  }
74  }
75 
76  template <typename FbsGridList, typename GridType, typename FactoryFunc>
77  void DeserializeGrid(FbsGridList* fbsGridList, FactoryFunc factoryFunc, std::vector<GridType>* gridList)
78  {
79  for (const auto& grid : (*fbsGridList))
80  {
81  auto type = grid->type()->c_str();
82 
83  std::vector<uint8_t> gridSerialized(grid->data()->begin(), grid->data()->end());
84 
85  auto newGrid = factoryFunc(type);
86  newGrid->Deserialize(gridSerialized);
87 
88  gridList->push_back(newGrid);
89  }
90  }
91 }
92 
93 #endif
3-D vector class.
Definition: Vector3.h:26
T x
X (or the first) component of the vector.
Definition: Vector2.h:29
T z
Z (or the third) component of the vector.
Definition: Vector3.h:38
void DeserializeGrid(FbsGridList *fbsGridList, FactoryFunc factoryFunc, std::vector< GridType > *gridList)
Definition: FlatbuffersHelper.h:77
T x
X (or the first) component of the point.
Definition: Point2.h:28
fbs::Size2 CubbyFlowToFlatbuffers(const Size2 &vec)
Definition: FlatbuffersHelper.h:21
2-D point class.
Definition: Point2.h:25
3-D point class.
Definition: Point3.h:26
void SerializeGrid(flatbuffers::FlatBufferBuilder *builder, const std::vector< GridType > &gridList, FbsFactoryFunc func, std::vector< flatbuffers::Offset< FbsGridType >> *fbsGridList)
Definition: FlatbuffersHelper.h:62
Point2< size_t > Size2
Definition: Size2.h:16
T y
Y (or the second) component of the point.
Definition: Point2.h:34
T x
X (or the first) component of the vector.
Definition: Vector3.h:29
Definition: pybind11Utils.h:24
Vector2< double > Vector2D
Double-type 2D vector.
Definition: Vector2.h:341
T z
Z (or the third) component of the point.
Definition: Point3.h:38
T y
Y (or the second) component of the point.
Definition: Point3.h:35
T y
Y (or the second) component of the vector.
Definition: Vector2.h:35
T x
X (or the first) component of the point.
Definition: Point3.h:29
2-D vector class.
Definition: Vector2.h:26
Size2 FlatbuffersToCubbyFlow(const fbs::Size2 &vec)
Definition: FlatbuffersHelper.h:41
T y
Y (or the second) component of the vector.
Definition: Vector3.h:35
Point3< size_t > Size3
Definition: Size3.h:16
Vector3< double > Vector3D
Double-type 3D vector.
Definition: Vector3.h:353