summaryrefslogtreecommitdiff
path: root/sw/inc/calbck.hxx
blob: 4c76fe9fd9929387ed84048b88cb1d621904a596 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* -*- 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_CALBCK_HXX
#define INCLUDED_SW_INC_CALBCK_HXX

#include <tools/rtti.hxx>
#include "swdllapi.h"
#include <boost/noncopyable.hpp>
#include <ring.hxx>
#include <hintids.hxx>
#include <hints.hxx>
#include <typeinfo>


class SwModify;
class SwClientIter;
class SfxPoolItem;
class SfxHint;

/*
    SwModify and SwClient cooperate in propagating attribute changes.
    If an attribute changes, the change is notified to all dependent
    formats and other interested objects, e.g. Nodes. The clients will detect
    if the change affects them. It could be that the changed attribute is
    overruled in the receiving object so that its change does not become
    effective or that the receiver is not interested in the particular attribute
    in general (though probably in other attributes of the SwModify object they
    are registered in).
    As SwModify objects are derived from SwClient, they can create a chain of SwClient
    objects where changes can get propagated through.
    Each SwClient can be registered at only one SwModify object, while each SwModify
    object is connected to a list of SwClient objects. If an object derived from SwClient
    wants to get notifications from more than one SwModify object, it must create additional
    SwClient objects. The SwDepend class allows to handle their notifications in the same
    notification callback as it forwards the Modify() calls it receives to a "master"
    SwClient implementation.
    The SwClientIter class allows to iterate over the SwClient objects registered at an
    SwModify. For historical reasons its ability to use TypeInfo to restrict this iteration
    to objects of a particular type created a lot of code that misuses SwClient-SwModify
    relationships that basically should be used only for Modify() callbacks.
    This is still subject to refactoring.
    Until this gets resolved, new SwClientIter base code should be reduced to the absolute
    minimum and it also should be wrapped by SwIterator templates that prevent that the
    code gets polluted by pointer casts (see switerator.hxx).
 */

class SwModify;
class SwClient;
class SwClientIter;
template<typename E, typename S> class SwIterator;

namespace sw
{
    struct LegacyModifyHint SAL_FINAL: SfxHint
    {
        LegacyModifyHint(const SfxPoolItem* pOld, const SfxPoolItem* pNew) : m_pOld(pOld), m_pNew(pNew) {};
        const SfxPoolItem* m_pOld;
        const SfxPoolItem* m_pNew;
    };
    /// refactoring out the some of the more sane SwClient functionality
    class SW_DLLPUBLIC WriterListener : ::boost::noncopyable
    {
        friend class ::SwModify;
        friend class ::SwClient;
        friend class ::SwClientIter;
        private:
            WriterListener* m_pLeft;
            WriterListener* m_pRight; ///< double-linked list of other clients
        protected:
            WriterListener()
                : m_pLeft(nullptr), m_pRight(nullptr)
            {}
            virtual ~WriterListener() {};
            virtual void SwClientNotify( const SwModify&, const SfxHint& rHint) =0;
        public:
            bool IsLast() const { return !m_pLeft && !m_pRight; }
   };
}
// SwClient
class SW_DLLPUBLIC SwClient : ::sw::WriterListener
{
    // avoids making the details of the linked list and the callback method public
    friend class SwModify;
    friend class SwClientIter;

    SwModify *pRegisteredIn;        ///< event source

protected:
    // single argument ctors shall be explicit.
    inline explicit SwClient( SwModify* pToRegisterIn );

    // write access to pRegisteredIn shall be granted only to the object itself (protected access)
    SwModify* GetRegisteredInNonConst() const { return pRegisteredIn; }

public:

    SwClient() : pRegisteredIn(nullptr) {}
    virtual ~SwClient() SAL_OVERRIDE;
    // callbacks received from SwModify (friend class - so these methods can be private)
    // should be called only from SwModify the client is registered in
    // mba: IMHO this method should be pure virtual
    // DO NOT USE IN NEW CODE! use SwClientNotify instead.
    virtual void Modify( const SfxPoolItem* pOldValue, const SfxPoolItem* pNewValue )
        { CheckRegistration( pOldValue, pNewValue ); }
    // when overriding this, you MUST call SwClient::SwClientModify() in the override!
    virtual void SwClientNotify( const SwModify&, const SfxHint& rHint)
    {
        // assuming the compiler to realize that a dynamic_cast to a final class is just a pointer compare ...
        auto pLegacyHint(dynamic_cast<const sw::LegacyModifyHint*>(&rHint));
        if(pLegacyHint)
            Modify(pLegacyHint->m_pOld, pLegacyHint->m_pNew);
    };

    // in case an SwModify object is destroyed that itself is registered in another SwModify,
    // its SwClient objects can decide to get registered to the latter instead by calling this method
    void CheckRegistration( const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue );

    // controlled access to Modify method
    // mba: this is still considered a hack and it should be fixed; the name makes grep-ing easier
    void ModifyNotification( const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue ) { this->Modify ( pOldValue, pNewValue ); }
    void SwClientNotifyCall( const SwModify& rModify, const SfxHint& rHint ) { SwClientNotify( rModify, rHint ); }

    const SwModify* GetRegisteredIn() const { return pRegisteredIn; }
    SwModify* GetRegisteredIn() { return pRegisteredIn; }

    // needed for class SwClientIter
    TYPEINFO();

    // get information about attribute
    virtual bool GetInfo( SfxPoolItem& ) const { return true; }
};


// SwModify

// class has a doubly linked list for dependencies
class SW_DLLPUBLIC SwModify: public SwClient
{
    sw::WriterListener* pRoot;                // the start of the linked list of clients
    bool bModifyLocked : 1;         // don't broadcast changes now
    bool bLockClientList : 1;       // may be set when this instance notifies its clients
    bool bInDocDTOR : 1;            // workaround for problems when a lot of objects are destroyed
    bool bInCache   : 1;
    bool bInSwFntCache : 1;

    // mba: IMHO this method should be pure virtual
    // DO NOT USE IN NEW CODE! use CallSwClientNotify instead.
    virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew) SAL_OVERRIDE
        { NotifyClients( pOld, pNew ); };

public:
    SwModify()
        : SwClient(nullptr), pRoot(nullptr), bModifyLocked(false), bLockClientList(false), bInDocDTOR(false), bInCache(false), bInSwFntCache(false)
    {}
    explicit SwModify( SwModify* pToRegisterIn )
        : SwClient(pToRegisterIn), pRoot(nullptr), bModifyLocked(false), bLockClientList(false), bInDocDTOR(false), bInCache(false), bInSwFntCache(false)
    {}

    // broadcasting: send notifications to all clients
    // DO NOT USE IN NEW CODE! use CallSwClientNotify instead.
    void NotifyClients( const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue );
    // DO NOT USE IN NEW CODE! use CallSwClientNotify instead.
    void ModifyBroadcast( const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue)
        { CallSwClientNotify( sw::LegacyModifyHint{ pOldValue, pNewValue } ); };
    // the same, but without setting bModifyLocked or checking for any of the flags
    // mba: it would be interesting to know why this is necessary
    // also allows to limit callback to certain type (HACK)
    // DO NOT USE IN NEW CODE! use CallSwClientNotify instead.
    inline void ModifyBroadcast( const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue, TypeId nType );

    // a more universal broadcasting mechanism
    inline void CallSwClientNotify( const SfxHint& rHint ) const;

    virtual ~SwModify();

    void Add(SwClient *pDepend);
    SwClient* Remove(SwClient *pDepend);
    const SwClient* GetDepends() const  { return static_cast<SwClient*>(pRoot); }

    // get information about attribute
    virtual bool GetInfo( SfxPoolItem& ) const SAL_OVERRIDE;

    void LockModify()                   { bModifyLocked = true;  }
    void UnlockModify()                 { bModifyLocked = false; }
    void SetInCache( bool bNew )        { bInCache = bNew;       }
    void SetInSwFntCache( bool bNew )   { bInSwFntCache = bNew;  }
    void SetInDocDTOR()                 { bInDocDTOR = true; }
    bool IsModifyLocked() const     { return bModifyLocked;  }
    bool IsInDocDTOR()    const     { return bInDocDTOR;     }
    bool IsInCache()      const     { return bInCache;       }
    bool IsInSwFntCache() const     { return bInSwFntCache;  }

    void CheckCaching( const sal_uInt16 nWhich );
    bool IsLastDepend() { return pRoot && pRoot->IsLast(); }
};

// SwDepend

/*
 * Helper class for objects that need to depend on more than one SwClient
 */
class SW_DLLPUBLIC SwDepend SAL_FINAL : public SwClient
{
    SwClient *m_pToTell;

public:
    SwDepend() : m_pToTell(nullptr) {}
    SwDepend(SwClient *pTellHim, SwModify *pDepend) : SwClient(pDepend), m_pToTell(pTellHim) {}

    SwClient* GetToTell() { return m_pToTell; }

    /** get Client information */
    virtual bool GetInfo( SfxPoolItem& rInfo) const SAL_OVERRIDE
        { return m_pToTell ? m_pToTell->GetInfo( rInfo ) : true; }
protected:
    virtual void Modify( const SfxPoolItem* pOldValue, const SfxPoolItem *pNewValue ) SAL_OVERRIDE
    {
        if( pNewValue && pNewValue->Which() == RES_OBJECTDYING )
            CheckRegistration(pOldValue,pNewValue);
        else if( m_pToTell )
            m_pToTell->ModifyNotification(pOldValue, pNewValue);
    }
    virtual void SwClientNotify( const SwModify& rModify, const SfxHint& rHint ) SAL_OVERRIDE
        { if(m_pToTell) m_pToTell->SwClientNotifyCall(rModify, rHint); }
};

class SwClientIter SAL_FINAL : public sw::Ring<SwClientIter>
{
    friend SwClient* SwModify::Remove(SwClient*); ///< for pointer adjustments
    friend void SwModify::Add(SwClient*);   ///< for pointer adjustments
    template<typename E, typename S> friend class SwIterator; ///< for typed interation
    friend void SwModify::ModifyBroadcast( const SfxPoolItem*, const SfxPoolItem*, TypeId); ///< for typed iteration

    const SwModify& m_rRoot;

    // the current object in an iteration
    SwClient* m_pCurrent;

    // in case the current object is already removed, the next object in the list
    // is marked down to become the current object in the next step
    // this is necessary because iteration requires access to members of the current object
    SwClient* m_pPosition;
    SwClient* GetLeftOfPos() { return static_cast<SwClient*>(m_pPosition->m_pLeft); }
    SwClient* GetRighOfPos() { return static_cast<SwClient*>(m_pPosition->m_pRight); }

    // iterator can be limited to return only SwClient objects of a certain type
    TypeId m_aSearchType;

    static SW_DLLPUBLIC SwClientIter* our_pClientIters;

public:
    SwClientIter( const SwModify& rModify )
        : m_rRoot(rModify)
        , m_aSearchType(nullptr)
    {
        MoveTo(our_pClientIters);
        our_pClientIters = this;
        m_pCurrent = m_pPosition = const_cast<SwClient*>(m_rRoot.GetDepends());
    }
    ~SwClientIter() SAL_OVERRIDE
    {
        assert(our_pClientIters);
        if(our_pClientIters == this)
            our_pClientIters = unique() ? nullptr : GetNextInRing();
        MoveTo(nullptr);
    }

    const SwModify& GetModify() const { return m_rRoot; }

    SwClient* operator++()
    {
        if( m_pPosition == m_pCurrent )
            m_pPosition = static_cast<SwClient*>(m_pPosition->m_pRight);
        return m_pCurrent = m_pPosition;
    }
    SwClient* GoStart()
    {
        if((m_pPosition = const_cast<SwClient*>(m_rRoot.GetDepends())))
            while( m_pPosition->m_pLeft )
                m_pPosition = static_cast<SwClient*>(m_pPosition->m_pLeft);
        return m_pCurrent = m_pPosition;
    }
    SwClient* GoEnd()
    {
        if(!m_pPosition)
            m_pPosition = const_cast<SwClient*>(m_rRoot.GetDepends());
        if(m_pPosition)
            while( m_pPosition->m_pRight )
                m_pPosition = static_cast<SwClient*>(m_pPosition->m_pRight);
        return m_pCurrent = m_pPosition;
    }

    // returns the current SwClient object, wether it is still a client or not
    SwClient& operator*() const
        { return *m_pCurrent; }
    // returns the current SwClient object, wether it is still a client or not
    SwClient* operator->() const
        { return m_pCurrent; }
    explicit operator bool() const
        { return m_pCurrent!=nullptr; }

    // return "true" if an object was removed from a client chain in iteration
    // adding objects to a client chain in iteration is forbidden
    // SwModify::Add() asserts this
    bool IsChanged() const { return m_pPosition != m_pCurrent; }
};

SwClient::SwClient( SwModify* pToRegisterIn )
    : pRegisteredIn( nullptr )
{
    if(pToRegisterIn)
        pToRegisterIn->Add(this);
}

void SwModify::ModifyBroadcast( const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue, TypeId nType)
{
    SwClientIter aIter(*this);
    aIter.GoStart();
    while(aIter)
    {
        if( aIter.m_pPosition == aIter.m_pCurrent )
            aIter.m_pPosition = static_cast<SwClient*>(aIter.m_pPosition->m_pRight);
        while(aIter.m_pPosition && !aIter.m_pPosition->IsA( nType ) )
            aIter.m_pPosition = static_cast<SwClient*>(aIter.m_pPosition->m_pRight);
        aIter.m_pCurrent = aIter.m_pPosition;
        aIter->Modify( pOldValue, pNewValue );
    }
}

void SwModify::CallSwClientNotify( const SfxHint& rHint ) const
{
    for(SwClientIter aIter(*this); aIter; ++aIter)
        aIter->SwClientNotify( *this, rHint );
}
#endif

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