00001 /*******************************************************************\ 00002 00003 SESAME project software license 00004 00005 Copyright (C) 2002, 2003 University of Amsterdam 00006 00007 This program is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU General Public License 00009 as published by the Free Software Foundation; either version 2 00010 of the License, or (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00020 02111-1307, USA. 00021 00022 For information regarding the SESAME software project see 00023 http://sesamesim.sourceforge.net or email 00024 jcofflan@users.sourceforge.net 00025 00026 \*******************************************************************/ 00027 00028 #ifndef BASICMUXMESSAGE_H 00029 #define BASICMUXMESSAGE_H 00030 00031 #include <string.h> 00032 00033 class BasicMUXMessageBase { 00034 protected: 00035 typedef struct { 00036 long type; 00037 long key; 00038 char data[0]; 00039 } message_t; 00040 00041 size_t size; 00042 message_t *msg; 00043 00044 BasicMUXMessageBase() : size(0), msg(NULL) { 00045 } 00046 00047 public: 00048 BasicMUXMessageBase(const void *data, size_t size) : size(size) { 00049 msg = (message_t *)new char[sizeof(message_t) + size]; 00050 if (data) memcpy(msg->data, data, size); 00051 } 00052 00053 BasicMUXMessageBase(size_t size) : size(size) { 00054 msg = (message_t *)new char[sizeof(message_t) + size]; 00055 } 00056 00057 ~BasicMUXMessageBase() { 00058 if (msg) delete msg; 00059 } 00060 00061 size_t getSize() {return size;} 00062 void *getData() {return msg->data;} 00063 00064 friend class BasicMUXChannelBase; 00065 friend class BasicMsgQueueMUX; 00066 }; 00067 00068 template<class T> class BasicMUXChannel; 00069 00070 template<class T> 00071 class BasicMUXMessage : public BasicMUXMessageBase { 00072 public: 00073 BasicMUXMessage() { 00074 size = sizeof(T); 00075 msg = (message_t *)new char[sizeof(message_t) + size]; 00076 } 00077 BasicMUXMessage(T &data) { 00078 size = sizeof(T); 00079 msg = (message_t *)new char[sizeof(message_t) + size]; 00080 (*(T *)msg->data) = data; 00081 } 00082 00083 T &operator->() {return *(T *)getData();} 00084 T &getDataRef() {return *(T *)getData();} 00085 00086 friend class BasicMUXChannel<T>; 00087 }; 00088 #endif