LibreOffice Module unotools (master) 1
configvaluecontainer.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <sal/log.hxx>
25#include <uno/data.h>
26#include <algorithm>
27#include <utility>
28#include <vector>
29
30namespace utl
31{
32
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::lang;
35
36 //= NodeValueAccessor
37
38 namespace {
39
40 enum class LocationType
41 {
42 SimplyObjectInstance,
43 Unbound
44 };
45
46 }
47
49 {
50 private:
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
55
56 public:
57 explicit NodeValueAccessor( OUString _aNodePath );
58
59 void bind( void* _pLocation, const Type& _rType );
60
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; }
66
67 bool operator == ( const NodeValueAccessor& rhs ) const;
68 };
69
71 :sRelativePath(std::move( _aNodePath ))
72 ,eLocationType( LocationType::Unbound )
73 ,pLocation( nullptr )
74 {
75 }
76
78 {
79 return ( sRelativePath == rhs.sRelativePath )
80 && ( eLocationType == rhs.eLocationType )
81 && ( pLocation == rhs.pLocation );
82 }
83
84 void NodeValueAccessor::bind( void* _pLocation, const Type& _rType )
85 {
86 SAL_WARN_IF(isBound(), "unotools.config", "NodeValueAccessor::bind: already bound!");
87
88 eLocationType = LocationType::SimplyObjectInstance;
89 pLocation = _pLocation;
90 aDataType = _rType;
91 }
92
93 static
94 void lcl_copyData( const NodeValueAccessor& _rAccessor, const Any& _rData, std::mutex& _rMutex )
95 {
96 std::unique_lock aGuard( _rMutex );
97
98 SAL_WARN_IF(!_rAccessor.isBound(), "unotools.config", "::utl::lcl_copyData: invalid accessor!");
99 switch ( _rAccessor.getLocType() )
100 {
101 case LocationType::SimplyObjectInstance:
102 {
103 if ( _rData.hasValue() )
104 {
105 // assign the value
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
110 );
111 SAL_WARN_IF(!bSuccess, "unotools.config",
112 "::utl::lcl_copyData( Accessor, Any ): could not assign the data (node path: \"" << _rAccessor.getPath() << "\"");
113 }
114 else {
115 SAL_INFO("unotools.config", "::utl::lcl_copyData: NULL value lost!");
116 }
117 }
118 break;
119 default:
120 break;
121 }
122 }
123
124 static
125 void lcl_copyData( Any& _rData, const NodeValueAccessor& _rAccessor, std::mutex& _rMutex )
126 {
127 std::unique_lock aGuard( _rMutex );
128
129 SAL_WARN_IF(!_rAccessor.isBound(), "unotools.config", "::utl::lcl_copyData: invalid accessor!" );
130 switch ( _rAccessor.getLocType() )
131 {
132 case LocationType::SimplyObjectInstance:
133 // a simple setValue...
134 _rData.setValue( _rAccessor.getLocation(), _rAccessor.getDataType() );
135 break;
136
137 default:
138 break;
139 }
140 }
141
142 //= functors on NodeValueAccessor instances
143
144 namespace {
145
147 struct SubNodeAccess
148 {
149 protected:
150 const OConfigurationNode& m_rRootNode;
152
153 public:
154 SubNodeAccess( const OConfigurationNode& _rRootNode, std::mutex& _rMutex )
155 :m_rRootNode( _rRootNode )
156 ,m_rMutex( _rMutex )
157 {
158 }
159 };
160
161 struct UpdateFromConfig : public SubNodeAccess
162 {
163 public:
164 UpdateFromConfig( const OConfigurationNode& _rRootNode, std::mutex& _rMutex ) : SubNodeAccess( _rRootNode, _rMutex ) { }
165
166 void operator() ( NodeValueAccessor const & _rAccessor )
167 {
168 ::utl::lcl_copyData( _rAccessor, m_rRootNode.getNodeValue( _rAccessor.getPath( ) ), m_rMutex );
169 }
170 };
171
172 struct UpdateToConfig : public SubNodeAccess
173 {
174 public:
175 UpdateToConfig( const OConfigurationNode& _rRootNode, std::mutex& _rMutex ) : SubNodeAccess( _rRootNode, _rMutex ) { }
176
177 void operator() ( NodeValueAccessor const & _rAccessor )
178 {
179 Any aNewValue;
180 lcl_copyData( aNewValue, _rAccessor, m_rMutex );
181 m_rRootNode.setNodeValue( _rAccessor.getPath( ), aNewValue );
182 }
183 };
184
185 }
186
187 //= OConfigurationValueContainerImpl
188
190 {
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
194
195 std::vector<NodeValueAccessor> aAccessors; // the accessors to the node values
196
197 OConfigurationValueContainerImpl( const Reference< XComponentContext >& _rxORB, std::mutex& _rMutex )
198 :xORB( _rxORB )
199 ,rMutex( _rMutex )
200 {
201 }
202 };
203
204 //= OConfigurationValueContainer
205
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 ) )
210 {
211 implConstruct( OUString::createFromAscii( _pConfigLocation ), _nLevels );
212 }
213
214 OConfigurationValueContainer::~OConfigurationValueContainer()
215 {
216 }
217
218 void OConfigurationValueContainer::implConstruct( const OUString& _rConfigLocation,
219 const sal_Int32 _nLevels )
220 {
221 SAL_WARN_IF(m_pImpl->aConfigRoot.isValid(), "unotools.config", "OConfigurationValueContainer::implConstruct: already initialized!");
222
223 // create the configuration node we're about to work with
225 m_pImpl->xORB,
226 _rConfigLocation,
227 _nLevels
228 );
229 SAL_WARN_IF(!m_pImpl->aConfigRoot.isValid(), "unotools.config",
230 "Could not access the configuration node located at " << _rConfigLocation);
231 }
232
233 void OConfigurationValueContainer::registerExchangeLocation( const char* _pRelativePath,
234 void* _pContainer, const Type& _rValueType )
235 {
236 // checks...
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( ) )),
247 "unotools.config",
248 "OConfigurationValueContainer::registerExchangeLocation: invalid type!" );
249
250 // build an accessor for this container
251 NodeValueAccessor aNewAccessor( OUString::createFromAscii( _pRelativePath ) );
252 aNewAccessor.bind( _pContainer, _rValueType );
253
254 // insert it into our structure
255 implRegisterExchangeLocation( aNewAccessor );
256 }
257
258 void OConfigurationValueContainer::read( )
259 {
260 std::for_each(
261 m_pImpl->aAccessors.begin(),
262 m_pImpl->aAccessors.end(),
263 UpdateFromConfig( m_pImpl->aConfigRoot, m_pImpl->rMutex )
264 );
265 }
266
267 void OConfigurationValueContainer::commit()
268 {
269 // write the current values in the exchange locations
270 std::for_each(
271 m_pImpl->aAccessors.begin(),
272 m_pImpl->aAccessors.end(),
273 UpdateToConfig( m_pImpl->aConfigRoot, m_pImpl->rMutex )
274 );
275
276 // commit the changes done
277 m_pImpl->aConfigRoot.commit( );
278 }
279
280 void OConfigurationValueContainer::implRegisterExchangeLocation( const NodeValueAccessor& _rAccessor )
281 {
282 // some checks
283 SAL_WARN_IF(m_pImpl->aConfigRoot.isValid() && !m_pImpl->aConfigRoot.hasByHierarchicalName(_rAccessor.getPath()),
284 "unotools.config",
285 "OConfigurationValueContainer::implRegisterExchangeLocation: invalid relative path!" );
286
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(),
291 _rAccessor)),
292 "unotools.config",
293 "OConfigurationValueContainer::implRegisterExchangeLocation: already registered a container for this subnode!" );
294
295 // remember the accessor
296 m_pImpl->aAccessors.push_back( _rAccessor );
297
298 // and initially fill the value
299 lcl_copyData( _rAccessor, m_pImpl->aConfigRoot.getNodeValue( _rAccessor.getPath() ), m_pImpl->rMutex );
300 }
301
302} // namespace utl
303
304/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
a specialized version of an OConfigurationNode, representing the root of a configuration sub treeOnly...
Definition: confignode.hxx:178
static OConfigurationTreeRoot createWithComponentContext(const css::uno::Reference< css::uno::XComponentContext > &_rxContext, const OUString &_rPath, sal_Int32 _nDepth=-1, CREATION_MODE _eMode=CM_UPDATABLE)
open a new top-level configuration nodeopens a new node which is the root if an own configuration sub...
Definition: confignode.cxx:539
const OConfigurationNode & m_rRootNode
std::mutex & m_rMutex
sal_Bool SAL_CALL uno_type_assignData(void *pDest, typelib_TypeDescriptionReference *pDestType, void *pSource, typelib_TypeDescriptionReference *pSourceType, uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
#define SAL_WARN_IF(condition, area, stream)
#define SAL_INFO(area, stream)
Type
static void lcl_copyData(Any &_rData, const NodeValueAccessor &_rAccessor, std::mutex &_rMutex)
static void lcl_copyData(const NodeValueAccessor &_rAccessor, const Any &_rData, std::mutex &_rMutex)
NodeValueAccessor(OUString _aNodePath)
void bind(void *_pLocation, const Type &_rType)
bool operator==(const NodeValueAccessor &rhs) const
LocationType getLocType() const
const OUString & getPath() const
const Type & getDataType() const
OConfigurationValueContainerImpl(const Reference< XComponentContext > &_rxORB, std::mutex &_rMutex)
std::vector< NodeValueAccessor > aAccessors
std::mutex mutex
Definition: textsearch.cxx:94