Kaydet (Commit) 92261a33 authored tarafından Thomas Arnhold's avatar Thomas Arnhold

fdo#62525: use cow_wrapper for StrokeAttribute

Change-Id: Icf5daca279902b90da98069338638c3ad432e69a
üst 8ee042bd
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
#define INCLUDED_DRAWINGLAYER_ATTRIBUTE_STROKEATTRIBUTE_HXX #define INCLUDED_DRAWINGLAYER_ATTRIBUTE_STROKEATTRIBUTE_HXX
#include <drawinglayer/drawinglayerdllapi.h> #include <drawinglayer/drawinglayerdllapi.h>
#include <o3tl/cow_wrapper.hxx>
#include <vector> #include <vector>
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
...@@ -39,8 +39,11 @@ namespace drawinglayer ...@@ -39,8 +39,11 @@ namespace drawinglayer
{ {
class DRAWINGLAYER_DLLPUBLIC StrokeAttribute class DRAWINGLAYER_DLLPUBLIC StrokeAttribute
{ {
public:
typedef o3tl::cow_wrapper< ImpStrokeAttribute > ImplType;
private: private:
ImpStrokeAttribute* mpStrokeAttribute; ImplType mpStrokeAttribute;
public: public:
/// constructors/assignmentoperator/destructor /// constructors/assignmentoperator/destructor
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
*/ */
#include <drawinglayer/attribute/strokeattribute.hxx> #include <drawinglayer/attribute/strokeattribute.hxx>
#include <rtl/instance.hxx>
#include <numeric> #include <numeric>
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
...@@ -29,9 +30,6 @@ namespace drawinglayer ...@@ -29,9 +30,6 @@ namespace drawinglayer
class ImpStrokeAttribute class ImpStrokeAttribute
{ {
public: public:
// refcounter
sal_uInt32 mnRefCount;
// data definitions // data definitions
::std::vector< double > maDotDashArray; // array of double which defines the dot-dash pattern ::std::vector< double > maDotDashArray; // array of double which defines the dot-dash pattern
double mfFullDotDashLen; // sum of maDotDashArray (for convenience) double mfFullDotDashLen; // sum of maDotDashArray (for convenience)
...@@ -39,12 +37,17 @@ namespace drawinglayer ...@@ -39,12 +37,17 @@ namespace drawinglayer
ImpStrokeAttribute( ImpStrokeAttribute(
const ::std::vector< double >& rDotDashArray, const ::std::vector< double >& rDotDashArray,
double fFullDotDashLen) double fFullDotDashLen)
: mnRefCount(0), : maDotDashArray(rDotDashArray),
maDotDashArray(rDotDashArray),
mfFullDotDashLen(fFullDotDashLen) mfFullDotDashLen(fFullDotDashLen)
{ {
} }
ImpStrokeAttribute()
: maDotDashArray(std::vector< double >()),
mfFullDotDashLen(0.0)
{
}
// data read access // data read access
const ::std::vector< double >& getDotDashArray() const { return maDotDashArray; } const ::std::vector< double >& getDotDashArray() const { return maDotDashArray; }
double getFullDotDashLen() const double getFullDotDashLen() const
...@@ -64,95 +67,50 @@ namespace drawinglayer ...@@ -64,95 +67,50 @@ namespace drawinglayer
return (getDotDashArray() == rCandidate.getDotDashArray() return (getDotDashArray() == rCandidate.getDotDashArray()
&& getFullDotDashLen() == rCandidate.getFullDotDashLen()); && getFullDotDashLen() == rCandidate.getFullDotDashLen());
} }
static ImpStrokeAttribute* get_global_default()
{
static ImpStrokeAttribute* pDefault = 0;
if(!pDefault)
{
pDefault = new ImpStrokeAttribute(
std::vector< double >(),
0.0);
// never delete; start with RefCount 1, not 0
pDefault->mnRefCount++;
}
return pDefault;
}
}; };
namespace
{
struct theGlobalDefault :
public rtl::Static< StrokeAttribute::ImplType, theGlobalDefault > {};
}
StrokeAttribute::StrokeAttribute( StrokeAttribute::StrokeAttribute(
const ::std::vector< double >& rDotDashArray, const ::std::vector< double >& rDotDashArray,
double fFullDotDashLen) double fFullDotDashLen)
: mpStrokeAttribute(new ImpStrokeAttribute( : mpStrokeAttribute(ImpStrokeAttribute(
rDotDashArray, fFullDotDashLen)) rDotDashArray, fFullDotDashLen))
{ {
} }
StrokeAttribute::StrokeAttribute() StrokeAttribute::StrokeAttribute()
: mpStrokeAttribute(ImpStrokeAttribute::get_global_default()) : mpStrokeAttribute(theGlobalDefault::get())
{ {
mpStrokeAttribute->mnRefCount++;
} }
StrokeAttribute::StrokeAttribute(const StrokeAttribute& rCandidate) StrokeAttribute::StrokeAttribute(const StrokeAttribute& rCandidate)
: mpStrokeAttribute(rCandidate.mpStrokeAttribute) : mpStrokeAttribute(rCandidate.mpStrokeAttribute)
{ {
mpStrokeAttribute->mnRefCount++;
} }
StrokeAttribute::~StrokeAttribute() StrokeAttribute::~StrokeAttribute()
{ {
if(mpStrokeAttribute->mnRefCount)
{
mpStrokeAttribute->mnRefCount--;
}
else
{
delete mpStrokeAttribute;
}
} }
bool StrokeAttribute::isDefault() const bool StrokeAttribute::isDefault() const
{ {
return mpStrokeAttribute == ImpStrokeAttribute::get_global_default(); return mpStrokeAttribute.same_object(theGlobalDefault::get());
} }
StrokeAttribute& StrokeAttribute::operator=(const StrokeAttribute& rCandidate) StrokeAttribute& StrokeAttribute::operator=(const StrokeAttribute& rCandidate)
{ {
if(rCandidate.mpStrokeAttribute != mpStrokeAttribute) mpStrokeAttribute = rCandidate.mpStrokeAttribute;
{
if(mpStrokeAttribute->mnRefCount)
{
mpStrokeAttribute->mnRefCount--;
}
else
{
delete mpStrokeAttribute;
}
mpStrokeAttribute = rCandidate.mpStrokeAttribute;
mpStrokeAttribute->mnRefCount++;
}
return *this; return *this;
} }
bool StrokeAttribute::operator==(const StrokeAttribute& rCandidate) const bool StrokeAttribute::operator==(const StrokeAttribute& rCandidate) const
{ {
if(rCandidate.mpStrokeAttribute == mpStrokeAttribute) return rCandidate.mpStrokeAttribute == mpStrokeAttribute;
{
return true;
}
if(rCandidate.isDefault() != isDefault())
{
return false;
}
return (*rCandidate.mpStrokeAttribute == *mpStrokeAttribute);
} }
const ::std::vector< double >& StrokeAttribute::getDotDashArray() const const ::std::vector< double >& StrokeAttribute::getDotDashArray() const
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment