src/common/chunk.h
changeset 9 2c31ae7c94db
child 16 99e833adbb46
equal deleted inserted replaced
8:cb4ae01ba180 9:2c31ae7c94db
       
     1 /* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
       
     2 /*
       
     3  * Copyright (c) 2005 INRIA
       
     4  * All rights reserved.
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License version 2 as
       
     8  * published by the Free Software Foundation;
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18  *
       
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    20  */
       
    21 
       
    22 #ifndef CHUNK_H
       
    23 #define CHUNK_H
       
    24 
       
    25 #include <stdint.h>
       
    26 #include <ostream>
       
    27 #include "buffer.h"
       
    28 
       
    29 namespace yans {
       
    30 
       
    31 /**
       
    32  * \brief Protocol header serialization and deserialization.
       
    33  *
       
    34  * Every Protocol header which needs to be inserted and removed
       
    35  * from a Packet instance must derive from this abstract base class
       
    36  * and implement the private pure virtual methods defined here.
       
    37  */
       
    38 class Chunk {
       
    39 public:
       
    40 	Chunk ();
       
    41 	/**
       
    42 	 * Derived classes must provided an explicit virtual destructor
       
    43 	 */
       
    44 	virtual ~Chunk () = 0;
       
    45 
       
    46 	void print (std::ostream &os) const;
       
    47 
       
    48 	void add (Buffer *buffer) const;
       
    49 	void peek (Buffer const *buffer);
       
    50 	void remove (Buffer *buffer);
       
    51 private:
       
    52 	bool m_must_peek_before_remove;
       
    53 	/**
       
    54 	 * \param os the std output stream in which this 
       
    55 	 *           protocol header must print itself.
       
    56 	 */
       
    57 	virtual void print (std::ostream *os) const = 0;
       
    58 
       
    59 	/**
       
    60 	 * \param buffer the buffer in which the protocol header
       
    61 	 *        must serialize itself.
       
    62 	 *
       
    63 	 * This method must:
       
    64 	 *   - reserve room for its serialized representation in the input buffer
       
    65 	 *   - serialize itself in this reserved room
       
    66 	 */
       
    67 	virtual void add_to (Buffer *buffer) const = 0;
       
    68 	/**
       
    69 	 * \param buffer the buffer from which the protocol header must
       
    70 	 *        deserialize itself.
       
    71 	 *
       
    72 	 */
       
    73 	virtual void peek_from (Buffer const *buffer) = 0;
       
    74 	/**
       
    75 	 * \param buffer the buffer from which the protocol header
       
    76 	 *        must remove itself.
       
    77 	 *
       
    78 	 * This method must remove its serialized representation 
       
    79 	 * from the input buffer. This method does not need to deserialize
       
    80 	 * the data itself.
       
    81 	 */
       
    82 	virtual void remove_from (Buffer *buffer) = 0;
       
    83 };
       
    84 
       
    85 std::ostream& operator<< (std::ostream& os, Chunk const& chunk);
       
    86 
       
    87 }; // namespace yans
       
    88 
       
    89 #endif /* CHUNK_H */