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 <unotools/configvaluecontainer.hxx>
21 #include <unotools/confignode.hxx>
22 #include <tools/debug.hxx>
28 #include <rtl/strbuf.hxx>
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::lang
;
41 ltSimplyObjectInstance
,
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 NodeValueAccessor( const OUString
& _rNodePath
);
58 void bind( void* _pLocation
, const Type
& _rType
);
60 bool isBound( ) const { return ( ltUnbound
!= eLocationType
) && ( NULL
!= 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( const OUString
& _rNodePath
)
70 :sRelativePath( _rNodePath
)
71 ,eLocationType( ltUnbound
)
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 DBG_ASSERT( !isBound(), "NodeValueAccessor::bind: already bound!" );
87 eLocationType
= ltSimplyObjectInstance
;
88 pLocation
= _pLocation
;
95 void lcl_copyData( const NodeValueAccessor
& _rAccessor
, const Any
& _rData
, ::osl::Mutex
& _rMutex
)
97 ::osl::MutexGuard
aGuard( _rMutex
);
99 DBG_ASSERT( _rAccessor
.isBound(), "::utl::lcl_copyData: invalid accessor!" );
100 switch ( _rAccessor
.getLocType() )
102 case ltSimplyObjectInstance
:
104 if ( _rData
.hasValue() )
111 _rAccessor
.getLocation(), _rAccessor
.getDataType().getTypeLibType(),
112 const_cast< void* >( _rData
.getValue() ), _rData
.getValueType().getTypeLibType(),
113 (uno_QueryInterfaceFunc
)cpp_queryInterface
, (uno_AcquireFunc
)cpp_acquire
, (uno_ReleaseFunc
)cpp_release
116 OStringBuffer
aBuf( 256 );
117 aBuf
.append("::utl::lcl_copyData( Accessor, Any ): could not assign the data (node path: ");
118 aBuf
.append( OUStringToOString( _rAccessor
.getPath(), RTL_TEXTENCODING_ASCII_US
) );
120 DBG_ASSERT( bSuccess
, aBuf
.getStr() );
124 DBG_WARNING( "::utl::lcl_copyData: NULL value lost!" );
129 // a simple assignment of an Any ...
130 *static_cast< Any
* >( _rAccessor
.getLocation() ) = _rData
;
140 void lcl_copyData( Any
& _rData
, const NodeValueAccessor
& _rAccessor
, ::osl::Mutex
& _rMutex
)
142 ::osl::MutexGuard
aGuard( _rMutex
);
144 DBG_ASSERT( _rAccessor
.isBound(), "::utl::lcl_copyData: invalid accessor!" );
145 switch ( _rAccessor
.getLocType() )
147 case ltSimplyObjectInstance
:
148 // a simple setValue ....
149 _rData
.setValue( _rAccessor
.getLocation(), _rAccessor
.getDataType() );
153 // a simple assignment of an Any ...
154 _rData
= *static_cast< Any
* >( _rAccessor
.getLocation() );
161 //= functors on NodeValueAccessor instances
163 /// base class for functors syncronizing between exchange locations and config sub nodes
164 struct SubNodeAccess
: public ::std::unary_function
< NodeValueAccessor
, void >
167 const OConfigurationNode
& m_rRootNode
;
168 ::osl::Mutex
& m_rMutex
;
171 SubNodeAccess( const OConfigurationNode
& _rRootNode
, ::osl::Mutex
& _rMutex
)
172 :m_rRootNode( _rRootNode
)
178 struct UpdateFromConfig
: public SubNodeAccess
181 UpdateFromConfig( const OConfigurationNode
& _rRootNode
, ::osl::Mutex
& _rMutex
) : SubNodeAccess( _rRootNode
, _rMutex
) { }
183 void operator() ( NodeValueAccessor
& _rAccessor
)
185 ::utl::lcl_copyData( _rAccessor
, m_rRootNode
.getNodeValue( _rAccessor
.getPath( ) ), m_rMutex
);
189 struct UpdateToConfig
: public SubNodeAccess
192 UpdateToConfig( const OConfigurationNode
& _rRootNode
, ::osl::Mutex
& _rMutex
) : SubNodeAccess( _rRootNode
, _rMutex
) { }
194 void operator() ( NodeValueAccessor
& _rAccessor
)
197 lcl_copyData( aNewValue
, _rAccessor
, m_rMutex
);
198 m_rRootNode
.setNodeValue( _rAccessor
.getPath( ), aNewValue
);
202 typedef std::vector
<NodeValueAccessor
> NodeValueAccessors
;
204 //= OConfigurationValueContainerImpl
206 struct OConfigurationValueContainerImpl
208 Reference
< XComponentContext
> xORB
; // the service factory
209 ::osl::Mutex
& rMutex
; // the mutex for accessing the data containers
210 OConfigurationTreeRoot aConfigRoot
; // the configuration node we're accessing
212 NodeValueAccessors aAccessors
; // the accessors to the node values
214 OConfigurationValueContainerImpl( const Reference
< XComponentContext
>& _rxORB
, ::osl::Mutex
& _rMutex
)
221 //= OConfigurationValueContainer
223 OConfigurationValueContainer::OConfigurationValueContainer(
224 const Reference
< XComponentContext
>& _rxORB
, ::osl::Mutex
& _rAccessSafety
,
225 const sal_Char
* _pConfigLocation
, const sal_uInt16 _nAccessFlags
, const sal_Int32 _nLevels
)
226 :m_pImpl( new OConfigurationValueContainerImpl( _rxORB
, _rAccessSafety
) )
228 implConstruct( OUString::createFromAscii( _pConfigLocation
), _nAccessFlags
, _nLevels
);
231 OConfigurationValueContainer::~OConfigurationValueContainer()
236 void OConfigurationValueContainer::implConstruct( const OUString
& _rConfigLocation
,
237 const sal_uInt16 _nAccessFlags
, const sal_Int32 _nLevels
)
239 DBG_ASSERT( !m_pImpl
->aConfigRoot
.isValid(), "OConfigurationValueContainer::implConstruct: already initialized!" );
241 // create the configuration node we're about to work with
242 m_pImpl
->aConfigRoot
= OConfigurationTreeRoot::createWithComponentContext(
246 ( _nAccessFlags
& CVC_UPDATE_ACCESS
) ? OConfigurationTreeRoot::CM_UPDATABLE
: OConfigurationTreeRoot::CM_READONLY
,
247 ( _nAccessFlags
& CVC_IMMEDIATE_UPDATE
) ? sal_False
: sal_True
250 OStringBuffer
aBuf(256);
251 aBuf
.append("Could not access the configuration node located at ");
252 aBuf
.append( OUStringToOString( _rConfigLocation
, RTL_TEXTENCODING_ASCII_US
) );
254 DBG_ASSERT( m_pImpl
->aConfigRoot
.isValid(), aBuf
.getStr() );
258 void OConfigurationValueContainer::registerExchangeLocation( const sal_Char
* _pRelativePath
,
259 void* _pContainer
, const Type
& _rValueType
)
262 DBG_ASSERT( _pContainer
, "OConfigurationValueContainer::registerExchangeLocation: invalid container location!" );
263 DBG_ASSERT( ( TypeClass_CHAR
== _rValueType
.getTypeClass( ) )
264 || ( TypeClass_BOOLEAN
== _rValueType
.getTypeClass( ) )
265 || ( TypeClass_BYTE
== _rValueType
.getTypeClass( ) )
266 || ( TypeClass_SHORT
== _rValueType
.getTypeClass( ) )
267 || ( TypeClass_LONG
== _rValueType
.getTypeClass( ) )
268 || ( TypeClass_DOUBLE
== _rValueType
.getTypeClass( ) )
269 || ( TypeClass_STRING
== _rValueType
.getTypeClass( ) )
270 || ( TypeClass_SEQUENCE
== _rValueType
.getTypeClass( ) ),
271 "OConfigurationValueContainer::registerExchangeLocation: invalid type!" );
273 // build an accessor for this container
274 NodeValueAccessor
aNewAccessor( OUString::createFromAscii( _pRelativePath
) );
275 aNewAccessor
.bind( _pContainer
, _rValueType
);
277 // insert it into our structure
278 implRegisterExchangeLocation( aNewAccessor
);
281 void OConfigurationValueContainer::read( )
284 m_pImpl
->aAccessors
.begin(),
285 m_pImpl
->aAccessors
.end(),
286 UpdateFromConfig( m_pImpl
->aConfigRoot
, m_pImpl
->rMutex
)
290 void OConfigurationValueContainer::write( bool _bCommit
)
292 // collect the current values in the exchange locations
294 m_pImpl
->aAccessors
.begin(),
295 m_pImpl
->aAccessors
.end(),
296 UpdateToConfig( m_pImpl
->aConfigRoot
, m_pImpl
->rMutex
)
299 // commit the changes done (if requested)
304 void OConfigurationValueContainer::commit( bool _bWrite
)
306 // write the current values in the exchange locations (if requested)
310 // commit the changes done
311 m_pImpl
->aConfigRoot
.commit( );
314 void OConfigurationValueContainer::implRegisterExchangeLocation( const NodeValueAccessor
& _rAccessor
)
317 DBG_ASSERT( !m_pImpl
->aConfigRoot
.isValid() || m_pImpl
->aConfigRoot
.hasByHierarchicalName( _rAccessor
.getPath() ),
318 "OConfigurationValueContainer::implRegisterExchangeLocation: invalid relative path!" );
321 // another check (should be the first container for this node)
322 NodeValueAccessors::const_iterator aExistent
= ::std::find(
323 m_pImpl
->aAccessors
.begin(),
324 m_pImpl
->aAccessors
.end(),
327 DBG_ASSERT( m_pImpl
->aAccessors
.end() == aExistent
, "OConfigurationValueContainer::implRegisterExchangeLocation: already registered a container for this subnode!" );
330 // remember the accessor
331 m_pImpl
->aAccessors
.push_back( _rAccessor
);
333 // and initially fill the value
334 lcl_copyData( _rAccessor
, m_pImpl
->aConfigRoot
.getNodeValue( _rAccessor
.getPath() ), m_pImpl
->rMutex
);
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */