Added error checking using exceptions

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21112 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini 2007-05-12 08:46:56 +00:00
parent 7f2c1e802f
commit 140334f858
2 changed files with 222 additions and 112 deletions

View File

@ -64,11 +64,11 @@ public:
protected:
status_t WriteData(const void *data, size_t size);
template <typename T> status_t Write(const T &data) { return WriteData(&data, sizeof(data)); }
status_t BeginOp(const int16 &op);
status_t EndOp();
// throw a status_t on error
void BeginOp(const int16 &op);
void EndOp();
void WriteData(const void *data, size_t size);
template <typename T> void Write(const T &data) { WriteData(&data, sizeof(data)); }
private:
BPositionIO *fData;

View File

@ -32,6 +32,8 @@ PictureDataWriter::PictureDataWriter(BPositionIO *data)
status_t
PictureDataWriter::SetTo(BPositionIO *data)
{
if (data == NULL)
return B_BAD_VALUE;
fData = data;
return B_OK;
}
@ -40,9 +42,13 @@ PictureDataWriter::SetTo(BPositionIO *data)
status_t
PictureDataWriter::WriteSetOrigin(const BPoint &point)
{
BeginOp(B_PIC_SET_ORIGIN);
Write<BPoint>(point);
EndOp();
try {
BeginOp(B_PIC_SET_ORIGIN);
Write<BPoint>(point);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -50,13 +56,17 @@ PictureDataWriter::WriteSetOrigin(const BPoint &point)
status_t
PictureDataWriter::WriteInvertRect(const BRect &rect)
{
WriteSetDrawingMode(B_OP_INVERT);
BeginOp(B_PIC_FILL_RECT);
Write<BRect>(rect);
EndOp();
try {
WriteSetDrawingMode(B_OP_INVERT);
BeginOp(B_PIC_FILL_RECT);
Write<BRect>(rect);
EndOp();
WriteSetDrawingMode(B_OP_COPY);
WriteSetDrawingMode(B_OP_COPY);
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -64,9 +74,13 @@ PictureDataWriter::WriteInvertRect(const BRect &rect)
status_t
PictureDataWriter::WriteSetDrawingMode(const drawing_mode &mode)
{
BeginOp(B_PIC_SET_DRAWING_MODE);
Write<int16>((int16)mode);
EndOp();
try {
BeginOp(B_PIC_SET_DRAWING_MODE);
Write<int16>((int16)mode);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -74,9 +88,13 @@ PictureDataWriter::WriteSetDrawingMode(const drawing_mode &mode)
status_t
PictureDataWriter::WriteSetPenSize(const float &penSize)
{
BeginOp(B_PIC_SET_PEN_SIZE);
Write<float>(penSize);
EndOp();
try {
BeginOp(B_PIC_SET_PEN_SIZE);
Write<float>(penSize);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -84,11 +102,15 @@ PictureDataWriter::WriteSetPenSize(const float &penSize)
status_t
PictureDataWriter::WriteSetLineMode(const cap_mode &cap, const join_mode &join, const float &miterLimit)
{
BeginOp(B_PIC_SET_LINE_MODE);
Write<int16>((int16)cap);
Write<int16>((int16)join);
Write<float>(miterLimit);
EndOp();
try {
BeginOp(B_PIC_SET_LINE_MODE);
Write<int16>((int16)cap);
Write<int16>((int16)join);
Write<float>(miterLimit);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -96,9 +118,13 @@ PictureDataWriter::WriteSetLineMode(const cap_mode &cap, const join_mode &join,
status_t
PictureDataWriter::WriteSetScale(const float &scale)
{
BeginOp(B_PIC_SET_SCALE);
Write<float>(scale);
EndOp();
try {
BeginOp(B_PIC_SET_SCALE);
Write<float>(scale);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -106,9 +132,13 @@ PictureDataWriter::WriteSetScale(const float &scale)
status_t
PictureDataWriter::WriteSetHighColor(const rgb_color &color)
{
BeginOp(B_PIC_SET_FORE_COLOR);
Write<rgb_color>(color);
EndOp();
try {
BeginOp(B_PIC_SET_FORE_COLOR);
Write<rgb_color>(color);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -116,9 +146,13 @@ PictureDataWriter::WriteSetHighColor(const rgb_color &color)
status_t
PictureDataWriter::WriteSetLowColor(const rgb_color &color)
{
BeginOp(B_PIC_SET_BACK_COLOR);
Write<rgb_color>(color);
EndOp();
try {
BeginOp(B_PIC_SET_BACK_COLOR);
Write<rgb_color>(color);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -126,9 +160,13 @@ PictureDataWriter::WriteSetLowColor(const rgb_color &color)
status_t
PictureDataWriter::WriteDrawRect(const BRect &rect, const bool &fill)
{
BeginOp(fill ? B_PIC_FILL_RECT : B_PIC_STROKE_RECT);
Write<BRect>(rect);
EndOp();
try {
BeginOp(fill ? B_PIC_FILL_RECT : B_PIC_STROKE_RECT);
Write<BRect>(rect);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -136,10 +174,14 @@ PictureDataWriter::WriteDrawRect(const BRect &rect, const bool &fill)
status_t
PictureDataWriter::WriteDrawRoundRect(const BRect &rect, const BPoint &radius, const bool &fill)
{
BeginOp(fill ? B_PIC_FILL_ROUND_RECT : B_PIC_STROKE_ROUND_RECT);
Write<BRect>(rect);
Write<BPoint>(radius);
EndOp();
try {
BeginOp(fill ? B_PIC_FILL_ROUND_RECT : B_PIC_STROKE_ROUND_RECT);
Write<BRect>(rect);
Write<BPoint>(radius);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -147,9 +189,13 @@ PictureDataWriter::WriteDrawRoundRect(const BRect &rect, const BPoint &radius, c
status_t
PictureDataWriter::WriteDrawEllipse(const BRect &rect, const bool &fill)
{
BeginOp(fill ? B_PIC_FILL_ELLIPSE : B_PIC_STROKE_ELLIPSE);
Write<BRect>(rect);
EndOp();
try {
BeginOp(fill ? B_PIC_FILL_ELLIPSE : B_PIC_STROKE_ELLIPSE);
Write<BRect>(rect);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -158,12 +204,16 @@ status_t
PictureDataWriter::WriteDrawArc(const BPoint &center, const BPoint &radius,
const float &startTheta, const float &arcTheta, const bool &fill)
{
BeginOp(fill ? B_PIC_FILL_ARC : B_PIC_STROKE_ARC);
Write<BPoint>(center);
Write<BPoint>(radius);
Write<float>(startTheta);
Write<float>(arcTheta);
EndOp();
try {
BeginOp(fill ? B_PIC_FILL_ARC : B_PIC_STROKE_ARC);
Write<BPoint>(center);
Write<BPoint>(radius);
Write<float>(startTheta);
Write<float>(arcTheta);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -171,10 +221,14 @@ PictureDataWriter::WriteDrawArc(const BPoint &center, const BPoint &radius,
status_t
PictureDataWriter::WriteStrokeLine(const BPoint &start, const BPoint &end)
{
BeginOp(B_PIC_STROKE_LINE);
Write<BPoint>(start);
Write<BPoint>(end);
EndOp();
try {
BeginOp(B_PIC_STROKE_LINE);
Write<BPoint>(start);
Write<BPoint>(end);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -183,17 +237,21 @@ status_t
PictureDataWriter::WriteDrawString(const BPoint &where, const char *string,
const int32 &length, const escapement_delta &escapement)
{
BeginOp(B_PIC_SET_PEN_LOCATION);
Write<BPoint>(where);
EndOp();
BeginOp(B_PIC_DRAW_STRING);
Write<float>(escapement.space);
Write<float>(escapement.nonspace);
//WriteData(string, length + 1); // TODO: is string 0 terminated? why is length given?
WriteData(string, length);
Write<uint8>(0);
EndOp();
try {
BeginOp(B_PIC_SET_PEN_LOCATION);
Write<BPoint>(where);
EndOp();
BeginOp(B_PIC_DRAW_STRING);
Write<float>(escapement.space);
Write<float>(escapement.nonspace);
//WriteData(string, length + 1); // TODO: is string 0 terminated? why is length given?
WriteData(string, length);
Write<uint8>(0);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -203,12 +261,16 @@ status_t
PictureDataWriter::WriteDrawShape(const int32 &opCount, const void *opList,
const int32 &ptCount, const void *ptList, const bool &fill)
{
BeginOp(fill ? B_PIC_FILL_SHAPE : B_PIC_STROKE_SHAPE);
Write<int32>(opCount);
Write<int32>(ptCount);
WriteData(opList, opCount * sizeof(uint32));
WriteData(ptList, ptCount * sizeof(BPoint));
EndOp();
try {
BeginOp(fill ? B_PIC_FILL_SHAPE : B_PIC_STROKE_SHAPE);
Write<int32>(opCount);
Write<int32>(ptCount);
WriteData(opList, opCount * sizeof(uint32));
WriteData(ptList, ptCount * sizeof(BPoint));
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -221,16 +283,20 @@ PictureDataWriter::WriteDrawBitmap(const BRect &srcRect, const BRect &dstRect, c
{
if (length != height * bytesPerRow)
debugger("PictureDataWriter::WriteDrawBitmap: invalid length");
BeginOp(B_PIC_DRAW_PIXELS);
Write<BRect>(srcRect);
Write<BRect>(dstRect);
Write<int32>(width);
Write<int32>(height);
Write<int32>(bytesPerRow);
Write<int32>(colorSpace);
Write<int32>(flags);
WriteData(data, length);
EndOp();
try {
BeginOp(B_PIC_DRAW_PIXELS);
Write<BRect>(srcRect);
Write<BRect>(dstRect);
Write<int32>(width);
Write<int32>(height);
Write<int32>(bytesPerRow);
Write<int32>(colorSpace);
Write<int32>(flags);
WriteData(data, length);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -258,9 +324,13 @@ PictureDataWriter::WriteSetFontStyle(const font_style &style)
status_t
PictureDataWriter::WriteSetFontSpacing(const int32 &spacing)
{
BeginOp(B_PIC_SET_FONT_SPACING);
Write<int32>(spacing);
EndOp();
try {
BeginOp(B_PIC_SET_FONT_SPACING);
Write<int32>(spacing);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -268,9 +338,13 @@ PictureDataWriter::WriteSetFontSpacing(const int32 &spacing)
status_t
PictureDataWriter::WriteSetFontSize(const float &size)
{
BeginOp(B_PIC_SET_FONT_SIZE);
Write<float>(size);
EndOp();
try {
BeginOp(B_PIC_SET_FONT_SIZE);
Write<float>(size);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -278,9 +352,13 @@ PictureDataWriter::WriteSetFontSize(const float &size)
status_t
PictureDataWriter::WriteSetFontRotation(const float &rotation)
{
BeginOp(B_PIC_SET_FONT_ROTATE);
Write<float>(rotation);
EndOp();
try {
BeginOp(B_PIC_SET_FONT_ROTATE);
Write<float>(rotation);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -288,9 +366,13 @@ PictureDataWriter::WriteSetFontRotation(const float &rotation)
status_t
PictureDataWriter::WriteSetFontEncoding(const int32 &encoding)
{
BeginOp(B_PIC_SET_FONT_ENCODING);
Write<int32>(encoding);
EndOp();
try {
BeginOp(B_PIC_SET_FONT_ENCODING);
Write<int32>(encoding);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -298,9 +380,13 @@ PictureDataWriter::WriteSetFontEncoding(const int32 &encoding)
status_t
PictureDataWriter::WriteSetFontFlags(const int32 &flags)
{
BeginOp(B_PIC_SET_FONT_FLAGS);
Write<int32>(flags);
EndOp();
try {
BeginOp(B_PIC_SET_FONT_FLAGS);
Write<int32>(flags);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -308,9 +394,13 @@ PictureDataWriter::WriteSetFontFlags(const int32 &flags)
status_t
PictureDataWriter::WriteSetFontShear(const int32 &shear)
{
BeginOp(B_PIC_SET_FONT_SHEAR);
Write<int32>(shear);
EndOp();
try {
BeginOp(B_PIC_SET_FONT_SHEAR);
Write<int32>(shear);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -318,9 +408,13 @@ PictureDataWriter::WriteSetFontShear(const int32 &shear)
status_t
PictureDataWriter::WriteSetFontFace(const int32 &face)
{
BeginOp(B_PIC_SET_FONT_FACE);
Write<int32>(face);
EndOp();
try {
BeginOp(B_PIC_SET_FONT_FACE);
Write<int32>(face);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -328,8 +422,12 @@ PictureDataWriter::WriteSetFontFace(const int32 &face)
status_t
PictureDataWriter::WritePushState()
{
BeginOp(B_PIC_PUSH_STATE);
EndOp();
try {
BeginOp(B_PIC_PUSH_STATE);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
@ -337,29 +435,38 @@ PictureDataWriter::WritePushState()
status_t
PictureDataWriter::WritePopState()
{
BeginOp(B_PIC_POP_STATE);
EndOp();
try {
BeginOp(B_PIC_POP_STATE);
EndOp();
} catch (status_t &status) {
return status;
}
return B_OK;
}
// private
status_t
void
PictureDataWriter::BeginOp(const int16 &op)
{
if (fData == NULL)
throw B_NO_INIT;
fStack.push(fData->Position());
fData->Write(&op, sizeof(op));
// Init the size of the opcode block to 0
size_t size = 0;
fData->Write(&size, sizeof(size));
return B_OK;
}
status_t
void
PictureDataWriter::EndOp()
{
if (fData == NULL)
throw B_NO_INIT;
off_t curPos = fData->Position();
off_t stackPos = fStack.top();
fStack.pop();
@ -374,12 +481,15 @@ PictureDataWriter::EndOp()
fData->Seek(stackPos + sizeof(int16), SEEK_SET);
fData->Write(&size, sizeof(size));
fData->Seek(curPos, SEEK_SET);
return B_OK;
}
status_t
void
PictureDataWriter::WriteData(const void *data, size_t size)
{
return fData->Write(data, size);
ssize_t result = fData->Write(data, size);
if (result < 0)
throw (status_t)result;
if (result != size)
throw B_IO_ERROR;
}