Cafu Engine
_aux.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_SCENEGRAPH_AUX_HPP_INCLUDED
8 #define CAFU_SCENEGRAPH_AUX_HPP_INCLUDED
9 
10 #include "Math3D/Vector3.hpp"
11 #include "Templates/Array.hpp"
12 
13 #if defined(_WIN32) && _MSC_VER<1600
14 #include "pstdint.h" // Paul Hsieh's portable implementation of the stdint.h header.
15 #else
16 #include <stdint.h>
17 #endif
18 
19 #include <fstream>
20 #include <map>
21 #include <string>
22 
23 
24 namespace cf
25 {
26  namespace SceneGraph
27  {
28  namespace aux
29  {
30  /// Reads an "int32_t" from a std::istream.
31  int32_t ReadInt32(std::istream& InFile);
32 
33  /// Reads an "uint16_t" from a std::istream.
34  uint16_t ReadUInt16(std::istream& InFile);
35 
36  /// Reads an "uint32_t" from a std::istream.
37  uint32_t ReadUInt32(std::istream& InFile);
38 
39  /// Reads a "float" from a std::istream.
40  float ReadFloat(std::istream& InFile);
41 
42  /// Reads a "double" from a std::istream.
43  double ReadDouble(std::istream& InFile);
44 
45  /// Reads a std::string from a std::istream.
46  std::string ReadString(std::istream& InFile);
47 
48  /// Reads a Vector3dT from a std::istream.
49  Vector3dT ReadVector3d(std::istream& InFile);
50 
51  /// Reads a Vector3fT from a std::istream.
52  Vector3fT ReadVector3f(std::istream& InFile);
53 
54 
55  /// Writes an "int32_t" into a std::ostream.
56  void Write(std::ostream& OutFile, int32_t i);
57 
58  /// Writes an "uint16_t" into a std::ostream.
59  void Write(std::ostream& OutFile, uint16_t ui);
60 
61  /// Writes an "uint32_t" into a std::ostream.
62  void Write(std::ostream& OutFile, uint32_t ui);
63 
64  /// Writes a "float" into a std::ostream.
65  void Write(std::ostream& OutFile, float f);
66 
67  /// Writes a "double" into a std::ostream.
68  void Write(std::ostream& OutFile, double d);
69 
70  /// Writes a std::string into a std::ostream.
71  void Write(std::ostream& OutFile, const std::string& Str);
72 
73  /// Writes a Vector3dT into a std::ostream.
74  void Write(std::ostream& OutFile, const Vector3T<double>& v);
75 
76  /// Writes a Vector3fT into a std::ostream.
77  void Write(std::ostream& OutFile, const Vector3T<float>& v);
78 
79 
80  /// This function casts the given integer i to an int32_t, and checks that the returned int32_t has the same value as i.
81  /// If the check fails (the value of i overflows the int32_t), the function triggers an assertion in debug builds and
82  /// throws an std::overflow_error in all builds.
83  /// The function is intended for use in cross-platform (ILP32, LLP64, LP64) serialization code where sizeof(i) can be 4 or 8,
84  /// and i should be written as a 4-byte value in order to be readable on all platforms.
85  template<class T> int32_t cnc_i32(T i)
86  {
87  assert(sizeof(i)>=4); // If this ever fails, it is more a curiosity than a real issue.
88 
89  const int32_t i32=static_cast<int32_t>(i);
90 
91  assert(i32==i);
92  if (i32!=i) throw std::overflow_error("The cast in aux::cnc_i32() caused a loss of data.");
93 
94  return i32;
95  }
96 
97  /// Just like cnc_i32(), but for unsigned integers that are cast to uint32_t.
98  template<class T> uint32_t cnc_ui32(T ui)
99  {
100  assert(sizeof(ui)>=4); // If this ever fails, it is more a curiosity than a real issue.
101 
102  const uint32_t ui32=static_cast<uint32_t>(ui);
103 
104  assert(ui32==ui);
105  if (ui32!=ui) throw std::overflow_error("The cast in aux::cnc_ui32() caused a loss of data.");
106 
107  return ui32;
108  }
109 
110 
111  class PoolT
112  {
113  public:
114 
116  {
117  // See "Die C++ Programmiersprache" by Bjarne Stroustrup pages 498 and 510 and
118  // Scott Meyers "Effective STL" Item 21 for more information about this struct.
119  bool operator () (const Vector3dT& v1, const Vector3dT& v2) const
120  {
121  if (v1.x < v2.x) return true;
122  if (v1.x > v2.x) return false;
123 
124  if (v1.y < v2.y) return true;
125  if (v1.y > v2.y) return false;
126 
127  return v1.z < v2.z;
128  }
129  };
130 
132  {
133  // See "Die C++ Programmiersprache" by Bjarne Stroustrup pages 498 and 510 and
134  // Scott Meyers "Effective STL" Item 21 for more information about this struct.
135  bool operator () (const Vector3fT& v1, const Vector3fT& v2) const
136  {
137  if (v1.x < v2.x) return true;
138  if (v1.x > v2.x) return false;
139 
140  if (v1.y < v2.y) return true;
141  if (v1.y > v2.y) return false;
142 
143  return v1.z < v2.z;
144  }
145  };
146 
147  std::string ReadString(std::istream& InFile);
148  Vector3T<double> ReadVector3d(std::istream& InFile);
149  Vector3T<float> ReadVector3f(std::istream& InFile);
150 
151  void Write(std::ostream& OutFile, const std::string& s);
152  void Write(std::ostream& OutFile, const Vector3T<double>& v);
153  void Write(std::ostream& OutFile, const Vector3T<float>& v);
154 
155 
156  private:
157 
158  // Helper containers when the PoolT is used for reading.
159  ArrayT< std::string > ReadStrings;
160  ArrayT< Vector3T<double> > ReadVectors3d;
161  ArrayT< Vector3T<float> > ReadVectors3f;
162 
163  // Helper containers when the PoolT is used for writing.
164  std::map<std::string, uint32_t > WriteStrings;
165  std::map<Vector3dT, uint32_t, LessVector3d> WriteVectors3d;
166  std::map<Vector3fT, uint32_t, LessVector3f> WriteVectors3f;
167  };
168  }
169  }
170 }
171 
172 #endif
Definition: _aux.hpp:111
T y
The y-component of this vector.
Definition: Vector3.hpp:41
This class represents a polymorphic 3-dimensional vector.
Definition: Misc.hpp:11
T z
The z-component of this vector.
Definition: Vector3.hpp:42
T x
The x-component of this vector.
Definition: Vector3.hpp:40