From d1acc68a9d9d76725ad72dc3d77eed2ebe457aec Mon Sep 17 00:00:00 2001 From: shadow303 Date: Fri, 11 Jul 2003 01:49:58 +0000 Subject: [PATCH] Templatize PortLink & PortMessage git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3950 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/app/PortLink.h | 8 +- headers/private/app/PortMessage.h | 9 +- src/kits/app/PortLink.cpp | 173 +----------------------------- src/kits/app/PortMessage.cpp | 124 ++------------------- 4 files changed, 11 insertions(+), 303 deletions(-) diff --git a/headers/private/app/PortLink.h b/headers/private/app/PortLink.h index e05d8078aa..00b1daa5a8 100644 --- a/headers/private/app/PortLink.h +++ b/headers/private/app/PortLink.h @@ -35,13 +35,7 @@ public: status_t FlushWithReply(PortMessage *msg,bigtime_t timeout=B_INFINITE_TIMEOUT); status_t Attach(const void *data, size_t size); - status_t Attach(int32 data); - status_t Attach(int16 data); - status_t Attach(int8 data); - status_t Attach(float data); - status_t Attach(bool data); - status_t Attach(BRect data); - status_t Attach(BPoint data); + template status_t Attach(Type data); void MakeEmpty(void); protected: void FlattenData(int8 **buffer,int32 *size); diff --git a/headers/private/app/PortMessage.h b/headers/private/app/PortMessage.h index 5aa584457d..4d6c5e96c2 100644 --- a/headers/private/app/PortMessage.h +++ b/headers/private/app/PortMessage.h @@ -47,15 +47,8 @@ public: void *Buffer(void) { return _buffer; } ssize_t BufferSize(void) { return _buffersize; } - status_t Read(int64 *data); - status_t Read(bool *data); - status_t Read(BPoint *data); - status_t Read(BRect *data); - status_t Read(float *data); - status_t Read(int *data); - status_t Read(long *data); - status_t Read(double *data); status_t Read(void *data, ssize_t size); + template status_t Read(Type *data); void Rewind(void); diff --git a/src/kits/app/PortLink.cpp b/src/kits/app/PortLink.cpp index e67afdee4e..cb750e0001 100644 --- a/src/kits/app/PortLink.cpp +++ b/src/kits/app/PortLink.cpp @@ -536,178 +536,9 @@ printf("\tAttach(): Couldn't assign data to PortLinkData object\n"); return B_OK; } -// These functions were added for a major convenience in passing common types -// Eventually, I'd like to templatize these, but for now, this'll do - -status_t PortLink::Attach(int32 data) +template status_t PortLink::Attach(Type data) { -#ifdef PLDEBUG -printf("Attach(%ld)\n",data); -#endif - int32 size=sizeof(int32); - -#ifdef CAPACITY_CHECKING - if(bufferlength+size>capacity) - return B_NO_MEMORY; -#endif - - // create a new storage object and stash the data - PortLinkData *pld=new PortLinkData; - if(pld->Set(&data,size)==B_OK) - { - attachlist->AddItem(pld); - bufferlength+=size; - } - else - { - delete pld; - return B_ERROR; - } - return B_OK; -} - -status_t PortLink::Attach(int16 data) -{ -#ifdef PLDEBUG -printf("Attach(%d)\n",data); -#endif - int32 size=sizeof(int16); - -#ifdef CAPACITY_CHECKING - if(bufferlength+size>capacity) - return B_NO_MEMORY; -#endif - - // create a new storage object and stash the data - PortLinkData *pld=new PortLinkData; - if(pld->Set(&data,size)==B_OK) - { - attachlist->AddItem(pld); - bufferlength+=size; - } - else - { - delete pld; - return B_ERROR; - } - return B_OK; -} - -status_t PortLink::Attach(int8 data) -{ -#ifdef PLDEBUG -printf("Attach(%d)\n",data); -#endif - int32 size=sizeof(int8); - -#ifdef CAPACITY_CHECKING - if(bufferlength+size>capacity) - return B_NO_MEMORY; -#endif - - // create a new storage object and stash the data - PortLinkData *pld=new PortLinkData; - if(pld->Set(&data,size)==B_OK) - { - attachlist->AddItem(pld); - bufferlength+=size; - } - else - { - delete pld; - return B_ERROR; - } - return B_OK; -} - -status_t PortLink::Attach(float data) -{ -#ifdef PLDEBUG -printf("Attach(%f)\n",data); -#endif - int32 size=sizeof(float); - -#ifdef CAPACITY_CHECKING - if(bufferlength+size>capacity) - return B_NO_MEMORY; -#endif - - // create a new storage object and stash the data - PortLinkData *pld=new PortLinkData; - if(pld->Set(&data,size)==B_OK) - { - attachlist->AddItem(pld); - bufferlength+=size; - } - else - { - delete pld; - return B_ERROR; - } - return B_OK; -} - -status_t PortLink::Attach(bool data) -{ -#ifdef PLDEBUG -printf("Attach(%s)\n",(data)?"true":"false"); -#endif - - int32 size=sizeof(bool); - -#ifdef CAPACITY_CHECKING - if(bufferlength+size>capacity) - return B_NO_MEMORY; -#endif - - // create a new storage object and stash the data - PortLinkData *pld=new PortLinkData; - if(pld->Set(&data,size)==B_OK) - { - attachlist->AddItem(pld); - bufferlength+=size; - } - else - { - delete pld; - return B_ERROR; - } - return B_OK; -} - -status_t PortLink::Attach(BRect data) -{ -#ifdef PLDEBUG -printf("Attach(BRect(%f,%f,%f,%f))\n",data.left,data.top,data.right,data.bottom); -#endif - int32 size=sizeof(BRect); - -#ifdef CAPACITY_CHECKING - if(bufferlength+size>capacity) - return B_NO_MEMORY; -#endif - - // create a new storage object and stash the data - PortLinkData *pld=new PortLinkData; - if(pld->Set(&data,size)==B_OK) - { - attachlist->AddItem(pld); - bufferlength+=size; - } - else - { - delete pld; - return B_ERROR; - } - return B_OK; -} - -status_t PortLink::Attach(BPoint data) -{ -#ifdef PLDEBUG -printf("Attach(BPoint(%f,%f))\n",data.x,data.y); -#endif - int32 size=sizeof(BPoint); + int32 size=sizeof(Type); #ifdef CAPACITY_CHECKING if(bufferlength+size>capacity) diff --git a/src/kits/app/PortMessage.cpp b/src/kits/app/PortMessage.cpp index 5d246c4bd3..1ab7a91f3d 100644 --- a/src/kits/app/PortMessage.cpp +++ b/src/kits/app/PortMessage.cpp @@ -128,130 +128,20 @@ void PortMessage::SetBuffer(const void *buffer, const ssize_t &size, const bool } } -status_t PortMessage::Read(int64 *data) +template status_t PortMessage::Read(Type *data) { + int32 size = sizeof(Type); + if(!data) return B_BAD_VALUE; if( !_buffer || - _buffersize(_buffer+_buffersize)) ) + (_buffersize < size) || + (_index+size > _buffer+_buffersize) ) return B_NO_MEMORY; - *data=*((int64*)_index); - _index+=sizeof(bool); - - return B_OK; -} - -status_t PortMessage::Read(bool *data) -{ - if(!data) - return B_BAD_VALUE; - - if( !_buffer || - _buffersize(_buffer+_buffersize)) ) - return B_NO_MEMORY; - - *data=*((bool*)_index); - _index+=sizeof(bool); - - return B_OK; -} - -status_t PortMessage::Read(BPoint *data) -{ - if(!data) - return B_BAD_VALUE; - - if( !_buffer || - _buffersize(_buffer+_buffersize)) ) - return B_NO_MEMORY; - - *data=*((BPoint*)_index); - _index+=sizeof(BPoint); - - return B_OK; -} - -status_t PortMessage::Read(BRect *data) -{ - if(!data) - return B_BAD_VALUE; - - if( !_buffer || - _buffersize(_buffer+_buffersize)) ) - return B_NO_MEMORY; - - *data=*((BRect*)_index); - _index+=sizeof(BRect); - - return B_OK; -} - -status_t PortMessage::Read(float *data) -{ - if(!data) - return B_BAD_VALUE; - - if( !_buffer || - _buffersize(_buffer+_buffersize)) ) - return B_NO_MEMORY; - - *data=*((float*)_index); - _index+=sizeof(float); - - return B_OK; -} - -status_t PortMessage::Read(int *data) -{ - if(!data) - return B_BAD_VALUE; - - if( !_buffer || - _buffersize(_buffer+_buffersize)) ) - return B_NO_MEMORY; - - *data=*((int*)_index); - _index+=sizeof(int); - - return B_OK; -} - -status_t PortMessage::Read(long *data) -{ - if(!data) - return B_BAD_VALUE; - - if( !_buffer || - _buffersize(_buffer+_buffersize)) ) - return B_NO_MEMORY; - - *data=*((long*)_index); - _index+=sizeof(long); - - return B_OK; -} - -status_t PortMessage::Read(double *data) -{ - if(!data) - return B_BAD_VALUE; - - if( !_buffer || - _buffersize(_buffer+_buffersize)) ) - return B_NO_MEMORY; - - *data=*((double*)_index); - _index+=sizeof(double); + *data=*((Type*)_index); + _index+=size; return B_OK; }