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