Panda3D
datagramIterator.h
1 // Filename: datagramIterator.h
2 // Created by: jns (07Feb00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef DATAGRAMITERATOR_H
16 #define DATAGRAMITERATOR_H
17 
18 #include "pandabase.h"
19 
20 #include "datagram.h"
21 #include "numeric_types.h"
22 
23 ////////////////////////////////////////////////////////////////////
24 // Class : DatagramIterator
25 // Description : A class to retrieve the individual data elements
26 // previously stored in a Datagram. Elements may be
27 // retrieved one at a time; it is up to the caller to
28 // know the correct type and order of each element.
29 ////////////////////////////////////////////////////////////////////
30 class EXPCL_PANDAEXPRESS DatagramIterator {
31 public:
32  INLINE void assign(Datagram &datagram, size_t offset = 0);
33 
34 PUBLISHED:
35  INLINE DatagramIterator();
36  INLINE DatagramIterator(const Datagram &datagram, size_t offset = 0);
37  INLINE DatagramIterator(const DatagramIterator &copy);
38  INLINE void operator = (const DatagramIterator &copy);
39  INLINE ~DatagramIterator();
40 
41  INLINE bool get_bool();
42  INLINE PN_int8 get_int8();
43  INLINE PN_uint8 get_uint8();
44 
45  INLINE PN_int16 get_int16();
46  INLINE PN_int32 get_int32();
47  INLINE PN_int64 get_int64();
48  INLINE PN_uint16 get_uint16();
49  INLINE PN_uint32 get_uint32();
50  INLINE PN_uint64 get_uint64();
51  INLINE PN_float32 get_float32();
52  INLINE PN_float64 get_float64();
53  INLINE PN_stdfloat get_stdfloat();
54 
55  INLINE PN_int16 get_be_int16();
56  INLINE PN_int32 get_be_int32();
57  INLINE PN_int64 get_be_int64();
58  INLINE PN_uint16 get_be_uint16();
59  INLINE PN_uint32 get_be_uint32();
60  INLINE PN_uint64 get_be_uint64();
61  INLINE PN_float32 get_be_float32();
62  INLINE PN_float64 get_be_float64();
63 
64  string get_string();
65  string get_string32();
66  string get_z_string();
67  string get_fixed_string(size_t size);
68  wstring get_wstring();
69 
70  INLINE void skip_bytes(size_t size);
71  string extract_bytes(size_t size);
72  size_t extract_bytes(unsigned char *into, size_t size);
73 
74  INLINE string get_remaining_bytes() const;
75  INLINE int get_remaining_size() const;
76 
77  INLINE const Datagram &get_datagram() const;
78  INLINE size_t get_current_index() const;
79 
80  void output(ostream &out) const;
81  void write(ostream &out, unsigned int indent=0) const;
82 
83 private:
84  const Datagram *_datagram;
85  size_t _current_index;
86 
87 public:
88  static TypeHandle get_class_type() {
89  return _type_handle;
90  }
91  static void init_type() {
92  register_type(_type_handle, "DatagramIterator");
93  }
94 
95 private:
96  static TypeHandle _type_handle;
97 };
98 
99 // These generic functions are primarily for reading a value from a
100 // datagram from within a template in which the actual type of the
101 // value is not known. If you do know the type, it's preferable to
102 // use the explicit get_*() method from above instead.
103 
104 INLINE void
105 generic_read_datagram(bool &result, DatagramIterator &source);
106 INLINE void
107 generic_read_datagram(int &result, DatagramIterator &source);
108 INLINE void
109 generic_read_datagram(float &result, DatagramIterator &source);
110 INLINE void
111 generic_read_datagram(double &result, DatagramIterator &source);
112 INLINE void
113 generic_read_datagram(string &result, DatagramIterator &source);
114 INLINE void
115 generic_read_datagram(wstring &result, DatagramIterator &source);
116 
117 #include "datagramIterator.I"
118 
119 #endif
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43