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 <core/exceptions/software.h>
25#include <fvutils/net/fuse_message_content.h>
26
27#include <cstdlib>
28#include <cstring>
29
30namespace firevision {
31
32/** @class FuseMessageContent <fvutils/net/fuse_message_content.h>
33 * FUSE message content.
34 * Interface for complex FUSE network messages. Use this type if you want
35 * either a nicer interface to your network message or if you need a more
36 * complex kind of message type, for example by using DynamicBuffer.
37 *
38 * Implement all accessor methods that you need and add any data you want.
39 * In the end you have to implement serialize() to create a single contiguous
40 * buffer that contains all the data that has to be sent. Make _payload point
41 * to this buffer and _payload_size contain the size of the buffer.
42 *
43 * @see DynamicBuffer
44 * @ingroup FUSE
45 * @ingroup FireVisioin
46 * @author Tim Niemueller
47 *
48 * @fn void FuseMessageContent::serialize() = 0
49 * Serialize message content.
50 * Generate a single contiguous buffer. Make _payload point to this buffer and
51 * _payload_size contain the size of the buffer.
52 */
53
54/** Constructor. */
56{
57 _payload = NULL;
58 _payload_size = 0;
59}
60
61/** Virtual empty destructor. */
63{
64}
65
66/** Return pointer to payload.
67 * @return pointer to payload
68 * @exception NullPointerException thrown if _payload does not point to a valid
69 * buffer or if _payload_size is zero.
70 */
71void *
73{
74 if ((_payload == NULL) || (_payload_size == 0)) {
75 throw fawkes::NullPointerException("Payload in network message content may not be NULL");
76 }
77 return _payload;
78}
79
80/** Return payload size
81 * @return payload size
82 * @exception NullPointerException thrown if _payload does not point to a valid
83 * buffer or if _payload_size is zero.
84 */
85size_t
87{
88 if ((_payload == NULL) || (_payload_size == 0)) {
89 throw fawkes::NullPointerException("Payload in network message content may not be NULL");
90 }
91 return _payload_size;
92}
93
94/** Copy payload into payload buffer to a specified offset.
95 * This assumes that you have made sure that the buffer is big enough!
96 * @param offset offset in _payload where to copy the data to
97 * @param buf buffer to copy from
98 * @param len number of bytes to copy from buf
99 */
100void
101FuseMessageContent::copy_payload(size_t offset, void *buf, size_t len)
102{
103 void *tmp = (void *)((size_t)_payload + offset);
104 memcpy(tmp, buf, len);
105}
106
107/** Free message payload. */
108void
110{
111 if (_payload)
112 free(_payload);
113 _payload = NULL;
114 _payload_size = 0;
115}
116
117} // end namespace firevision
A NULL pointer was supplied where not allowed.
Definition: software.h:32
void free_payload()
Free message payload.
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.
virtual size_t payload_size() const
Return payload size.
void * _payload
Pointer to payload.
virtual ~FuseMessageContent()
Virtual empty destructor.