From 9192e5de7dd52f3864d4870569458b2d9226f2a0 Mon Sep 17 00:00:00 2001 From: David Tardon Date: Fri, 24 Dec 2010 12:33:33 +0100 Subject: [PATCH] make copying aware of NULL pointers --- lotuswordpro/source/filter/clone.hxx | 82 +++++++++++++++++++ .../source/filter/lwpbreaksoverride.cxx | 3 +- .../source/filter/lwpcharborderoverride.cxx | 5 +- lotuswordpro/source/filter/lwpoverride.cxx | 12 +-- .../source/filter/lwpparaborderoverride.cxx | 9 +- 5 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 lotuswordpro/source/filter/clone.hxx diff --git a/lotuswordpro/source/filter/clone.hxx b/lotuswordpro/source/filter/clone.hxx new file mode 100644 index 000000000000..a210a8308184 --- /dev/null +++ b/lotuswordpro/source/filter/clone.hxx @@ -0,0 +1,82 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Version: MPL 1.1 / GPLv3+ / LGPLv3+ + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Initial Developer of the Original Code is + * Red Hat, Inc. + * Portions created by the Initial Developer are Copyright (C) 2010 the + * Initial Developer. All Rights Reserved. + * + * Contributor(s): David Tardon + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 3 or later (the "GPLv3+"), or + * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"), + * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable + * instead of those above. + */ + +namespace detail +{ + +template +struct has_clone +{ + template + struct test; + + typedef char yes; + typedef struct { char a[2]; } no; + + template + static yes check_sig(U*, test* = 0); + template + static no check_sig(...); + + static bool const value = sizeof(has_clone::template check_sig(0)) == sizeof(yes); +}; + +template +struct cloner +{ + static T* clone(T* const other) + { + return new T(*other); + } +}; + +template +struct cloner +{ + static T* clone(T* const other) + { + return other->clone(); + } +}; + +} + +/** Creates a new copy of the passed object. + If other is 0, just returns 0. Otherwise, if other has function + named clone with signature T* (T::*)() const, the function is called. + Otherwise, copy constructor is used. + + @returns 0 or newly allocated object + */ +template +T* clone(T* const other) +{ + return other ? detail::cloner::value>::clone(other) : 0; +} + +// vim: set sts=4 sw=4 et: diff --git a/lotuswordpro/source/filter/lwpbreaksoverride.cxx b/lotuswordpro/source/filter/lwpbreaksoverride.cxx index 38111c229bb9..f6c8f756321f 100644 --- a/lotuswordpro/source/filter/lwpbreaksoverride.cxx +++ b/lotuswordpro/source/filter/lwpbreaksoverride.cxx @@ -64,6 +64,7 @@ #include +#include "clone.hxx" #include "lwpbreaksoverride.hxx" #include "lwpobjstrm.hxx" #include "lwpatomholder.hxx" @@ -78,7 +79,7 @@ LwpBreaksOverride::LwpBreaksOverride(LwpBreaksOverride const& rOther) : LwpOverride(rOther) , m_pNextStyle(0) { - std::auto_ptr pNextStyle(new LwpAtomHolder(*rOther.m_pNextStyle)); + std::auto_ptr pNextStyle(::clone(rOther.m_pNextStyle)); m_pNextStyle = pNextStyle.release(); } diff --git a/lotuswordpro/source/filter/lwpcharborderoverride.cxx b/lotuswordpro/source/filter/lwpcharborderoverride.cxx index 927d1dd78b03..b654afdf6f58 100644 --- a/lotuswordpro/source/filter/lwpcharborderoverride.cxx +++ b/lotuswordpro/source/filter/lwpcharborderoverride.cxx @@ -64,6 +64,7 @@ #include +#include "clone.hxx" #include "lwpcharborderoverride.hxx" #include "lwpborderstuff.hxx" #include "lwpmargins.hxx" @@ -80,8 +81,8 @@ LwpCharacterBorderOverride::LwpCharacterBorderOverride(LwpCharacterBorderOverrid , m_nAboveWidth(rOther.m_nAboveWidth) , m_nBelowWidth(rOther.m_nBelowWidth) { - std::auto_ptr pBorderStuff(new LwpBorderStuff(*rOther.m_pBorderStuff)); - std::auto_ptr pMargins(new LwpMargins(*rOther.m_pMargins)); + std::auto_ptr pBorderStuff(::clone(rOther.m_pBorderStuff)); + std::auto_ptr pMargins(::clone(rOther.m_pMargins)); m_pBorderStuff = pBorderStuff.release(); m_pMargins = pMargins.release(); } diff --git a/lotuswordpro/source/filter/lwpoverride.cxx b/lotuswordpro/source/filter/lwpoverride.cxx index 42e0b5d5599e..af46e8dd16ed 100644 --- a/lotuswordpro/source/filter/lwpoverride.cxx +++ b/lotuswordpro/source/filter/lwpoverride.cxx @@ -64,6 +64,7 @@ #include +#include "clone.hxx" #include "lwpoverride.hxx" #include "lwpfilehdr.hxx" #include "lwpatomholder.hxx" @@ -387,10 +388,10 @@ LwpSpacingOverride::LwpSpacingOverride(LwpSpacingOverride const& rOther) , m_pParaSpacingAbove(0) , m_pParaSpacingBelow(0) { - std::auto_ptr pSpacing(rOther.m_pSpacing->clone()); - std::auto_ptr pAboveLineSpacing(rOther.m_pAboveLineSpacing->clone()); - std::auto_ptr pParaSpacingAbove(rOther.m_pParaSpacingAbove->clone()); - std::auto_ptr pParaSpacingBelow(rOther.m_pParaSpacingBelow->clone()); + std::auto_ptr pSpacing(::clone(rOther.m_pSpacing)); + std::auto_ptr pAboveLineSpacing(::clone(rOther.m_pAboveLineSpacing)); + std::auto_ptr pParaSpacingAbove(::clone(rOther.m_pParaSpacingAbove)); + std::auto_ptr pParaSpacingBelow(::clone(rOther.m_pParaSpacingBelow)); m_pSpacing = pSpacing.release(); m_pAboveLineSpacing = pAboveLineSpacing.release(); m_pParaSpacingAbove = pParaSpacingAbove.release(); @@ -469,8 +470,7 @@ LwpAmikakeOverride::LwpAmikakeOverride(LwpAmikakeOverride const& rOther) , m_pBackgroundStuff(0) , m_nType(rOther.m_nType) { - std::auto_ptr pBackgroundStuff( - new LwpBackgroundStuff(*rOther.m_pBackgroundStuff)); + std::auto_ptr pBackgroundStuff(::clone(rOther.m_pBackgroundStuff)); m_pBackgroundStuff = pBackgroundStuff.release(); } diff --git a/lotuswordpro/source/filter/lwpparaborderoverride.cxx b/lotuswordpro/source/filter/lwpparaborderoverride.cxx index 51b831f6fe37..b36b432eeddf 100644 --- a/lotuswordpro/source/filter/lwpparaborderoverride.cxx +++ b/lotuswordpro/source/filter/lwpparaborderoverride.cxx @@ -64,6 +64,7 @@ #include +#include "clone.hxx" #include "lwpparaborderoverride.hxx" #include "lwpborderstuff.hxx" #include "lwpshadow.hxx" @@ -105,10 +106,10 @@ LwpParaBorderOverride::LwpParaBorderOverride(LwpParaBorderOverride const& rOther , m_nRightWidth(rOther.m_nRightWidth) , m_nBetweenMargin(rOther.m_nBetweenMargin) { - std::auto_ptr pBorderStuff(new LwpBorderStuff(*rOther.m_pBorderStuff)); - std::auto_ptr pBetweenStuff(new LwpBorderStuff(*rOther.m_pBetweenStuff)); - std::auto_ptr pShadow(new LwpShadow(*rOther.m_pShadow)); - std::auto_ptr pMargins(new LwpMargins(*rOther.m_pMargins)); + std::auto_ptr pBorderStuff(::clone(rOther.m_pBorderStuff)); + std::auto_ptr pBetweenStuff(::clone(rOther.m_pBetweenStuff)); + std::auto_ptr pShadow(::clone(rOther.m_pShadow)); + std::auto_ptr pMargins(::clone(rOther.m_pMargins)); m_pBorderStuff = pBorderStuff.release(); m_pBetweenStuff = pBetweenStuff.release(); m_pShadow = pShadow.release();