1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "collect.hxx"
24 #define MAXCOLLECTIONSIZE 16384
27 ScDataObject::~ScDataObject()
33 static void lcl_DeleteScDataObjects( ScDataObject
** p
, sal_uInt16 nCount
)
37 for (sal_uInt16 i
= 0; i
< nCount
; i
++) delete p
[i
];
42 ScCollection::ScCollection(sal_uInt16 nLim
, sal_uInt16 nDel
) :
48 if (nDelta
> MAXDELTA
)
52 if (nLimit
> MAXCOLLECTIONSIZE
)
53 nLimit
= MAXCOLLECTIONSIZE
;
54 else if (nLimit
< nDelta
)
56 pItems
= new ScDataObject
*[nLimit
];
59 ScCollection::ScCollection(const ScCollection
& rCollection
)
69 ScCollection::~ScCollection()
71 lcl_DeleteScDataObjects( pItems
, nCount
);
74 bool ScCollection::AtInsert(sal_uInt16 nIndex
, ScDataObject
* pScDataObject
)
76 if ((nCount
< MAXCOLLECTIONSIZE
) && (nIndex
<= nCount
) && pItems
)
80 ScDataObject
** pNewItems
= new ScDataObject
*[nLimit
+ nDelta
];
83 nLimit
= sal::static_int_cast
<sal_uInt16
>( nLimit
+ nDelta
);
84 memcpy(pNewItems
, pItems
, nCount
* sizeof(ScDataObject
*));
89 memmove(&pItems
[nIndex
+ 1], &pItems
[nIndex
], (nCount
- nIndex
) * sizeof(ScDataObject
*));
90 pItems
[nIndex
] = pScDataObject
;
97 bool ScCollection::Insert(ScDataObject
* pScDataObject
)
99 return AtInsert(nCount
, pScDataObject
);
102 ScDataObject
* ScCollection::At(sal_uInt16 nIndex
) const
105 return pItems
[nIndex
];
110 ScCollection
& ScCollection::operator=( const ScCollection
& r
)
112 // Check for self-assignment
116 lcl_DeleteScDataObjects( pItems
, nCount
);
121 pItems
= new ScDataObject
*[nLimit
];
122 for ( sal_uInt16 i
=0; i
<nCount
; i
++ )
123 pItems
[i
] = r
.pItems
[i
]->Clone();
128 ScDataObject
* ScCollection::Clone() const
130 return new ScCollection(*this);
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */