Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / unotools / source / config / configvaluecontainer.cxx
blobfcdae8faee5847aadf1f4344433f1ab60a50a249
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 <sal/log.hxx>
23 #include <unotools/configvaluecontainer.hxx>
24 #include <unotools/confignode.hxx>
25 #include <uno/data.h>
26 #include <algorithm>
27 #include <vector>
29 namespace utl
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::lang;
35 //= NodeValueAccessor
37 enum class LocationType
39 SimplyObjectInstance,
40 Unbound
43 struct NodeValueAccessor
45 private:
46 OUString const sRelativePath; // the relative path of the node
47 LocationType eLocationType; // the type of location where the value is stored
48 void* pLocation; // the pointer to the location
49 Type aDataType; // the type object pointed to by pLocation
51 public:
52 explicit NodeValueAccessor( const OUString& _rNodePath );
54 void bind( void* _pLocation, const Type& _rType );
56 bool isBound( ) const { return ( LocationType::Unbound != eLocationType ) && ( nullptr != pLocation ); }
57 const OUString& getPath( ) const { return sRelativePath; }
58 LocationType getLocType( ) const { return eLocationType; }
59 void* getLocation( ) const { return pLocation; }
60 const Type& getDataType( ) const { return aDataType; }
62 bool operator == ( const NodeValueAccessor& rhs ) const;
65 NodeValueAccessor::NodeValueAccessor( const OUString& _rNodePath )
66 :sRelativePath( _rNodePath )
67 ,eLocationType( LocationType::Unbound )
68 ,pLocation( nullptr )
72 bool NodeValueAccessor::operator == ( const NodeValueAccessor& rhs ) const
74 return ( sRelativePath == rhs.sRelativePath )
75 && ( eLocationType == rhs.eLocationType )
76 && ( pLocation == rhs.pLocation );
79 void NodeValueAccessor::bind( void* _pLocation, const Type& _rType )
81 SAL_WARN_IF(isBound(), "unotools.config", "NodeValueAccessor::bind: already bound!");
83 eLocationType = LocationType::SimplyObjectInstance;
84 pLocation = _pLocation;
85 aDataType = _rType;
88 static
89 void lcl_copyData( const NodeValueAccessor& _rAccessor, const Any& _rData, ::osl::Mutex& _rMutex )
91 ::osl::MutexGuard aGuard( _rMutex );
93 SAL_WARN_IF(!_rAccessor.isBound(), "unotools.config", "::utl::lcl_copyData: invalid accessor!");
94 switch ( _rAccessor.getLocType() )
96 case LocationType::SimplyObjectInstance:
98 if ( _rData.hasValue() )
100 // assign the value
101 bool bSuccess = uno_type_assignData(
102 _rAccessor.getLocation(), _rAccessor.getDataType().getTypeLibType(),
103 const_cast< void* >( _rData.getValue() ), _rData.getValueType().getTypeLibType(),
104 cpp_queryInterface, cpp_acquire, cpp_release
106 SAL_WARN_IF(!bSuccess, "unotools.config",
107 "::utl::lcl_copyData( Accessor, Any ): could not assign the data (node path: \"" << _rAccessor.getPath() << "\"");
109 else {
110 SAL_INFO("unotools.config", "::utl::lcl_copyData: NULL value lost!");
113 break;
114 default:
115 break;
119 static
120 void lcl_copyData( Any& _rData, const NodeValueAccessor& _rAccessor, ::osl::Mutex& _rMutex )
122 ::osl::MutexGuard aGuard( _rMutex );
124 SAL_WARN_IF(!_rAccessor.isBound(), "unotools.config", "::utl::lcl_copyData: invalid accessor!" );
125 switch ( _rAccessor.getLocType() )
127 case LocationType::SimplyObjectInstance:
128 // a simple setValue...
129 _rData.setValue( _rAccessor.getLocation(), _rAccessor.getDataType() );
130 break;
132 default:
133 break;
137 //= functors on NodeValueAccessor instances
139 /// base class for functors synchronizing between exchange locations and config sub nodes
140 struct SubNodeAccess
142 protected:
143 const OConfigurationNode& m_rRootNode;
144 ::osl::Mutex& m_rMutex;
146 public:
147 SubNodeAccess( const OConfigurationNode& _rRootNode, ::osl::Mutex& _rMutex )
148 :m_rRootNode( _rRootNode )
149 ,m_rMutex( _rMutex )
154 struct UpdateFromConfig : public SubNodeAccess
156 public:
157 UpdateFromConfig( const OConfigurationNode& _rRootNode, ::osl::Mutex& _rMutex ) : SubNodeAccess( _rRootNode, _rMutex ) { }
159 void operator() ( NodeValueAccessor const & _rAccessor )
161 ::utl::lcl_copyData( _rAccessor, m_rRootNode.getNodeValue( _rAccessor.getPath( ) ), m_rMutex );
165 struct UpdateToConfig : public SubNodeAccess
167 public:
168 UpdateToConfig( const OConfigurationNode& _rRootNode, ::osl::Mutex& _rMutex ) : SubNodeAccess( _rRootNode, _rMutex ) { }
170 void operator() ( NodeValueAccessor const & _rAccessor )
172 Any aNewValue;
173 lcl_copyData( aNewValue, _rAccessor, m_rMutex );
174 m_rRootNode.setNodeValue( _rAccessor.getPath( ), aNewValue );
178 //= OConfigurationValueContainerImpl
180 struct OConfigurationValueContainerImpl
182 Reference< XComponentContext > xORB; // the service factory
183 ::osl::Mutex& rMutex; // the mutex for accessing the data containers
184 OConfigurationTreeRoot aConfigRoot; // the configuration node we're accessing
186 std::vector<NodeValueAccessor> aAccessors; // the accessors to the node values
188 OConfigurationValueContainerImpl( const Reference< XComponentContext >& _rxORB, ::osl::Mutex& _rMutex )
189 :xORB( _rxORB )
190 ,rMutex( _rMutex )
195 //= OConfigurationValueContainer
197 OConfigurationValueContainer::OConfigurationValueContainer(
198 const Reference< XComponentContext >& _rxORB, ::osl::Mutex& _rAccessSafety,
199 const sal_Char* _pConfigLocation, const sal_Int32 _nLevels )
200 :m_pImpl( new OConfigurationValueContainerImpl( _rxORB, _rAccessSafety ) )
202 implConstruct( OUString::createFromAscii( _pConfigLocation ), _nLevels );
205 OConfigurationValueContainer::~OConfigurationValueContainer()
209 void OConfigurationValueContainer::implConstruct( const OUString& _rConfigLocation,
210 const sal_Int32 _nLevels )
212 SAL_WARN_IF(m_pImpl->aConfigRoot.isValid(), "unotools.config", "OConfigurationValueContainer::implConstruct: already initialized!");
214 // create the configuration node we're about to work with
215 m_pImpl->aConfigRoot = OConfigurationTreeRoot::createWithComponentContext(
216 m_pImpl->xORB,
217 _rConfigLocation,
218 _nLevels
220 SAL_WARN_IF(!m_pImpl->aConfigRoot.isValid(), "unotools.config",
221 "Could not access the configuration node located at " << _rConfigLocation);
224 void OConfigurationValueContainer::registerExchangeLocation( const sal_Char* _pRelativePath,
225 void* _pContainer, const Type& _rValueType )
227 // checks...
228 SAL_WARN_IF(!_pContainer, "unotools.config",
229 "OConfigurationValueContainer::registerExchangeLocation: invalid container location!");
230 SAL_WARN_IF(!( (TypeClass_CHAR == _rValueType.getTypeClass( ) )
231 || ( TypeClass_BOOLEAN == _rValueType.getTypeClass( ) )
232 || ( TypeClass_BYTE == _rValueType.getTypeClass( ) )
233 || ( TypeClass_SHORT == _rValueType.getTypeClass( ) )
234 || ( TypeClass_LONG == _rValueType.getTypeClass( ) )
235 || ( TypeClass_DOUBLE == _rValueType.getTypeClass( ) )
236 || ( TypeClass_STRING == _rValueType.getTypeClass( ) )
237 || ( TypeClass_SEQUENCE == _rValueType.getTypeClass( ) )),
238 "unotools.config",
239 "OConfigurationValueContainer::registerExchangeLocation: invalid type!" );
241 // build an accessor for this container
242 NodeValueAccessor aNewAccessor( OUString::createFromAscii( _pRelativePath ) );
243 aNewAccessor.bind( _pContainer, _rValueType );
245 // insert it into our structure
246 implRegisterExchangeLocation( aNewAccessor );
249 void OConfigurationValueContainer::read( )
251 std::for_each(
252 m_pImpl->aAccessors.begin(),
253 m_pImpl->aAccessors.end(),
254 UpdateFromConfig( m_pImpl->aConfigRoot, m_pImpl->rMutex )
258 void OConfigurationValueContainer::commit()
260 // write the current values in the exchange locations
261 std::for_each(
262 m_pImpl->aAccessors.begin(),
263 m_pImpl->aAccessors.end(),
264 UpdateToConfig( m_pImpl->aConfigRoot, m_pImpl->rMutex )
267 // commit the changes done
268 m_pImpl->aConfigRoot.commit( );
271 void OConfigurationValueContainer::implRegisterExchangeLocation( const NodeValueAccessor& _rAccessor )
273 // some checks
274 SAL_WARN_IF(m_pImpl->aConfigRoot.isValid() && !m_pImpl->aConfigRoot.hasByHierarchicalName(_rAccessor.getPath()),
275 "unotools.config",
276 "OConfigurationValueContainer::implRegisterExchangeLocation: invalid relative path!" );
278 // another check (should be the first container for this node)
279 SAL_WARN_IF(!(m_pImpl->aAccessors.end() == ::std::find(
280 m_pImpl->aAccessors.begin(),
281 m_pImpl->aAccessors.end(),
282 _rAccessor)),
283 "unotools.config",
284 "OConfigurationValueContainer::implRegisterExchangeLocation: already registered a container for this subnode!" );
286 // remember the accessor
287 m_pImpl->aAccessors.push_back( _rAccessor );
289 // and initially fill the value
290 lcl_copyData( _rAccessor, m_pImpl->aConfigRoot.getNodeValue( _rAccessor.getPath() ), m_pImpl->rMutex );
293 } // namespace utl
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */