HaikuDepot: Fix passing back cloned StyleData objects

* In the methods that create a new style data object
   by cloning the object and changing the respective
   property, returning a Reference to a style data
   object makes it easier on the calling side to deal
   with the same object being returned or a new one.
This commit is contained in:
Stephan Aßmus 2013-09-02 11:49:07 +02:00
parent 6f1dbba164
commit 621cb2bf13
4 changed files with 72 additions and 46 deletions

View File

@ -57,48 +57,72 @@ ParagraphStyle::operator!=(const ParagraphStyle& other) const
bool bool
ParagraphStyle::SetAlignment(::Alignment alignment) ParagraphStyle::SetAlignment(::Alignment alignment)
{ {
fStyleData.SetTo(fStyleData->SetAlignment(alignment)); ParagraphStyleDataRef data = fStyleData->SetAlignment(alignment);
return fStyleData->Alignment() == alignment; if (data == fStyleData)
return data->Alignment() == alignment;
fStyleData = data;
return true;
} }
bool bool
ParagraphStyle::SetJustify(bool justify) ParagraphStyle::SetJustify(bool justify)
{ {
fStyleData.SetTo(fStyleData->SetJustify(justify)); ParagraphStyleDataRef data = fStyleData->SetJustify(justify);
return fStyleData->Justify() == justify; if (data == fStyleData)
return data->Justify() == justify;
fStyleData = data;
return true;
} }
bool bool
ParagraphStyle::SetFirstLineInset(float inset) ParagraphStyle::SetFirstLineInset(float inset)
{ {
fStyleData.SetTo(fStyleData->SetFirstLineInset(inset)); ParagraphStyleDataRef data = fStyleData->SetFirstLineInset(inset);
return fStyleData->FirstLineInset() == inset; if (data == fStyleData)
return data->FirstLineInset() == inset;
fStyleData = data;
return true;
} }
bool bool
ParagraphStyle::SetLineInset(float inset) ParagraphStyle::SetLineInset(float inset)
{ {
fStyleData.SetTo(fStyleData->SetLineInset(inset)); ParagraphStyleDataRef data = fStyleData->SetLineInset(inset);
return fStyleData->LineInset() == inset; if (data == fStyleData)
return data->LineInset() == inset;
fStyleData = data;
return true;
} }
bool bool
ParagraphStyle::SetSpacingTop(float spacing) ParagraphStyle::SetSpacingTop(float spacing)
{ {
fStyleData.SetTo(fStyleData->SetSpacingTop(spacing)); ParagraphStyleDataRef data = fStyleData->SetSpacingTop(spacing);
return fStyleData->SpacingTop() == spacing; if (data == fStyleData)
return data->SpacingTop() == spacing;
fStyleData = data;
return true;
} }
bool bool
ParagraphStyle::SetSpacingBottom(float spacing) ParagraphStyle::SetSpacingBottom(float spacing)
{ {
fStyleData.SetTo(fStyleData->SetSpacingBottom(spacing)); ParagraphStyleDataRef data = fStyleData->SetSpacingBottom(spacing);
return fStyleData->SpacingBottom() == spacing; if (data == fStyleData)
return data->SpacingBottom() == spacing;
fStyleData = data;
return true;
} }

View File

@ -37,7 +37,7 @@ public:
private: private:
ParagraphDataRef fStyleData; ParagraphStyleDataRef fStyleData;
}; };

View File

@ -58,93 +58,93 @@ ParagraphStyleData::operator!=(const ParagraphStyleData& other) const
} }
ParagraphStyleData* ParagraphStyleDataRef
ParagraphStyleData::SetAlignment(::Alignment alignment) ParagraphStyleData::SetAlignment(::Alignment alignment)
{ {
if (fAlignment == alignment) if (fAlignment == alignment)
return this; return ParagraphStyleDataRef(this);
ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this); ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this);
if (ret == NULL) if (ret == NULL)
return this; return ParagraphStyleDataRef(this);
ret->fAlignment = alignment; ret->fAlignment = alignment;
return ret; return ParagraphStyleDataRef(ret, true);
} }
ParagraphStyleData* ParagraphStyleDataRef
ParagraphStyleData::SetJustify(bool justify) ParagraphStyleData::SetJustify(bool justify)
{ {
if (fJustify == justify) if (fJustify == justify)
return this; return ParagraphStyleDataRef(this);
ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this); ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this);
if (ret == NULL) if (ret == NULL)
return this; return ParagraphStyleDataRef(this);
ret->fJustify = justify; ret->fJustify = justify;
return ret; return ParagraphStyleDataRef(ret, true);
} }
ParagraphStyleData* ParagraphStyleDataRef
ParagraphStyleData::SetFirstLineInset(float inset) ParagraphStyleData::SetFirstLineInset(float inset)
{ {
if (fFirstLineInset == inset) if (fFirstLineInset == inset)
return this; return ParagraphStyleDataRef(this);
ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this); ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this);
if (ret == NULL) if (ret == NULL)
return this; return ParagraphStyleDataRef(this);
ret->fFirstLineInset = inset; ret->fFirstLineInset = inset;
return ret; return ParagraphStyleDataRef(ret, true);
} }
ParagraphStyleData* ParagraphStyleDataRef
ParagraphStyleData::SetLineInset(float inset) ParagraphStyleData::SetLineInset(float inset)
{ {
if (fLineInset == inset) if (fLineInset == inset)
return this; return ParagraphStyleDataRef(this);
ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this); ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this);
if (ret == NULL) if (ret == NULL)
return this; return ParagraphStyleDataRef(this);
ret->fLineInset = inset; ret->fLineInset = inset;
return ret; return ParagraphStyleDataRef(ret, true);
} }
ParagraphStyleData* ParagraphStyleDataRef
ParagraphStyleData::SetSpacingTop(float spacing) ParagraphStyleData::SetSpacingTop(float spacing)
{ {
if (fSpacingTop == spacing) if (fSpacingTop == spacing)
return this; return ParagraphStyleDataRef(this);
ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this); ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this);
if (ret == NULL) if (ret == NULL)
return this; return ParagraphStyleDataRef(this);
ret->fSpacingTop = spacing; ret->fSpacingTop = spacing;
return ret; return ParagraphStyleDataRef(ret, true);
} }
ParagraphStyleData* ParagraphStyleDataRef
ParagraphStyleData::SetSpacingBottom(float spacing) ParagraphStyleData::SetSpacingBottom(float spacing)
{ {
if (fSpacingBottom == spacing) if (fSpacingBottom == spacing)
return this; return ParagraphStyleDataRef(this);
ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this); ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this);
if (ret == NULL) if (ret == NULL)
return this; return ParagraphStyleDataRef(this);
ret->fSpacingBottom = spacing; ret->fSpacingBottom = spacing;
return ret; return ParagraphStyleDataRef(ret, true);
} }

View File

@ -14,6 +14,11 @@ enum Alignment {
ALIGN_RIGHT = 2, ALIGN_RIGHT = 2,
}; };
class ParagraphStyleData;
typedef BReference<ParagraphStyleData> ParagraphStyleDataRef;
// You cannot modify a ParagraphStyleData object once it has been // You cannot modify a ParagraphStyleData object once it has been
// created. // created.
class ParagraphStyleData : public BReferenceable { class ParagraphStyleData : public BReferenceable {
@ -27,27 +32,27 @@ public:
bool operator!=( bool operator!=(
const ParagraphStyleData& other) const; const ParagraphStyleData& other) const;
ParagraphStyleData* SetAlignment(::Alignment alignment); ParagraphStyleDataRef SetAlignment(::Alignment alignment);
inline ::Alignment Alignment() const inline ::Alignment Alignment() const
{ return fAlignment; } { return fAlignment; }
ParagraphStyleData* SetJustify(bool justify); ParagraphStyleDataRef SetJustify(bool justify);
inline bool Justify() const inline bool Justify() const
{ return fJustify; } { return fJustify; }
ParagraphStyleData* SetFirstLineInset(float inset); ParagraphStyleDataRef SetFirstLineInset(float inset);
inline float FirstLineInset() const inline float FirstLineInset() const
{ return fFirstLineInset; } { return fFirstLineInset; }
ParagraphStyleData* SetLineInset(float inset); ParagraphStyleDataRef SetLineInset(float inset);
inline float LineInset() const inline float LineInset() const
{ return fLineInset; } { return fLineInset; }
ParagraphStyleData* SetSpacingTop(float spacing); ParagraphStyleDataRef SetSpacingTop(float spacing);
inline float SpacingTop() const inline float SpacingTop() const
{ return fSpacingTop; } { return fSpacingTop; }
ParagraphStyleData* SetSpacingBottom(float spacing); ParagraphStyleDataRef SetSpacingBottom(float spacing);
inline float SpacingBottom() const inline float SpacingBottom() const
{ return fSpacingBottom; } { return fSpacingBottom; }
@ -66,7 +71,4 @@ private:
}; };
typedef BReference<ParagraphStyleData> ParagraphDataRef;
#endif // PARAGRAPH_STYLE_DATA_H #endif // PARAGRAPH_STYLE_DATA_H