Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / explorer / nochaos.cxx
blob33f219e3247e4ea91972b1545cdf8722efba1cc3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <sal/config.h>
22 #include <tools/solar.h>
23 #include <svl/itempool.hxx>
24 #include <svl/poolitem.hxx>
25 #include <svl/stritem.hxx>
26 #include <nochaos.hxx>
27 #include <memory>
30 #define WID_CHAOS_START 500
33 namespace {
35 class CntItemPool;
37 class CntStaticPoolDefaults_Impl
39 static const sal_uInt32 m_nItems = 1;
40 std::vector<SfxPoolItem*> mvDefaults;
41 std::unique_ptr<SfxItemInfo[]> m_pItemInfos;
43 private:
44 inline void Insert( SfxPoolItem* pItem );
46 public:
47 explicit CntStaticPoolDefaults_Impl();
48 ~CntStaticPoolDefaults_Impl();
49 CntStaticPoolDefaults_Impl(const CntStaticPoolDefaults_Impl&) = delete;
50 CntStaticPoolDefaults_Impl& operator=(const CntStaticPoolDefaults_Impl&) = delete;
52 std::vector<SfxPoolItem*>* GetDefaults() { return &mvDefaults; }
53 const SfxItemInfo* GetItemInfos() const { return m_pItemInfos.get(); }
57 class CntItemPool: public SfxItemPool
59 static CntItemPool* _pThePool;
60 sal_uInt16 _nRefs;
62 protected:
63 CntItemPool();
64 virtual ~CntItemPool() override;
66 public:
67 static CntItemPool* Acquire();
68 static sal_uInt16 Release();
73 // static
74 SfxItemPool* NoChaos::GetItemPool()
76 // Get and hold CHAOS item pool.
77 return CntItemPool::Acquire();
81 // static
82 sal_uInt16 NoChaos::ReleaseItemPool()
84 // Release CHAOS item pool.
85 return CntItemPool::Release();
89 // CntItemPool implementation
92 static CntStaticPoolDefaults_Impl* pPoolDefs_Impl = nullptr;
94 // static member!
95 CntItemPool* CntItemPool::_pThePool = nullptr;
98 CntItemPool::CntItemPool()
99 : SfxItemPool( "chaos", WID_CHAOS_START, WID_CHAOS_START, nullptr ),
100 _nRefs( 0 )
102 FreezeIdRanges();
104 // Create static defaults.
105 pPoolDefs_Impl = new CntStaticPoolDefaults_Impl;
107 // Set item infos.
108 SetItemInfos( pPoolDefs_Impl->GetItemInfos() );
110 // Set static pool default items.
111 SetDefaults( pPoolDefs_Impl->GetDefaults() );
115 //virtual
116 CntItemPool::~CntItemPool()
118 // Release static pool default items.
119 ReleaseDefaults();
123 // static
124 CntItemPool* CntItemPool::Acquire()
126 if ( !_pThePool )
127 _pThePool = new CntItemPool;
129 _pThePool->_nRefs++;
131 return _pThePool;
135 // static
136 sal_uInt16 CntItemPool::Release()
138 if ( !_pThePool )
139 return 0;
141 sal_uInt16& nRefs = _pThePool->_nRefs;
143 if ( nRefs )
144 --nRefs;
146 if ( !nRefs )
148 delete _pThePool;
149 _pThePool = nullptr;
150 delete pPoolDefs_Impl;
151 pPoolDefs_Impl = nullptr;
152 return 0;
155 return nRefs;
159 // CntStaticPoolDefaults_Impl implementation.
162 inline void CntStaticPoolDefaults_Impl::Insert(
163 SfxPoolItem* pItem /* Static Pool Default Item */ )
165 sal_uInt16 nPos = pItem->Which() - WID_CHAOS_START;
167 mvDefaults[ nPos ] = pItem;
168 m_pItemInfos[ nPos ]._nSID = 0;
169 m_pItemInfos[ nPos ]._bPoolable = true;
173 CntStaticPoolDefaults_Impl::~CntStaticPoolDefaults_Impl()
175 for ( sal_uInt32 n = 0; n < m_nItems; ++n )
176 delete mvDefaults[ n ];
180 CntStaticPoolDefaults_Impl::CntStaticPoolDefaults_Impl()
181 : mvDefaults( m_nItems, nullptr ),
182 m_pItemInfos( new SfxItemInfo [ m_nItems ] )
184 memset( m_pItemInfos.get(), 0, sizeof( SfxItemInfo ) * m_nItems );
185 Insert( new SfxStringItem( WID_CHAOS_START, OUString() ) );
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */