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
ParagraphStyle::SetAlignment(::Alignment alignment)
{
fStyleData.SetTo(fStyleData->SetAlignment(alignment));
return fStyleData->Alignment() == alignment;
ParagraphStyleDataRef data = fStyleData->SetAlignment(alignment);
if (data == fStyleData)
return data->Alignment() == alignment;
fStyleData = data;
return true;
}
bool
ParagraphStyle::SetJustify(bool justify)
{
fStyleData.SetTo(fStyleData->SetJustify(justify));
return fStyleData->Justify() == justify;
ParagraphStyleDataRef data = fStyleData->SetJustify(justify);
if (data == fStyleData)
return data->Justify() == justify;
fStyleData = data;
return true;
}
bool
ParagraphStyle::SetFirstLineInset(float inset)
{
fStyleData.SetTo(fStyleData->SetFirstLineInset(inset));
return fStyleData->FirstLineInset() == inset;
ParagraphStyleDataRef data = fStyleData->SetFirstLineInset(inset);
if (data == fStyleData)
return data->FirstLineInset() == inset;
fStyleData = data;
return true;
}
bool
ParagraphStyle::SetLineInset(float inset)
{
fStyleData.SetTo(fStyleData->SetLineInset(inset));
return fStyleData->LineInset() == inset;
ParagraphStyleDataRef data = fStyleData->SetLineInset(inset);
if (data == fStyleData)
return data->LineInset() == inset;
fStyleData = data;
return true;
}
bool
ParagraphStyle::SetSpacingTop(float spacing)
{
fStyleData.SetTo(fStyleData->SetSpacingTop(spacing));
return fStyleData->SpacingTop() == spacing;
ParagraphStyleDataRef data = fStyleData->SetSpacingTop(spacing);
if (data == fStyleData)
return data->SpacingTop() == spacing;
fStyleData = data;
return true;
}
bool
ParagraphStyle::SetSpacingBottom(float spacing)
{
fStyleData.SetTo(fStyleData->SetSpacingBottom(spacing));
return fStyleData->SpacingBottom() == spacing;
ParagraphStyleDataRef data = fStyleData->SetSpacingBottom(spacing);
if (data == fStyleData)
return data->SpacingBottom() == spacing;
fStyleData = data;
return true;
}

View File

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

View File

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

View File

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