summaryrefslogtreecommitdiff
path: root/sw/inc/ring.hxx
blob: 33bc069bdfba0f758b6c551cfaab269800fc1ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
 */
#ifndef INCLUDED_SW_INC_RING_HXX
#define INCLUDED_SW_INC_RING_HXX

#include <swdllapi.h>
#include <swtypes.hxx>
#include <utility>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/intrusive/circular_list_algorithms.hpp>

namespace sw
{
    class Ring_node_traits;
    template <class T> class RingIterator;

    template <class T>
    class Ring
    {
        struct Ring_node_traits
        {
            typedef T node;
            typedef T* node_ptr;
            typedef const T* const_node_ptr;
            static node_ptr get_next(const_node_ptr n) { return n->GetNext(); };
            static void set_next(node_ptr n, node_ptr next) { n->pNext = next; };
            static node_ptr get_previous(const_node_ptr n) { return n->GetPrev(); };
            static void set_previous(node_ptr n, node_ptr previous) { n->pPrev = previous; };
        };
        friend struct Ring_node_traits;
        typedef boost::intrusive::circular_list_algorithms<Ring_node_traits> algo;
        T* pNext;
        T* pPrev;    ///< In order to speed up inserting and deleting.

    protected:
        Ring()
            { algo::init_header(static_cast< T* >(this)); }
        Ring( T* );
    public:
        typedef RingIterator<T> iterator;
        typedef RingIterator<const T> const_iterator;
        virtual ~Ring()
            { algo::unlink(static_cast< T* >(this)); };
        void MoveTo( T* pDestRing );
        void MoveRingTo( T* pDestRing );

        T* GetNext() const       { return pNext; }
        T* GetPrev() const       { return pPrev; }
        // unfortunately we cant name these STL-conforming, as some derived classes
        // also derive from other STL containers (which is bad anyway, but ...)
        iterator beginRing();
        iterator endRing();
        const_iterator beginRing() const;
        const_iterator endRing() const;
        std::pair<iterator, iterator> rangeRing()
            { return std::make_pair(beginRing(), endRing()); }
        std::pair<const_iterator, const_iterator> rangeRing() const
            { return std::make_pair(beginRing(), endRing()); }

        sal_uInt32 numberOf() const
            { return algo::count(static_cast< const T* >(this)); }
    };

    template <class T>
    inline Ring<T>::Ring( T* pObj )
    {
        T* pThis = static_cast< T* >(this);
        if( !pObj )
            algo::init_header(pThis);
        else
            algo::link_before(pObj, pThis);
    }

    template <class T>
    inline void Ring<T>::MoveTo(T* pDestRing)
    {
        T* pThis = static_cast< T* >(this);
        // insert into "new"
        if( pDestRing )
        {
            if(algo::unique(pThis))
                algo::link_before(pDestRing, pThis);
            else
                algo::transfer(pDestRing, pThis);
        }
        else
            algo::unlink(pThis);
    }

    template <class T>
    inline void Ring<T>::MoveRingTo(T* pDestRing)
    {
        std::swap(*(&pPrev->pNext), *(&pDestRing->pPrev->pNext));
        std::swap(*(&pPrev), *(&pDestRing->pPrev));
    }

    template <class T>
    class RingIterator : public boost::iterator_facade<
          RingIterator<T>
        , T
        , boost::forward_traversal_tag
        >
    {
        public:
            RingIterator()
                : m_pCurrent(nullptr)
                , m_pStart(nullptr)
            {}
            explicit RingIterator(T* pRing, bool bStart = true)
                : m_pCurrent(nullptr)
                , m_pStart(pRing)
            {
                if(!bStart)
                    m_pCurrent = m_pStart;
            }
        private:
            friend class boost::iterator_core_access;
            void increment()
                { m_pCurrent = m_pCurrent ? m_pCurrent->GetNext() : m_pStart->GetNext(); }
            bool equal(RingIterator const& other) const
                { return m_pCurrent == other.m_pCurrent && m_pStart == m_pStart; }
            T& dereference() const
                { return m_pCurrent ? *m_pCurrent : * m_pStart; }
            T* m_pCurrent;
            T* m_pStart;
    };

    template <class T>
    inline typename Ring<T>::iterator Ring<T>::beginRing()
        { return Ring<T>::iterator(static_cast< T* >(this)); };

    template <class T>
    inline typename Ring<T>::iterator Ring<T>::endRing()
        { return Ring<T>::iterator(static_cast< T* >(this), false); };

    template <class T>
    inline typename Ring<T>::const_iterator Ring<T>::beginRing() const
        { return Ring<T>::const_iterator(static_cast< const T* >(this)); };

    template <class T>
    inline typename Ring<T>::const_iterator Ring<T>::endRing() const
        { return Ring<T>::const_iterator(static_cast< const T* >(this), false); };
}
#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */