open-dis-cpp
DataStream.h
1 
5 #ifndef _dcl_dis_data_stream_h_
6 #define _dcl_dis_data_stream_h_
7 
8 // the class member, DataStream::BufferType is causing warnign 4251.
9 // disable it until a proper fix is found, as instructed from the enlightening article:
10 // http://www.unknownroad.com/rtfm/VisualStudio/warningC4251.html
11 #if _MSC_VER
12 #pragma warning( push )
13 #pragma warning( disable : 4251 )
14 #endif
15 
16 #include <string> // for typedef, member
17 #include <vector> // for typedef, member
18 #include <dis6/utils/Endian.h> // for enum
19 #include <dis6/opendis6_export.h> // for library symbols
20 #include <cstdlib> // for size_t and NULL definition
21 #include <cstring> // for memcpy
22 
23 namespace DIS
24 {
28  class OPENDIS6_EXPORT DataStream
29  {
30  public:
34  DataStream(Endian stream);
35 
40  DataStream(const char* buffer, size_t length, Endian stream);
41  ~DataStream();
42 
45  const char& operator [](unsigned int offset) const;
46 
48  void SetStream(const char* buffer, size_t length, Endian order);
49 
50  // write operations
51  DataStream& operator <<(bool c);
52  DataStream& operator <<(char c);
53  DataStream& operator <<(unsigned char c);
54  DataStream& operator <<(float c);
55  DataStream& operator <<(double c);
56  DataStream& operator <<(int c);
57  DataStream& operator <<(unsigned int c);
58  DataStream& operator <<(long long c);
59  DataStream& operator <<(unsigned long long c);
60  DataStream& operator <<(unsigned short c);
61  DataStream& operator <<(short c);
62 
63  // read operations
64  DataStream& operator >>(bool& c);
65  DataStream& operator >>(char& c);
66  DataStream& operator >>(unsigned char& c);
67  DataStream& operator >>(float& c);
68  DataStream& operator >>(double& c);
69  DataStream& operator >>(int& c);
70  DataStream& operator >>(unsigned int& c);
71  DataStream& operator >>(long long& c);
72  DataStream& operator >>(unsigned long long& c);
73  DataStream& operator >>(unsigned short& c);
74  DataStream& operator >>(short& c);
75 
76  Endian GetStreamEndian() const;
77  Endian GetMachineEndian() const;
78 
79  size_t GetWritePos() const;
80  size_t GetReadPos() const;
81 
82  size_t size() const;
83 
84  void clear();
85 
86  bool empty() const;
87 
88  private:
89  template<typename T, typename IterT>
90  void IncrementPointer(IterT& iter)
91  {
92  iter += sizeof(T);
93  }
94 
95  template<typename T, typename IterT>
96  void DecrementPointer(IterT& iter)
97  {
98  iter -= sizeof(T);
99  }
100 
103  template<typename T>
104  void WriteAlgorithm(T t)
105  {
106  char* ch = reinterpret_cast<char*>( &t );
107  DoFlip( ch , sizeof(T) );
108  DoWrite( ch , sizeof(T) );
109  IncrementPointer<T>( _write_pos );
110  }
111 
114  template<typename T>
115  void ReadAlgorithm(T& t)
116  {
117  char ch[sizeof(T)];
118  DoRead( ch , sizeof(T) );
119  DoFlip( ch , sizeof(T) );
120  memcpy(&t, ch, sizeof(t));
121  IncrementPointer<T>( _read_pos );
122  }
123 
125  void DoFlip(char* buf, size_t bufsize);
126 
127  void DoWrite(const char* buf, size_t bufsize);
128 
129  void DoRead(char* ch, size_t bufsize);
130 
131 
132  typedef std::vector<char> BufferType;
133  //const BufferType& GetBuffer() const;
134 
135  BufferType _buffer;
136 
138  size_t _read_pos;
139  size_t _write_pos;
140 
142  Endian _stream_endian;
143 
145  Endian _machine_endian;
146  };
147 }
148 
149 #if _MSC_VER
150 #pragma warning( pop )
151 #endif
152 
153 #endif // _dcl_dis_data_stream_h_
Symbolic names as defined in 5.1.4.
Definition: AcknowledgePdu.h:8
a class to support managing a network buffer.
Definition: DataStream.h:28