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 : * 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 <unotools/componentresmodule.hxx>
21 :
22 : #include <boost/noncopyable.hpp>
23 : #include <tools/resmgr.hxx>
24 : #include <osl/diagnose.h>
25 : #include <rtl/strbuf.hxx>
26 :
27 : namespace utl
28 : {
29 :
30 : //= OComponentResModuleImpl
31 :
32 : /** PIMPL-class for OComponentResourceModule
33 :
34 : not threadsafe!
35 : */
36 : class OComponentResModuleImpl: private boost::noncopyable
37 : {
38 : private:
39 : ResMgr* m_pResources;
40 : bool m_bInitialized;
41 : OString m_sResFilePrefix;
42 :
43 : public:
44 56 : OComponentResModuleImpl( const OString& _rResFilePrefix )
45 : :m_pResources( NULL )
46 : ,m_bInitialized( false )
47 56 : ,m_sResFilePrefix( _rResFilePrefix )
48 : {
49 56 : }
50 :
51 0 : ~OComponentResModuleImpl()
52 0 : {
53 0 : freeResManager();
54 0 : }
55 :
56 : /** releases our resource manager
57 : */
58 : void freeResManager();
59 :
60 : /** retrieves our resource manager
61 : */
62 : ResMgr* getResManager();
63 : };
64 :
65 4 : void OComponentResModuleImpl::freeResManager()
66 : {
67 4 : delete m_pResources, m_pResources = NULL;
68 4 : m_bInitialized = false;
69 4 : }
70 :
71 2 : ResMgr* OComponentResModuleImpl::getResManager()
72 : {
73 2 : if ( !m_pResources && !m_bInitialized )
74 : {
75 : // create a manager with a fixed prefix
76 2 : OString aMgrName = m_sResFilePrefix;
77 :
78 2 : m_pResources = ResMgr::CreateResMgr( aMgrName.getStr() );
79 : OSL_ENSURE( m_pResources,
80 : OStringBuffer( "OModuleImpl::getResManager: could not create the resource manager (file name: " )
81 : .append(aMgrName)
82 : .append(")!").getStr() );
83 :
84 2 : m_bInitialized = true;
85 : }
86 2 : return m_pResources;
87 : }
88 :
89 : //= OComponentResourceModule
90 :
91 56 : OComponentResourceModule::OComponentResourceModule( const OString& _rResFilePrefix )
92 : :BaseClass()
93 56 : ,m_pImpl( new OComponentResModuleImpl( _rResFilePrefix ) )
94 : {
95 56 : }
96 :
97 0 : OComponentResourceModule::~OComponentResourceModule()
98 : {
99 0 : }
100 :
101 2 : ResMgr* OComponentResourceModule::getResManager()
102 : {
103 2 : ::osl::MutexGuard aGuard( m_aMutex );
104 2 : return m_pImpl->getResManager();
105 : }
106 :
107 4 : void OComponentResourceModule::onFirstClient()
108 : {
109 4 : BaseClass::onFirstClient();
110 4 : }
111 :
112 4 : void OComponentResourceModule::onLastClient()
113 : {
114 4 : m_pImpl->freeResManager();
115 4 : BaseClass::onLastClient();
116 4 : }
117 :
118 : } // namespace utl
119 :
120 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|