Fawkes API  Fawkes Development Version
fuse_message_content.cpp
1 
2 /***************************************************************************
3  * fuse_message_content.cpp - FUSE network message content
4  *
5  * Created: Thu Nov 22 17:23:20 2007
6  * Copyright 2006-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvutils/net/fuse_message_content.h>
25 #include <core/exceptions/software.h>
26 
27 #include <cstring>
28 #include <cstdlib>
29 
30 namespace firevision {
31 #if 0 /* just to make Emacs auto-indent happy */
32 }
33 #endif
34 
35 /** @class FuseMessageContent <fvutils/net/fuse_message_content.h>
36  * FUSE message content.
37  * Interface for complex FUSE network messages. Use this type if you want
38  * either a nicer interface to your network message or if you need a more
39  * complex kind of message type, for example by using DynamicBuffer.
40  *
41  * Implement all accessor methods that you need and add any data you want.
42  * In the end you have to implement serialize() to create a single contiguous
43  * buffer that contains all the data that has to be sent. Make _payload point
44  * to this buffer and _payload_size contain the size of the buffer.
45  *
46  * @see DynamicBuffer
47  * @ingroup FUSE
48  * @ingroup FireVisioin
49  * @author Tim Niemueller
50  *
51  * @fn void FuseMessageContent::serialize() = 0
52  * Serialize message content.
53  * Generate a single contiguous buffer. Make _payload point to this buffer and
54  * _payload_size contain the size of the buffer.
55  */
56 
57 /** Constructor. */
59 {
60  _payload = NULL;
61  _payload_size = 0;
62 }
63 
64 
65 /** Virtual empty destructor. */
67 {
68 }
69 
70 
71 /** Return pointer to payload.
72  * @return pointer to payload
73  * @exception NullPointerException thrown if _payload does not point to a valid
74  * buffer or if _payload_size is zero.
75  */
76 void *
78 {
79  if ( (_payload == NULL) || (_payload_size == 0) ) {
80  throw fawkes::NullPointerException("Payload in network message content may not be NULL");
81  }
82  return _payload;
83 }
84 
85 
86 
87 /** Return payload size
88  * @return payload size
89  * @exception NullPointerException thrown if _payload does not point to a valid
90  * buffer or if _payload_size is zero.
91  */
92 size_t
94 {
95  if ( (_payload == NULL) || (_payload_size == 0) ) {
96  throw fawkes::NullPointerException("Payload in network message content may not be NULL");
97  }
98  return _payload_size;
99 }
100 
101 
102 /** Copy payload into payload buffer to a specified offset.
103  * This assumes that you have made sure that the buffer is big enough!
104  * @param offset offset in _payload where to copy the data to
105  * @param buf buffer to copy from
106  * @param len number of bytes to copy from buf
107  */
108 void
109 FuseMessageContent::copy_payload(size_t offset, void *buf, size_t len)
110 {
111  void *tmp = (void *)((size_t)_payload + offset);
112  memcpy(tmp, buf, len);
113 }
114 
115 
116 /** Free message payload. */
117 void
119 {
120  if ( _payload ) free(_payload);
121  _payload = NULL;
122  _payload_size = 0;
123 }
124 
125 } // end namespace firevision
A NULL pointer was supplied where not allowed.
Definition: software.h:34
void copy_payload(size_t offset, void *buf, size_t len)
Copy payload into payload buffer to a specified offset.
virtual void * payload() const
Return pointer to payload.
void free_payload()
Free message payload.
void * _payload
Pointer to payload.
virtual size_t payload_size() const
Return payload size.
virtual ~FuseMessageContent()
Virtual empty destructor.