Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : */
9 :
10 : #ifndef __SC_STLALGORITHM_HXX__
11 : #define __SC_STLALGORITHM_HXX__
12 :
13 : #include <functional>
14 : #include <limits>
15 :
16 : #include <rtl/alloc.h>
17 :
18 : /**
19 : * Function object to allow deleting instances stored in STL containers as
20 : * pointers.
21 : */
22 : template<typename T>
23 : struct ScDeleteObjectByPtr : public ::std::unary_function<T*, void>
24 : {
25 0 : void operator() (T* p)
26 : {
27 0 : delete p;
28 0 : }
29 : };
30 :
31 : namespace sc {
32 :
33 : /**
34 : * Custom allocator for STL container to ensure that the base address of
35 : * allocated storage is aligned to a specified boundary.
36 : */
37 : template<typename T, size_t _Alignment>
38 : class AlignedAllocator
39 : {
40 : public:
41 : typedef T value_type;
42 : typedef size_t size_type;
43 : typedef std::ptrdiff_t difference_type;
44 :
45 : typedef T* pointer;
46 : typedef const T* const_pointer;
47 : typedef T* void_pointer;
48 :
49 : typedef T& reference;
50 : typedef const T& const_reference;
51 :
52 : template<typename _Type2>
53 : struct rebind
54 : {
55 : typedef AlignedAllocator<_Type2,_Alignment> other;
56 : };
57 :
58 0 : AlignedAllocator() {}
59 0 : ~AlignedAllocator() {}
60 :
61 : template<typename _Type2>
62 : AlignedAllocator(const AlignedAllocator<_Type2,_Alignment>&) {}
63 :
64 0 : void construct(T* p, const value_type& val) { new(p) value_type(val); }
65 0 : void destroy(T* p)
66 : {
67 : p->~value_type();
68 : (void)p; // avoid bogus MSVC '12 "unreferenced formal parameter" warning
69 0 : }
70 :
71 0 : size_type max_size() const
72 : {
73 0 : return std::numeric_limits<size_type>::max() / sizeof(value_type);
74 : }
75 :
76 : bool operator== (const AlignedAllocator&) const { return true; }
77 : bool operator!= (const AlignedAllocator&) const { return false; }
78 :
79 0 : pointer allocate(size_type n)
80 : {
81 0 : return (pointer)rtl_allocateAlignedMemory(_Alignment, n*sizeof(value_type));
82 : }
83 :
84 0 : void deallocate(pointer p, size_type)
85 : {
86 0 : rtl_freeAlignedMemory(p);
87 0 : }
88 : };
89 :
90 : }
91 :
92 : #endif
93 :
94 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|