23#include <osl/diagnose.h>
24#include <osl/mutex.hxx>
31 class SharedResources_Impl
41 static void registerClient();
42 static void revokeClient();
44 static SharedResources_Impl&
50 SharedResources_Impl();
54 static ::osl::Mutex s_aMutex;
64 SharedResources_Impl::SharedResources_Impl()
69 OUString SharedResources_Impl::getResourceString(
TranslateId pId)
const
74 void SharedResources_Impl::registerClient()
79 void SharedResources_Impl::revokeClient()
81 ::osl::MutexGuard aGuard(
getMutex() );
82 if ( 0 == osl_atomic_decrement( &
s_nClients ) )
90 SharedResources_Impl& SharedResources_Impl::getInstance()
92 ::osl::MutexGuard aGuard(
getMutex() );
93 OSL_ENSURE(
s_nClients > 0,
"SharedResources_Impl::getInstance: no active clients!" );
103 bool lcl_substitute( OUString& _inout_rString,
104 const char* _pAsciiPattern, std::u16string_view _rReplace )
106 OUString oldString = _inout_rString;
107 OUString sPattern( OUString::createFromAscii( _pAsciiPattern ) );
108 _inout_rString = _inout_rString.replaceAll(sPattern, _rReplace);
109 return oldString != _inout_rString;
113 SharedResources::SharedResources()
115 SharedResources_Impl::registerClient();
119 SharedResources::~SharedResources()
121 SharedResources_Impl::revokeClient();
125 OUString SharedResources::getResourceString(
TranslateId pResId)
const
127 return SharedResources_Impl::getInstance().getResourceString(pResId);
131 OUString SharedResources::getResourceStringWithSubstitution(
TranslateId pResId,
132 const char* _pAsciiPatternToReplace,
const OUString& _rStringToSubstitute )
const
134 OUString sString( SharedResources_Impl::getInstance().getResourceString(pResId) );
135 if ( !lcl_substitute( sString, _pAsciiPatternToReplace, _rStringToSubstitute ) )
136 SAL_WARN(
"connectivity.resource",
"Unable to substitute " << _pAsciiPatternToReplace <<
" with " << _rStringToSubstitute);
141 OUString SharedResources::getResourceStringWithSubstitution(
TranslateId pResId,
142 const char* _pAsciiPatternToReplace1,
const OUString& _rStringToSubstitute1,
143 const char* _pAsciiPatternToReplace2,
const OUString& _rStringToSubstitute2 )
const
145 OUString sString( SharedResources_Impl::getInstance().getResourceString(pResId) );
146 if( !lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) )
147 SAL_WARN(
"connectivity.resource",
"Unable to substitute " << _pAsciiPatternToReplace1 <<
" with " << _rStringToSubstitute1);
148 if( !lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) )
149 SAL_WARN(
"connectivity.resource",
"Unable to substitute " << _pAsciiPatternToReplace2 <<
" with " << _rStringToSubstitute2);
154 OUString SharedResources::getResourceStringWithSubstitution(
TranslateId pResId,
155 const char* _pAsciiPatternToReplace1,
const OUString& _rStringToSubstitute1,
156 const char* _pAsciiPatternToReplace2,
const OUString& _rStringToSubstitute2,
157 const char* _pAsciiPatternToReplace3,
const OUString& _rStringToSubstitute3 )
const
159 OUString sString( SharedResources_Impl::getInstance().getResourceString(pResId) );
160 if( !lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) )
161 SAL_WARN(
"connectivity.resource",
"Unable to substitute " << _pAsciiPatternToReplace1 <<
" with " << _rStringToSubstitute1);
162 if( !lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) )
163 SAL_WARN(
"connectivity.resource",
"Unable to substitute " << _pAsciiPatternToReplace2 <<
" with " << _rStringToSubstitute2);
164 if( !lcl_substitute( sString, _pAsciiPatternToReplace3, _rStringToSubstitute3 ) )
165 SAL_WARN(
"connectivity.resource",
"Unable to substitute " << _pAsciiPatternToReplace3 <<
" with " << _rStringToSubstitute3);
169 OUString SharedResources::getResourceStringWithSubstitution(
TranslateId pResId,
170 const std::vector< std::pair<const char* , OUString > >& _rStringToSubstitutes)
const
172 OUString sString( SharedResources_Impl::getInstance().getResourceString(pResId) );
173 for(
const auto& [rPattern, rReplace] : _rStringToSubstitutes)
174 if( !lcl_substitute( sString, rPattern, rReplace ) )
175 SAL_WARN(
"connectivity.resource",
"Unable to substitute " << rPattern <<
" with " << rReplace);
#define SAL_WARN(area, stream)
OUString get(TranslateId sContextAndId, const std::locale &loc)
::osl::Mutex & getMutex()
static oslInterlockedCount s_nClients
static SharedResources_Impl * s_pInstance