SUMO - Simulation of Urban MObility
storage.h
Go to the documentation of this file.
1 /************************************************************************
2  ** This file is part of the network simulator Shawn. **
3  ** Copyright (C) 2004-2007 by the SwarmNet (www.swarmnet.de) project **
4  ** Shawn is free software; you can redistribute it and/or modify it **
5  ** under the terms of the BSD License. Refer to the shawn-licence.txt **
6  ** file in the root of the Shawn source tree for further details. **
7  ************************************************************************
8  ** **
9  ** \author Axel Wegener <wegener@itm.uni-luebeck.de> **
10  ** \author Bjoern Hendriks <hendriks@ibr.cs.tu-bs.de> **
11  ** **
12  ************************************************************************/
13 #ifndef __SHAWN_APPS_TCPIP_STORAGE_H
14 #define __SHAWN_APPS_TCPIP_STORAGE_H
15 
16 #ifdef SHAWN
17  #include <shawn_config.h>
18  #include "_apps_enable_cmake.h"
19  #ifdef ENABLE_TCPIP
20  #define BUILD_TCPIP
21  #endif
22 #else
23  #define BUILD_TCPIP
24 #endif
25 
26 
27 #ifdef BUILD_TCPIP
28 
29 #include <vector>
30 #include <string>
31 #include <stdexcept>
32 #include <iostream>
33 
34 namespace tcpip
35 {
36 
37 class Storage
38 {
39 
40 public:
41  typedef std::vector<unsigned char> StorageType;
42 
43 private:
44  StorageType store;
45  StorageType::const_iterator iter_;
46 
47  // sortation of bytes forwards or backwards?
48  bool bigEndian_;
49 
51  void init();
52 
54  void checkReadSafe(unsigned int num) const throw(std::invalid_argument);
56  unsigned char readCharUnsafe();
58  void writeByEndianess(const unsigned char * begin, unsigned int size);
60  void readByEndianess(unsigned char * array, int size);
61 
62 
63 public:
64 
66  Storage();
67 
69  Storage(const unsigned char[], int length=-1);
70 
71  // Destructor
72  virtual ~Storage();
73 
74  virtual bool valid_pos();
75  virtual unsigned int position() const;
76 
77  void reset();
79  std::string hexDump() const;
80 
81  virtual unsigned char readChar() throw(std::invalid_argument);
82  virtual void writeChar(unsigned char) throw();
83 
84  virtual int readByte() throw(std::invalid_argument);
85  virtual void writeByte(int) throw(std::invalid_argument);
86 // virtual void writeByte(unsigned char) throw();
87 
88  virtual int readUnsignedByte() throw(std::invalid_argument);
89  virtual void writeUnsignedByte(int) throw(std::invalid_argument);
90 
91  virtual std::string readString() throw(std::invalid_argument);
92  virtual void writeString(const std::string& s) throw();
93 
94  virtual std::vector<std::string> readStringList() throw(std::invalid_argument);
95  virtual void writeStringList(const std::vector<std::string> &s) throw();
96 
97  virtual int readShort() throw(std::invalid_argument);
98  virtual void writeShort(int) throw(std::invalid_argument);
99 
100  virtual int readInt() throw(std::invalid_argument);
101  virtual void writeInt(int) throw();
102 
103  virtual float readFloat() throw(std::invalid_argument);
104  virtual void writeFloat( float ) throw();
105 
106  virtual double readDouble() throw(std::invalid_argument);
107  virtual void writeDouble( double ) throw();
108 
109  virtual void writePacket(unsigned char* packet, int length);
110  virtual void writePacket(const std::vector<unsigned char> &packet);
111 
112  virtual void writeStorage(tcpip::Storage& store);
113 
114  // Some enabled functions of the underlying std::list
115  StorageType::size_type size() const { return store.size(); }
116 
117  StorageType::const_iterator begin() const { return store.begin(); }
118  StorageType::const_iterator end() const { return store.end(); }
119 
120 };
121 
122 } // namespace tcpip
123 
124 #endif // BUILD_TCPIP
125 
126 #endif
127 /*-----------------------------------------------------------------------
128  * Source $Source: $
129  * Version $Revision: 620 $
130  * Date $Date: 2011-07-08 17:39:10 +0200 (Fri, 08 Jul 2011) $
131  *-----------------------------------------------------------------------
132  * $Log: $
133  *-----------------------------------------------------------------------*/
virtual ~Storage()
Definition: socket.cpp:62
virtual int readShort()
void init()
Used in constructors to initialize local variables.
virtual std::vector< std::string > readStringList()
virtual unsigned int position() const
virtual double readDouble()
StorageType::size_type size() const
Definition: storage.h:115
virtual bool valid_pos()
virtual void writePacket(unsigned char *packet, int length)
void checkReadSafe(unsigned int num) const
Check if the next num bytes can be read safely.
virtual void writeUnsignedByte(int)
virtual unsigned char readChar()
StorageType store
Definition: storage.h:44
std::vector< unsigned char > StorageType
Definition: storage.h:41
bool bigEndian_
Definition: storage.h:48
virtual void writeInt(int)
void writeByEndianess(const unsigned char *begin, unsigned int size)
Write size elements of array begin according to endianess.
virtual int readUnsignedByte()
virtual void writeChar(unsigned char)
Storage()
Standard Constructor.
void readByEndianess(unsigned char *array, int size)
Read size elements into array according to endianess.
StorageType::const_iterator end() const
Definition: storage.h:118
virtual int readInt()
virtual void writeByte(int)
virtual void writeStringList(const std::vector< std::string > &s)
virtual void writeShort(int)
virtual std::string readString()
virtual void writeStorage(tcpip::Storage &store)
virtual void writeFloat(float)
virtual void writeString(const std::string &s)
StorageType::const_iterator iter_
Definition: storage.h:45
unsigned char readCharUnsafe()
Read a byte without validity check.
virtual float readFloat()
StorageType::const_iterator begin() const
Definition: storage.h:117
virtual void writeDouble(double)
virtual int readByte()
std::string hexDump() const
Dump storage content as series of hex values.