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 <rtl/ustring.hxx>
21 #include <xmloff/xmltkmap.hxx>
22 #include <xmloff/xmltoken.hxx>
23 #include <boost/functional/hash.hpp>
25 #include <unordered_map>
28 using namespace ::xmloff::token
;
30 class SvXMLTokenMap_Impl
35 std::size_t operator()(const std::pair
<sal_uInt16
,OUString
> &pair
) const
38 boost::hash_combine(seed
, pair
.first
);
39 boost::hash_combine(seed
, pair
.second
.hashCode());
43 std::unordered_map
< std::pair
<sal_uInt16
, OUString
>,
44 sal_uInt16
, PairHash
> m_aPrefixAndNameToTokenMap
;
45 std::unordered_map
< sal_Int32
, sal_uInt16
> m_aFastTokenToTokenMap
;
48 void insert( const SvXMLTokenMapEntry
& rEntry
);
49 sal_uInt16
get( sal_uInt16 nKeyPrefix
, const OUString
& rLName
) const;
50 sal_uInt16
get( sal_Int32 nFastTok
) const;
53 void SvXMLTokenMap_Impl::insert( const SvXMLTokenMapEntry
& rEntry
)
55 bool inserted
= m_aPrefixAndNameToTokenMap
.insert( std::make_pair( std::make_pair( rEntry
.nPrefixKey
,
56 GetXMLToken( rEntry
.eLocalName
) ), rEntry
.nToken
) ).second
;
57 assert(inserted
&& "duplicate token");
59 if( rEntry
.nFastToken
)
61 bool inserted2
= m_aFastTokenToTokenMap
.insert( std::make_pair( rEntry
.nFastToken
, rEntry
.nToken
) ).second
;
62 assert(inserted2
&& "duplicate token");
67 sal_uInt16
SvXMLTokenMap_Impl::get( sal_uInt16 nKeyPrefix
, const OUString
& rLName
) const
69 auto aIter( m_aPrefixAndNameToTokenMap
.find( std::make_pair( nKeyPrefix
, rLName
) ) );
70 if ( aIter
!= m_aPrefixAndNameToTokenMap
.end() )
71 return (*aIter
).second
;
73 return XML_TOK_UNKNOWN
;
76 sal_uInt16
SvXMLTokenMap_Impl::get( sal_Int32 nFastTok
) const
78 auto aIter( m_aFastTokenToTokenMap
.find( nFastTok
) );
79 if ( aIter
!= m_aFastTokenToTokenMap
.end() )
80 return (*aIter
).second
;
82 return XML_TOK_UNKNOWN
;
85 SvXMLTokenMap::SvXMLTokenMap( const SvXMLTokenMapEntry
*pMap
)
86 : m_pImpl( new SvXMLTokenMap_Impl
)
88 while( pMap
->eLocalName
!= XML_TOKEN_INVALID
)
90 m_pImpl
->insert( *pMap
);
95 SvXMLTokenMap::~SvXMLTokenMap()
99 sal_uInt16
SvXMLTokenMap::Get( sal_uInt16 nKeyPrefix
,
100 const OUString
& rLName
) const
102 return m_pImpl
->get( nKeyPrefix
, rLName
);
105 sal_uInt16
SvXMLTokenMap::Get( sal_Int32 nFastTok
) const
107 return m_pImpl
->get( nFastTok
);
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */