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 0 : OComponentResModuleImpl( const OString& _rResFilePrefix )
45 : :m_pResources( NULL )
46 : ,m_bInitialized( false )
47 0 : ,m_sResFilePrefix( _rResFilePrefix )
48 : {
49 0 : }
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 0 : void OComponentResModuleImpl::freeResManager()
66 : {
67 0 : delete m_pResources, m_pResources = NULL;
68 0 : m_bInitialized = false;
69 0 : }
70 :
71 0 : ResMgr* OComponentResModuleImpl::getResManager()
72 : {
73 0 : if ( !m_pResources && !m_bInitialized )
74 : {
75 : // create a manager with a fixed prefix
76 0 : OString aMgrName = m_sResFilePrefix;
77 :
78 0 : 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 0 : m_bInitialized = true;
85 : }
86 0 : return m_pResources;
87 : }
88 :
89 : //= OComponentResourceModule
90 :
91 0 : OComponentResourceModule::OComponentResourceModule( const OString& _rResFilePrefix )
92 : :BaseClass()
93 0 : ,m_pImpl( new OComponentResModuleImpl( _rResFilePrefix ) )
94 : {
95 0 : }
96 :
97 0 : OComponentResourceModule::~OComponentResourceModule()
98 : {
99 0 : }
100 :
101 0 : ResMgr* OComponentResourceModule::getResManager()
102 : {
103 0 : ::osl::MutexGuard aGuard( m_aMutex );
104 0 : return m_pImpl->getResManager();
105 : }
106 :
107 0 : void OComponentResourceModule::onFirstClient()
108 : {
109 0 : BaseClass::onFirstClient();
110 0 : }
111 :
112 0 : void OComponentResourceModule::onLastClient()
113 : {
114 0 : m_pImpl->freeResManager();
115 0 : BaseClass::onLastClient();
116 0 : }
117 :
118 : } // namespace utl
119 :
120 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|