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>
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::lang
;
37 enum class LocationType
43 struct NodeValueAccessor
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
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
)
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
;
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() )
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() << "\"");
110 SAL_INFO("unotools.config", "::utl::lcl_copyData: NULL value lost!");
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() );
137 //= functors on NodeValueAccessor instances
139 /// base class for functors synchronizing between exchange locations and config sub nodes
143 const OConfigurationNode
& m_rRootNode
;
144 ::osl::Mutex
& m_rMutex
;
147 SubNodeAccess( const OConfigurationNode
& _rRootNode
, ::osl::Mutex
& _rMutex
)
148 :m_rRootNode( _rRootNode
)
154 struct UpdateFromConfig
: public SubNodeAccess
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
168 UpdateToConfig( const OConfigurationNode
& _rRootNode
, ::osl::Mutex
& _rMutex
) : SubNodeAccess( _rRootNode
, _rMutex
) { }
170 void operator() ( NodeValueAccessor
const & _rAccessor
)
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
)
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(
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
)
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( ) )),
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( )
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
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
)
274 SAL_WARN_IF(m_pImpl
->aConfigRoot
.isValid() && !m_pImpl
->aConfigRoot
.hasByHierarchicalName(_rAccessor
.getPath()),
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(),
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
);
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */