1 //------------------------------------------------------------------------------ 2 // MessageUtils.h 3 // 4 //------------------------------------------------------------------------------ 5 6 #ifndef MESSAGEUTILS_H 7 #define MESSAGEUTILS_H 8 9 // Standard Includes ----------------------------------------------------------- 10 11 // System Includes ------------------------------------------------------------- 12 #include <ByteOrder.h> 13 #include <DataIO.h> 14 #include <Entry.h> 15 #include <Message.h> 16 #include <Messenger.h> 17 #include <SupportDefs.h> 18 19 // Project Includes ------------------------------------------------------------ 20 21 // Local Includes -------------------------------------------------------------- 22 23 // Local Defines --------------------------------------------------------------- 24 25 // Globals --------------------------------------------------------------------- 26 27 uint32 _checksum_(const uchar *buf, int32 size); 28 29 namespace BPrivate { // Only putting these here because Be did 30 31 status_t entry_ref_flatten(char* buffer, size_t* size, const entry_ref* ref); 32 status_t entry_ref_unflatten(entry_ref* ref, const char* buffer, size_t size); 33 status_t entry_ref_swap(char* buffer, size_t size); 34 35 } // namespace BPrivate 36 37 //------------------------------------------------------------------------------ 38 // _set_message_target_ 39 /*! \brief Sets the target of a message. 40 41 \param message The message. 42 \param token The target handler token. 43 \param preferred Indicates whether to use the looper's preferred handler. 44 */ 45 inline 46 void 47 _set_message_target_(BMessage *message, int32 token, bool preferred) 48 { 49 message->fTarget = token; 50 message->fPreferred = preferred; 51 } 52 //------------------------------------------------------------------------------ 53 // _set_message_reply_ 54 /*! \brief Sets a message's reply target. 55 56 \param message The message. 57 \param messenger The reply messenger. 58 */ 59 inline 60 void 61 _set_message_reply_(BMessage *message, BMessenger messenger) 62 { 63 message->fReplyTo.port = messenger.fPort; 64 message->fReplyTo.target = messenger.fHandlerToken; 65 message->fReplyTo.team = messenger.fTeam; 66 message->fReplyTo.preferred = messenger.fPreferredTarget; 67 } 68 //------------------------------------------------------------------------------ 69 inline int32 _get_message_target_(BMessage *msg) 70 { 71 return msg->fTarget; 72 } 73 //------------------------------------------------------------------------------ 74 inline bool _use_preferred_target_(BMessage *msg) 75 { 76 return msg->fPreferred; 77 } 78 //------------------------------------------------------------------------------ 79 inline status_t normalize_err(status_t err) 80 { 81 return err >= 0 ? B_OK : err; 82 } 83 //------------------------------------------------------------------------------ 84 template<class T> 85 inline void byte_swap(T& /*data*/) 86 { 87 // Specialize for data types which actually swap 88 } 89 //------------------------------------------------------------------------------ 90 inline void write_helper(BDataIO* stream, const void* data, size_t size, 91 status_t& err) 92 { 93 err = normalize_err(stream->Write(data, size)); 94 } 95 //------------------------------------------------------------------------------ 96 // Will the template madness never cease?? 97 template<class T> 98 inline status_t write_helper(BDataIO* stream, T& data, status_t& err) 99 { 100 return normalize_err(stream->Write((const void*)data, sizeof (T))); 101 } 102 //------------------------------------------------------------------------------ 103 class TReadHelper 104 { 105 public: 106 TReadHelper(BDataIO* stream) 107 : fStream(stream), err(B_OK), fSwap(false) 108 { 109 ; 110 } 111 112 TReadHelper(BDataIO* stream, bool swap) 113 : fStream(stream), err(B_OK), fSwap(swap) 114 { 115 ; 116 } 117 118 template<class T> inline void operator()(T& data) 119 { 120 err = normalize_err(fStream->Read((void*)&data, sizeof (T))); 121 if (err) 122 throw err; 123 124 if (IsSwapping()) 125 { 126 byte_swap(data); 127 } 128 } 129 130 template<class T> inline void operator()(T data, size_t len) 131 { 132 err = normalize_err(fStream->Read((void*)data, len)); 133 if (err) 134 throw err; 135 } 136 137 status_t Status() { return err; } 138 void SetSwap(bool yesNo) { fSwap = yesNo; } 139 bool IsSwapping() { return fSwap; } 140 141 private: 142 BDataIO* fStream; 143 status_t err; 144 bool fSwap; 145 }; 146 //------------------------------------------------------------------------------ 147 class TChecksumHelper 148 { 149 public: 150 TChecksumHelper(uchar* buffer) : fBuffer(buffer), fBufPtr(buffer) {;} 151 152 template<class T> inline void Cache(T& data) 153 { 154 *((T*)fBufPtr) = data; 155 fBufPtr += sizeof (T); 156 } 157 158 int32 CheckSum(); 159 160 private: 161 uchar* fBuffer; 162 uchar* fBufPtr; 163 }; 164 //------------------------------------------------------------------------------ 165 template<class T> inline status_t read_helper(BDataIO* stream, T& data) 166 { 167 return normalize_err(stream->Read((void*)&data, sizeof (T))); 168 } 169 //------------------------------------------------------------------------------ 170 template<> inline void byte_swap(double& data) 171 { 172 data = __swap_double(data); 173 } 174 template<> inline void byte_swap(float& data) 175 { 176 data = __swap_float(data); 177 } 178 template<> inline void byte_swap(int64& data) 179 { 180 data = __swap_int64(data); 181 } 182 template<> inline void byte_swap(int32& data) 183 { 184 data = __swap_int32(data); 185 } 186 template<> inline void byte_swap(int16& data) 187 { 188 data = __swap_int16(data); 189 } 190 template<> inline void byte_swap(entry_ref& data) 191 { 192 byte_swap(data.device); 193 byte_swap(data.directory); 194 } 195 //------------------------------------------------------------------------------ 196 197 #endif // MESSAGEUTILS_H 198 199 /* 200 * $Log $ 201 * 202 * $Id $ 203 * 204 */ 205 206