Templatize PortLink & PortMessage

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3950 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
shadow303 2003-07-11 01:49:58 +00:00
parent 1268b04d96
commit d1acc68a9d
4 changed files with 11 additions and 303 deletions

View File

@ -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 <class Type> status_t Attach(Type data);
void MakeEmpty(void);
protected:
void FlattenData(int8 **buffer,int32 *size);

View File

@ -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 <class Type> status_t Read(Type *data);
void Rewind(void);

View File

@ -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 <class Type> 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)

View File

@ -128,130 +128,20 @@ void PortMessage::SetBuffer(const void *buffer, const ssize_t &size, const bool
}
}
status_t PortMessage::Read(int64 *data)
template <class Type> status_t PortMessage::Read(Type *data)
{
int32 size = sizeof(Type);
if(!data)
return B_BAD_VALUE;
if( !_buffer ||
_buffersize<sizeof(int64) ||
((_index+sizeof(int64))>(_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<sizeof(bool) ||
((_index+sizeof(bool))>(_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<sizeof(BPoint) ||
((_index+sizeof(BPoint))>(_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<sizeof(BRect) ||
((_index+sizeof(BRect))>(_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<sizeof(float) ||
((_index+sizeof(float))>(_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<sizeof(int) ||
((_index+sizeof(int))>(_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<sizeof(long) ||
((_index+sizeof(long))>(_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<sizeof(double) ||
((_index+sizeof(double))>(_buffer+_buffersize)) )
return B_NO_MEMORY;
*data=*((double*)_index);
_index+=sizeof(double);
*data=*((Type*)_index);
_index+=size;
return B_OK;
}