LibreOffice Module sfx2 (master) 1
nochaos.cxx
Go to the documentation of this file.
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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <svl/itempool.hxx>
23#include <svl/poolitem.hxx>
24#include <svl/stritem.hxx>
25#include <nochaos.hxx>
26#include <memory>
27
28
29#define WID_CHAOS_START TypedWhichId<SfxStringItem>(500)
30
31
32namespace {
33
34class CntItemPool;
35
36class CntStaticPoolDefaults_Impl
37{
38 static const sal_uInt32 m_nItems = 1;
39 std::vector<SfxPoolItem*> mvDefaults;
40 std::unique_ptr<SfxItemInfo[]> m_pItemInfos;
41
42private:
43 inline void Insert( SfxPoolItem* pItem );
44
45public:
46 explicit CntStaticPoolDefaults_Impl();
47 ~CntStaticPoolDefaults_Impl();
48 CntStaticPoolDefaults_Impl(const CntStaticPoolDefaults_Impl&) = delete;
49 CntStaticPoolDefaults_Impl& operator=(const CntStaticPoolDefaults_Impl&) = delete;
50
51 std::vector<SfxPoolItem*>* GetDefaults() { return &mvDefaults; }
52 const SfxItemInfo* GetItemInfos() const { return m_pItemInfos.get(); }
53};
54
55
56class CntItemPool: public SfxItemPool
57{
58 static CntItemPool* _pThePool;
59 sal_uInt16 _nRefs;
60
61protected:
62 CntItemPool();
63 virtual ~CntItemPool() override;
64
65public:
66 static CntItemPool* Acquire();
67 static sal_uInt16 Release();
68};
69
70}
71
72// static
74{
75 // Get and hold CHAOS item pool.
76 return CntItemPool::Acquire();
77}
78
79
80// static
82{
83 // Release CHAOS item pool.
84 return CntItemPool::Release();
85}
86
87
88// CntItemPool implementation
89
90
91static CntStaticPoolDefaults_Impl* pPoolDefs_Impl = nullptr;
92
93// static member!
94CntItemPool* CntItemPool::_pThePool = nullptr;
95
96
97CntItemPool::CntItemPool()
98: SfxItemPool( "chaos", WID_CHAOS_START, WID_CHAOS_START, nullptr ),
99 _nRefs( 0 )
100{
101 FreezeIdRanges();
102
103 // Create static defaults.
104 pPoolDefs_Impl = new CntStaticPoolDefaults_Impl;
105
106 // Set item infos.
107 SetItemInfos( pPoolDefs_Impl->GetItemInfos() );
108
109 // Set static pool default items.
110 SetDefaults( pPoolDefs_Impl->GetDefaults() );
111}
112
113
114//virtual
115CntItemPool::~CntItemPool()
116{
117 // Release static pool default items.
118 ReleaseDefaults();
119}
120
121
122// static
123CntItemPool* CntItemPool::Acquire()
124{
125 if ( !_pThePool )
126 _pThePool = new CntItemPool;
127
128 _pThePool->_nRefs++;
129
130 return _pThePool;
131}
132
133
134// static
135sal_uInt16 CntItemPool::Release()
136{
137 if ( !_pThePool )
138 return 0;
139
140 sal_uInt16& nRefs = _pThePool->_nRefs;
141
142 if ( nRefs )
143 --nRefs;
144
145 if ( !nRefs )
146 {
147 delete _pThePool;
148 _pThePool = nullptr;
149 delete pPoolDefs_Impl;
150 pPoolDefs_Impl = nullptr;
151 return 0;
152 }
153
154 return nRefs;
155}
156
157
158// CntStaticPoolDefaults_Impl implementation.
159
160
161inline void CntStaticPoolDefaults_Impl::Insert(
162 SfxPoolItem* pItem /* Static Pool Default Item */ )
163{
164 sal_uInt16 nPos = pItem->Which() - WID_CHAOS_START;
165
166 mvDefaults[ nPos ] = pItem;
167 m_pItemInfos[ nPos ]._nSID = 0;
168 m_pItemInfos[ nPos ]._bPoolable = true;
169}
170
171
172CntStaticPoolDefaults_Impl::~CntStaticPoolDefaults_Impl()
173{
174 for ( sal_uInt32 n = 0; n < m_nItems; ++n )
175 delete mvDefaults[ n ];
176}
177
178
179CntStaticPoolDefaults_Impl::CntStaticPoolDefaults_Impl()
180: mvDefaults( m_nItems, nullptr ),
181 m_pItemInfos( new SfxItemInfo [ m_nItems ] )
182{
183 memset( m_pItemInfos.get(), 0, sizeof( SfxItemInfo ) * m_nItems );
184 Insert( new SfxStringItem( WID_CHAOS_START, OUString() ) );
185}
186
187/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static SfxItemPool * GetItemPool()
Definition: nochaos.cxx:73
static sal_uInt16 ReleaseItemPool()
Definition: nochaos.cxx:81
sal_uInt16 Which() const
virtual void Insert(SotClipboardFormatId nFormat, const OUString &rFormatName) override
sal_Int64 n
sal_uInt16 nPos
Definition: linksrc.cxx:118
#define WID_CHAOS_START
Definition: nochaos.cxx:29
static CntStaticPoolDefaults_Impl * pPoolDefs_Impl
Definition: nochaos.cxx:91