CARLA
CarlaRecorderHelpers.h
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include <sstream>
10 #include <vector>
11 
12 // get the final path + filename
13 std::string GetRecorderFilename(std::string Filename);
14 
15 // ---------
16 // recorder
17 // ---------
18 
19 // write binary data (using sizeof())
20 template <typename T>
21 void WriteValue(std::ostream &OutFile, const T &InObj)
22 {
23  OutFile.write(reinterpret_cast<const char *>(&InObj), sizeof(T));
24 }
25 
26 template <typename T>
27 void WriteStdVector(std::ostream &OutFile, const std::vector<T> &InVec)
28 {
29  WriteValue<uint32_t>(OutFile, InVec.size());
30  for (const auto& InObj : InVec)
31  {
32  WriteValue<T>(OutFile, InObj);
33  }
34 }
35 
36 template <typename T>
37 void WriteTArray(std::ostream &OutFile, const TArray<T> &InVec)
38 {
39  WriteValue<uint32_t>(OutFile, InVec.Num());
40  for (const auto& InObj : InVec)
41  {
42  WriteValue<T>(OutFile, InObj);
43  }
44 }
45 
46 // write binary data from FVector
47 void WriteFVector(std::ostream &OutFile, const FVector &InObj);
48 
49 // write binary data from FTransform
50 void WriteFTransform(std::ofstream &OutFile, const FTransform &InObj);
51 // write binary data from FString (length + text)
52 void WriteFString(std::ostream &OutFile, const FString &InObj);
53 
54 // ---------
55 // replayer
56 // ---------
57 
58 // read binary data (using sizeof())
59 template <typename T>
60 void ReadValue(std::istream &InFile, T &OutObj)
61 {
62  InFile.read(reinterpret_cast<char *>(&OutObj), sizeof(T));
63 }
64 
65 template <typename T>
66 void ReadStdVector(std::istream &InFile, std::vector<T> &OutVec)
67 {
68  uint32_t VecSize;
69  ReadValue<uint32_t>(InFile, VecSize);
70  OutVec.clear();
71  for (uint32_t i = 0; i < VecSize; ++i)
72  {
73  T InObj;
74  ReadValue<T>(InFile, InObj);
75  OutVec.push_back(InObj);
76  }
77 }
78 
79 template <typename T>
80 void ReadTArray(std::istream &InFile, TArray<T> &OutVec)
81 {
82  uint32_t VecSize;
83  ReadValue<uint32_t>(InFile, VecSize);
84  OutVec.Empty();
85  for (uint32_t i = 0; i < VecSize; ++i)
86  {
87  T InObj;
88  ReadValue<T>(InFile, InObj);
89  OutVec.Add(InObj);
90  }
91 }
92 
93 // read binary data from FVector
94 void ReadFVector(std::istream &InFile, FVector &OutObj);
95 
96 // read binary data from FTransform
97 void ReadTransform(std::ifstream &InFile, FTransform &OutObj);
98 // read binary data from FString (length + text)
99 void ReadFString(std::istream &InFile, FString &OutObj);
void ReadValue(std::istream &InFile, T &OutObj)
void ReadFString(std::istream &InFile, FString &OutObj)
void WriteTArray(std::ostream &OutFile, const TArray< T > &InVec)
void WriteFString(std::ostream &OutFile, const FString &InObj)
void WriteFVector(std::ostream &OutFile, const FVector &InObj)
void WriteStdVector(std::ostream &OutFile, const std::vector< T > &InVec)
void ReadFVector(std::istream &InFile, FVector &OutObj)
std::string GetRecorderFilename(std::string Filename)
void ReadTransform(std::ifstream &InFile, FTransform &OutObj)
void WriteValue(std::ostream &OutFile, const T &InObj)
void WriteFTransform(std::ofstream &OutFile, const FTransform &InObj)
void ReadTArray(std::istream &InFile, TArray< T > &OutVec)
void ReadStdVector(std::istream &InFile, std::vector< T > &OutVec)