1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
33 using namespace ::com::sun::star::uno
;
39 enum class LocationType
47 struct NodeValueAccessor
50 OUString sRelativePath
; // the relative path of the node
51 LocationType eLocationType
; // the type of location where the value is stored
52 void* pLocation
; // the pointer to the location
53 Type aDataType
; // the type object pointed to by pLocation
56 explicit NodeValueAccessor( OUString _aNodePath
);
58 void bind( void* _pLocation
, const Type
& _rType
);
60 bool isBound( ) const { return ( LocationType::Unbound
!= eLocationType
) && ( nullptr != pLocation
); }
61 const OUString
& getPath( ) const { return sRelativePath
; }
62 LocationType
getLocType( ) const { return eLocationType
; }
63 void* getLocation( ) const { return pLocation
; }
64 const Type
& getDataType( ) const { return aDataType
; }
66 bool operator == ( const NodeValueAccessor
& rhs
) const;
69 NodeValueAccessor::NodeValueAccessor( OUString _aNodePath
)
70 :sRelativePath(std::move( _aNodePath
))
71 ,eLocationType( LocationType::Unbound
)
76 bool NodeValueAccessor::operator == ( const NodeValueAccessor
& rhs
) const
78 return ( sRelativePath
== rhs
.sRelativePath
)
79 && ( eLocationType
== rhs
.eLocationType
)
80 && ( pLocation
== rhs
.pLocation
);
83 void NodeValueAccessor::bind( void* _pLocation
, const Type
& _rType
)
85 SAL_WARN_IF(isBound(), "unotools.config", "NodeValueAccessor::bind: already bound!");
87 eLocationType
= LocationType::SimplyObjectInstance
;
88 pLocation
= _pLocation
;
93 void lcl_copyData( const NodeValueAccessor
& _rAccessor
, const Any
& _rData
, std::mutex
& _rMutex
)
95 std::unique_lock
aGuard( _rMutex
);
97 SAL_WARN_IF(!_rAccessor
.isBound(), "unotools.config", "::utl::lcl_copyData: invalid accessor!");
98 switch ( _rAccessor
.getLocType() )
100 case LocationType::SimplyObjectInstance
:
102 if ( _rData
.hasValue() )
105 bool bSuccess
= uno_type_assignData(
106 _rAccessor
.getLocation(), _rAccessor
.getDataType().getTypeLibType(),
107 const_cast< void* >( _rData
.getValue() ), _rData
.getValueType().getTypeLibType(),
108 cpp_queryInterface
, cpp_acquire
, cpp_release
110 SAL_WARN_IF(!bSuccess
, "unotools.config",
111 "::utl::lcl_copyData( Accessor, Any ): could not assign the data (node path: \"" << _rAccessor
.getPath() << "\"");
114 SAL_INFO("unotools.config", "::utl::lcl_copyData: NULL value lost!");
124 void lcl_copyData( Any
& _rData
, const NodeValueAccessor
& _rAccessor
, std::mutex
& _rMutex
)
126 std::unique_lock
aGuard( _rMutex
);
128 SAL_WARN_IF(!_rAccessor
.isBound(), "unotools.config", "::utl::lcl_copyData: invalid accessor!" );
129 switch ( _rAccessor
.getLocType() )
131 case LocationType::SimplyObjectInstance
:
132 // a simple setValue...
133 _rData
.setValue( _rAccessor
.getLocation(), _rAccessor
.getDataType() );
141 //= functors on NodeValueAccessor instances
145 /// base class for functors synchronizing between exchange locations and config sub nodes
149 const OConfigurationNode
& m_rRootNode
;
150 std::mutex
& m_rMutex
;
153 SubNodeAccess( const OConfigurationNode
& _rRootNode
, std::mutex
& _rMutex
)
154 :m_rRootNode( _rRootNode
)
160 struct UpdateFromConfig
: public SubNodeAccess
163 UpdateFromConfig( const OConfigurationNode
& _rRootNode
, std::mutex
& _rMutex
) : SubNodeAccess( _rRootNode
, _rMutex
) { }
165 void operator() ( NodeValueAccessor
const & _rAccessor
)
167 ::utl::lcl_copyData( _rAccessor
, m_rRootNode
.getNodeValue( _rAccessor
.getPath( ) ), m_rMutex
);
171 struct UpdateToConfig
: public SubNodeAccess
174 UpdateToConfig( const OConfigurationNode
& _rRootNode
, std::mutex
& _rMutex
) : SubNodeAccess( _rRootNode
, _rMutex
) { }
176 void operator() ( NodeValueAccessor
const & _rAccessor
)
179 lcl_copyData( aNewValue
, _rAccessor
, m_rMutex
);
180 m_rRootNode
.setNodeValue( _rAccessor
.getPath( ), aNewValue
);
186 //= OConfigurationValueContainerImpl
188 struct OConfigurationValueContainerImpl
190 Reference
< XComponentContext
> xORB
; // the service factory
191 std::mutex
& rMutex
; // the mutex for accessing the data containers
192 OConfigurationTreeRoot aConfigRoot
; // the configuration node we're accessing
194 std::vector
<NodeValueAccessor
> aAccessors
; // the accessors to the node values
196 OConfigurationValueContainerImpl( const Reference
< XComponentContext
>& _rxORB
, std::mutex
& _rMutex
)
203 //= OConfigurationValueContainer
205 OConfigurationValueContainer::OConfigurationValueContainer(
206 const Reference
< XComponentContext
>& _rxORB
, std::mutex
& _rAccessSafety
,
207 const OUString
& _rConfigLocation
, const sal_Int32 _nLevels
)
208 :m_pImpl( new OConfigurationValueContainerImpl( _rxORB
, _rAccessSafety
) )
210 implConstruct( _rConfigLocation
, _nLevels
);
213 OConfigurationValueContainer::~OConfigurationValueContainer()
217 void OConfigurationValueContainer::implConstruct( const OUString
& _rConfigLocation
,
218 const sal_Int32 _nLevels
)
220 SAL_WARN_IF(m_pImpl
->aConfigRoot
.isValid(), "unotools.config", "OConfigurationValueContainer::implConstruct: already initialized!");
222 // create the configuration node we're about to work with
223 m_pImpl
->aConfigRoot
= OConfigurationTreeRoot::createWithComponentContext(
228 SAL_WARN_IF(!m_pImpl
->aConfigRoot
.isValid(), "unotools.config",
229 "Could not access the configuration node located at " << _rConfigLocation
);
232 void OConfigurationValueContainer::registerExchangeLocation( const OUString
& _rRelativePath
,
233 void* _pContainer
, const Type
& _rValueType
)
236 SAL_WARN_IF(!_pContainer
, "unotools.config",
237 "OConfigurationValueContainer::registerExchangeLocation: invalid container location!");
238 SAL_WARN_IF(!( (TypeClass_CHAR
== _rValueType
.getTypeClass( ) )
239 || ( TypeClass_BOOLEAN
== _rValueType
.getTypeClass( ) )
240 || ( TypeClass_BYTE
== _rValueType
.getTypeClass( ) )
241 || ( TypeClass_SHORT
== _rValueType
.getTypeClass( ) )
242 || ( TypeClass_LONG
== _rValueType
.getTypeClass( ) )
243 || ( TypeClass_DOUBLE
== _rValueType
.getTypeClass( ) )
244 || ( TypeClass_STRING
== _rValueType
.getTypeClass( ) )
245 || ( TypeClass_SEQUENCE
== _rValueType
.getTypeClass( ) )),
247 "OConfigurationValueContainer::registerExchangeLocation: invalid type!" );
249 // build an accessor for this container
250 NodeValueAccessor
aNewAccessor( _rRelativePath
);
251 aNewAccessor
.bind( _pContainer
, _rValueType
);
253 // insert it into our structure
254 implRegisterExchangeLocation( aNewAccessor
);
257 void OConfigurationValueContainer::read( )
260 m_pImpl
->aAccessors
.begin(),
261 m_pImpl
->aAccessors
.end(),
262 UpdateFromConfig( m_pImpl
->aConfigRoot
, m_pImpl
->rMutex
)
266 void OConfigurationValueContainer::commit()
268 // write the current values in the exchange locations
270 m_pImpl
->aAccessors
.begin(),
271 m_pImpl
->aAccessors
.end(),
272 UpdateToConfig( m_pImpl
->aConfigRoot
, m_pImpl
->rMutex
)
275 // commit the changes done
276 m_pImpl
->aConfigRoot
.commit( );
279 void OConfigurationValueContainer::implRegisterExchangeLocation( const NodeValueAccessor
& _rAccessor
)
282 SAL_WARN_IF(m_pImpl
->aConfigRoot
.isValid() && !m_pImpl
->aConfigRoot
.hasByHierarchicalName(_rAccessor
.getPath()),
284 "OConfigurationValueContainer::implRegisterExchangeLocation: invalid relative path!" );
286 // another check (should be the first container for this node)
287 SAL_WARN_IF(!(m_pImpl
->aAccessors
.end() == ::std::find(
288 m_pImpl
->aAccessors
.begin(),
289 m_pImpl
->aAccessors
.end(),
292 "OConfigurationValueContainer::implRegisterExchangeLocation: already registered a container for this subnode!" );
294 // remember the accessor
295 m_pImpl
->aAccessors
.push_back( _rAccessor
);
297 // and initially fill the value
298 lcl_copyData( _rAccessor
, m_pImpl
->aConfigRoot
.getNodeValue( _rAccessor
.getPath() ), m_pImpl
->rMutex
);
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */