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
;
34 using namespace ::com::sun::star::lang
;
40 enum class LocationType
48 struct NodeValueAccessor
51 OUString sRelativePath
; // the relative path of the node
52 LocationType eLocationType
; // the type of location where the value is stored
53 void* pLocation
; // the pointer to the location
54 Type aDataType
; // the type object pointed to by pLocation
57 explicit NodeValueAccessor( OUString _aNodePath
);
59 void bind( void* _pLocation
, const Type
& _rType
);
61 bool isBound( ) const { return ( LocationType::Unbound
!= eLocationType
) && ( nullptr != pLocation
); }
62 const OUString
& getPath( ) const { return sRelativePath
; }
63 LocationType
getLocType( ) const { return eLocationType
; }
64 void* getLocation( ) const { return pLocation
; }
65 const Type
& getDataType( ) const { return aDataType
; }
67 bool operator == ( const NodeValueAccessor
& rhs
) const;
70 NodeValueAccessor::NodeValueAccessor( OUString _aNodePath
)
71 :sRelativePath(std::move( _aNodePath
))
72 ,eLocationType( LocationType::Unbound
)
77 bool NodeValueAccessor::operator == ( const NodeValueAccessor
& rhs
) const
79 return ( sRelativePath
== rhs
.sRelativePath
)
80 && ( eLocationType
== rhs
.eLocationType
)
81 && ( pLocation
== rhs
.pLocation
);
84 void NodeValueAccessor::bind( void* _pLocation
, const Type
& _rType
)
86 SAL_WARN_IF(isBound(), "unotools.config", "NodeValueAccessor::bind: already bound!");
88 eLocationType
= LocationType::SimplyObjectInstance
;
89 pLocation
= _pLocation
;
94 void lcl_copyData( const NodeValueAccessor
& _rAccessor
, const Any
& _rData
, std::mutex
& _rMutex
)
96 std::unique_lock
aGuard( _rMutex
);
98 SAL_WARN_IF(!_rAccessor
.isBound(), "unotools.config", "::utl::lcl_copyData: invalid accessor!");
99 switch ( _rAccessor
.getLocType() )
101 case LocationType::SimplyObjectInstance
:
103 if ( _rData
.hasValue() )
106 bool bSuccess
= uno_type_assignData(
107 _rAccessor
.getLocation(), _rAccessor
.getDataType().getTypeLibType(),
108 const_cast< void* >( _rData
.getValue() ), _rData
.getValueType().getTypeLibType(),
109 cpp_queryInterface
, cpp_acquire
, cpp_release
111 SAL_WARN_IF(!bSuccess
, "unotools.config",
112 "::utl::lcl_copyData( Accessor, Any ): could not assign the data (node path: \"" << _rAccessor
.getPath() << "\"");
115 SAL_INFO("unotools.config", "::utl::lcl_copyData: NULL value lost!");
125 void lcl_copyData( Any
& _rData
, const NodeValueAccessor
& _rAccessor
, std::mutex
& _rMutex
)
127 std::unique_lock
aGuard( _rMutex
);
129 SAL_WARN_IF(!_rAccessor
.isBound(), "unotools.config", "::utl::lcl_copyData: invalid accessor!" );
130 switch ( _rAccessor
.getLocType() )
132 case LocationType::SimplyObjectInstance
:
133 // a simple setValue...
134 _rData
.setValue( _rAccessor
.getLocation(), _rAccessor
.getDataType() );
142 //= functors on NodeValueAccessor instances
146 /// base class for functors synchronizing between exchange locations and config sub nodes
150 const OConfigurationNode
& m_rRootNode
;
151 std::mutex
& m_rMutex
;
154 SubNodeAccess( const OConfigurationNode
& _rRootNode
, std::mutex
& _rMutex
)
155 :m_rRootNode( _rRootNode
)
161 struct UpdateFromConfig
: public SubNodeAccess
164 UpdateFromConfig( const OConfigurationNode
& _rRootNode
, std::mutex
& _rMutex
) : SubNodeAccess( _rRootNode
, _rMutex
) { }
166 void operator() ( NodeValueAccessor
const & _rAccessor
)
168 ::utl::lcl_copyData( _rAccessor
, m_rRootNode
.getNodeValue( _rAccessor
.getPath( ) ), m_rMutex
);
172 struct UpdateToConfig
: public SubNodeAccess
175 UpdateToConfig( const OConfigurationNode
& _rRootNode
, std::mutex
& _rMutex
) : SubNodeAccess( _rRootNode
, _rMutex
) { }
177 void operator() ( NodeValueAccessor
const & _rAccessor
)
180 lcl_copyData( aNewValue
, _rAccessor
, m_rMutex
);
181 m_rRootNode
.setNodeValue( _rAccessor
.getPath( ), aNewValue
);
187 //= OConfigurationValueContainerImpl
189 struct OConfigurationValueContainerImpl
191 Reference
< XComponentContext
> xORB
; // the service factory
192 std::mutex
& rMutex
; // the mutex for accessing the data containers
193 OConfigurationTreeRoot aConfigRoot
; // the configuration node we're accessing
195 std::vector
<NodeValueAccessor
> aAccessors
; // the accessors to the node values
197 OConfigurationValueContainerImpl( const Reference
< XComponentContext
>& _rxORB
, std::mutex
& _rMutex
)
204 //= OConfigurationValueContainer
206 OConfigurationValueContainer::OConfigurationValueContainer(
207 const Reference
< XComponentContext
>& _rxORB
, std::mutex
& _rAccessSafety
,
208 const char* _pConfigLocation
, const sal_Int32 _nLevels
)
209 :m_pImpl( new OConfigurationValueContainerImpl( _rxORB
, _rAccessSafety
) )
211 implConstruct( OUString::createFromAscii( _pConfigLocation
), _nLevels
);
214 OConfigurationValueContainer::~OConfigurationValueContainer()
218 void OConfigurationValueContainer::implConstruct( const OUString
& _rConfigLocation
,
219 const sal_Int32 _nLevels
)
221 SAL_WARN_IF(m_pImpl
->aConfigRoot
.isValid(), "unotools.config", "OConfigurationValueContainer::implConstruct: already initialized!");
223 // create the configuration node we're about to work with
224 m_pImpl
->aConfigRoot
= OConfigurationTreeRoot::createWithComponentContext(
229 SAL_WARN_IF(!m_pImpl
->aConfigRoot
.isValid(), "unotools.config",
230 "Could not access the configuration node located at " << _rConfigLocation
);
233 void OConfigurationValueContainer::registerExchangeLocation( const char* _pRelativePath
,
234 void* _pContainer
, const Type
& _rValueType
)
237 SAL_WARN_IF(!_pContainer
, "unotools.config",
238 "OConfigurationValueContainer::registerExchangeLocation: invalid container location!");
239 SAL_WARN_IF(!( (TypeClass_CHAR
== _rValueType
.getTypeClass( ) )
240 || ( TypeClass_BOOLEAN
== _rValueType
.getTypeClass( ) )
241 || ( TypeClass_BYTE
== _rValueType
.getTypeClass( ) )
242 || ( TypeClass_SHORT
== _rValueType
.getTypeClass( ) )
243 || ( TypeClass_LONG
== _rValueType
.getTypeClass( ) )
244 || ( TypeClass_DOUBLE
== _rValueType
.getTypeClass( ) )
245 || ( TypeClass_STRING
== _rValueType
.getTypeClass( ) )
246 || ( TypeClass_SEQUENCE
== _rValueType
.getTypeClass( ) )),
248 "OConfigurationValueContainer::registerExchangeLocation: invalid type!" );
250 // build an accessor for this container
251 NodeValueAccessor
aNewAccessor( OUString::createFromAscii( _pRelativePath
) );
252 aNewAccessor
.bind( _pContainer
, _rValueType
);
254 // insert it into our structure
255 implRegisterExchangeLocation( aNewAccessor
);
258 void OConfigurationValueContainer::read( )
261 m_pImpl
->aAccessors
.begin(),
262 m_pImpl
->aAccessors
.end(),
263 UpdateFromConfig( m_pImpl
->aConfigRoot
, m_pImpl
->rMutex
)
267 void OConfigurationValueContainer::commit()
269 // write the current values in the exchange locations
271 m_pImpl
->aAccessors
.begin(),
272 m_pImpl
->aAccessors
.end(),
273 UpdateToConfig( m_pImpl
->aConfigRoot
, m_pImpl
->rMutex
)
276 // commit the changes done
277 m_pImpl
->aConfigRoot
.commit( );
280 void OConfigurationValueContainer::implRegisterExchangeLocation( const NodeValueAccessor
& _rAccessor
)
283 SAL_WARN_IF(m_pImpl
->aConfigRoot
.isValid() && !m_pImpl
->aConfigRoot
.hasByHierarchicalName(_rAccessor
.getPath()),
285 "OConfigurationValueContainer::implRegisterExchangeLocation: invalid relative path!" );
287 // another check (should be the first container for this node)
288 SAL_WARN_IF(!(m_pImpl
->aAccessors
.end() == ::std::find(
289 m_pImpl
->aAccessors
.begin(),
290 m_pImpl
->aAccessors
.end(),
293 "OConfigurationValueContainer::implRegisterExchangeLocation: already registered a container for this subnode!" );
295 // remember the accessor
296 m_pImpl
->aAccessors
.push_back( _rAccessor
);
298 // and initially fill the value
299 lcl_copyData( _rAccessor
, m_pImpl
->aConfigRoot
.getNodeValue( _rAccessor
.getPath() ), m_pImpl
->rMutex
);
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */