Coverage Report

Created: 2025-11-09 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/alembic/lib/Alembic/Util/TokenMap.h
Line
Count
Source
1
//-*****************************************************************************
2
//
3
// Copyright (c) 2009-2015,
4
//  Sony Pictures Imageworks Inc. and
5
//  Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
6
//
7
// All rights reserved.
8
//
9
// Redistribution and use in source and binary forms, with or without
10
// modification, are permitted provided that the following conditions are
11
// met:
12
// *       Redistributions of source code must retain the above copyright
13
// notice, this list of conditions and the following disclaimer.
14
// *       Redistributions in binary form must reproduce the above
15
// copyright notice, this list of conditions and the following disclaimer
16
// in the documentation and/or other materials provided with the
17
// distribution.
18
// *       Neither the name of Industrial Light & Magic nor the names of
19
// its contributors may be used to endorse or promote products derived
20
// from this software without specific prior written permission.
21
//
22
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
//
34
//-*****************************************************************************
35
36
//-*****************************************************************************
37
//! \file Alembic/Util/TokenMap.h
38
//! \brief The header file containing the class definition for
39
//!     the \ref Alembic::Util::TokenMap class
40
//-*****************************************************************************
41
#ifndef Alembic_Util_TokenMap_h
42
#define Alembic_Util_TokenMap_h
43
44
#include <Alembic/Util/Export.h>
45
#include <Alembic/Util/Exception.h>
46
#include <Alembic/Util/Foundation.h>
47
48
namespace Alembic {
49
namespace Util {
50
namespace ALEMBIC_VERSION_NS {
51
52
//-*****************************************************************************
53
// TOKEN MAP
54
//
55
//! \brief A wrapper around std::map that serializes and deserializes the map
56
//!        into a doubly-tokenized string, usually of the form
57
//!        token=value;token=value;token=value;
58
//-*****************************************************************************
59
60
class ALEMBIC_EXPORT TokenMap
61
{
62
public:
63
    //-*************************************************************************
64
    // TYPEDEFS
65
    //-*************************************************************************
66
    //! The map_type is std::map<std::string,std::string>
67
    //! ...
68
    typedef std::map<std::string,std::string> map_type;
69
70
    //! key_type promoted from map_type::key_type, which is std::string
71
    //! ...
72
    typedef map_type::key_type key_type;
73
74
    //! data_type promoted from map_type::data_type, which is std::string
75
    //! ...
76
    // CJH: Not defined? typedef map_type::data_type data_type;
77
    typedef std::string data_type;
78
79
    //! value_type promoted from map_type::value_type, which is
80
    //! std::pair<std::string, std::string>
81
    typedef map_type::value_type value_type;
82
83
    //! iterator promoted from map_type::iterator
84
    //! ...
85
    typedef map_type::iterator iterator;
86
87
    //! const_iterator promoted from map_type::iterator
88
    //! ...
89
    typedef map_type::const_iterator const_iterator;
90
91
    //! reverse_iterator promoted from map_type::reverse_iterator
92
    //! ...
93
    typedef map_type::reverse_iterator reverse_iterator;
94
95
    //! const_reverse_iterator promoted from map_type::const_reverse_iterator
96
    //! ...
97
    typedef map_type::const_reverse_iterator const_reverse_iterator;
98
99
    //! reference promoted from map_type::reference
100
    //! ...
101
    typedef map_type::reference reference;
102
103
    //! const_reference promoted from map_type::const_reference
104
    //! ...
105
    typedef map_type::const_reference const_reference;
106
107
    //-*************************************************************************
108
    // CONSTRUCTORS
109
    //-*************************************************************************
110
    //! \brief Default constructor
111
    //!     Map is initialized with no entries. Values can be added using
112
    //!     The \ref set and \ref setUnique member functions
113
525k
    TokenMap() {}
114
115
    //-*************************************************************************
116
    //! \brief Explicit constructor
117
    //!     Map is initialized from given string, using the delimiter scheme
118
    //!     as presented. If the 'unique' bool is 'true', it will use
119
    //!     the \ref setUnique function, obeying the 'quiet' bool accordingly.
120
    //!     Otherwise it will use the \ref set function.
121
    explicit TokenMap( const std::string &config,
122
                       char pairSeparator = ';',
123
                       char assignSeparator = '=',
124
                       bool unique = false,
125
                       bool quiet = true )
126
0
    {
127
0
        if ( unique )
128
0
        {
129
0
            setUnique( config, pairSeparator, assignSeparator, quiet );
130
0
        }
131
0
        else
132
0
        {
133
0
            set( config, pairSeparator, assignSeparator );
134
0
        }
135
0
    }
136
137
    //-*************************************************************************
138
    //! Using default copy constructor
139
    //! ...
140
141
    //-*************************************************************************
142
    //! Using default assignment operator.
143
    //! ...
144
145
    //-*************************************************************************
146
    // SET
147
    //! \brief This function sets the token/value pairs in the map by
148
    //!     deserializing them from a doubly-delimited string.
149
    //!
150
    //! \details The delimiter scheme defaults to:
151
    //!     "token=value;token=value;token=value" but may be overridden
152
    //!     using the optional separator arguments. Values are added to the
153
    //!     map one-by-one, overwriting any values that were there before.
154
    //!     To avoid overwriting, use the \ref setUnique function, which can
155
    //!     silently or rigidly deal with conflicts
156
    void set( const std::string &config,
157
              char pairSeparator = ';',
158
              char assignSeparator = '=' );
159
160
    //-*************************************************************************
161
    // SET UNIQUE
162
    //! \brief This function sets only unique (not already stored) token/value
163
    //!     pairs by deserializing them from a doubly-delimited string.
164
    //!
165
    //! \details The delimiter scheme and rules are the same as \ref set ,
166
    //!     the main difference here is that the class will not overwrite
167
    //!     values that already exist. If the function is called with
168
    //!     the default value of 'true' for the 'quiet' parameter, it will
169
    //!     simply not write those values. Otherwise, it will throw a
170
    //!     \ref Alembic::Util::Exception
171
    void setUnique( const std::string &config,
172
                    char pairSeparator = ';',
173
                    char assignSeparator = '=',
174
                    bool quiet = true );
175
176
177
    //-*************************************************************************
178
    // GET
179
    //! \brief This function turns the map back into a doubly-tokenized string.
180
    //!
181
    //! \details The passed delimiters are used to delimit the string, and
182
    //!     they have default values. Checking is optionally performed
183
    //!     (based on the 'check' bool) to make sure neither the tokens nor
184
    //!     values contain the delimiter characters, and an
185
    //!     \ref Alembic::Util::Exception is thrown if a conflict is detected.
186
    std::string get( char pairSeparator = ';',
187
                     char assignSeparator = '=',
188
                     bool check = false ) const;
189
190
    //-*************************************************************************
191
    // CLEAR THE MAP
192
    //-*************************************************************************
193
512k
    void clear() { m_map.clear(); }
194
195
    //-*************************************************************************
196
    // INDIVIDUAL TOKEN ACCESS
197
    //-*************************************************************************
198
199
    //! \brief This function returns the number of pairs.
200
    //!        ...
201
0
    size_t size() const { return m_map.size(); }
202
203
    //! \brief This function returns whether the map contains an entry for
204
    //!     a particular token.
205
    bool tokenExists( const std::string &token ) const
206
0
    {
207
0
        return ( m_map.count( token ) > 0 );
208
0
    }
209
210
    //! \brief This function returns the string value associated with a
211
    //!     particular token, or the empty string "" if the map does not
212
    //!     contain this token-value pair.
213
    std::string value( const std::string &token ) const
214
882
    {
215
882
        const_iterator fiter = m_map.find( token );
216
882
        if ( fiter != m_map.end() )
217
313
        {
218
313
            return (*fiter).second;
219
313
        }
220
569
        else
221
569
        {
222
569
            return "";
223
569
        }
224
882
    }
225
226
    //! \brief This function is a shorthand for \ref value
227
    //!     It will not return a modifiable entry. To modify,
228
    //!     \ref setValue must be used.
229
    std::string operator[]( const std::string &token ) const
230
0
    {
231
0
        return value( token );
232
0
    }
233
234
    //! \brief This function sets the value of a token. It will either
235
    //!     add a new token-value pair if the map does not already contain
236
    //!     this token, or it will overwrite the value for this token if
237
    //!     it already exists. You can use the \ref tokenExists function
238
    //!     to manage uniqueness guarantees.
239
    void setValue( const std::string &keyStr,
240
                   const std::string &valueStr )
241
0
    {
242
0
        m_map[keyStr] = valueStr;
243
0
    }
244
245
    //-*************************************************************************
246
    // ITERATION
247
    //-*************************************************************************
248
249
    //! \brief same as std::map begin
250
    //!     Returns an \ref iterator corresponding to the beginning of the map
251
    //!     or the end of the map if the map is empty.
252
0
    iterator begin() { return m_map.begin(); }
253
254
    //! \brief same as std::map begin const
255
    //!     Returns a \ref const_iterator corresponding to the beginning of the
256
    //!     map or the end of the map if the map is empty.
257
0
    const_iterator begin() const { return m_map.begin(); }
258
259
    //! \brief same as std::map end
260
    //!     Returns an \ref iterator corresponding to the end of the map.
261
0
    iterator end() { return m_map.end(); }
262
263
    //! \brief same as std::map end const
264
    //!     Returns an \ref const_iterator corresponding to the end of the map.
265
0
    const_iterator end() const { return m_map.end(); }
266
267
    //-*************************************************************************
268
    // REVERSE ITERATION
269
    //-*************************************************************************
270
271
    //! \brief same as std::map rbegin
272
    //!     Returns an \ref reverse_iterator corresponding to the
273
    //!     reverse_beginning of the map or the reverse_end of the map
274
    //!     if the map is empty.
275
0
    reverse_iterator rbegin() { return m_map.rbegin(); }
276
277
    //! \brief same as std::map rbegin const
278
    //!     Returns a \ref const_reverse_iterator corresponding to the beginning
279
    //!     of the map or the end of the map if the map is empty.
280
0
    const_reverse_iterator rbegin() const { return m_map.rbegin(); }
281
282
    //! \brief same as std::map rend
283
    //!     Returns an \ref reverse_iterator corresponding to the
284
    //!     reverse end of the map.
285
0
    reverse_iterator rend() { return m_map.rend(); }
286
287
    //! \brief same as std::map rend const
288
    //!     Returns an \ref const_reverse_iterator corresponding to the end
289
    //!     of the map.
290
0
    const_reverse_iterator rend() const { return m_map.rend(); }
291
292
    //-*************************************************************************
293
    // COMPARISON
294
    //-*************************************************************************
295
296
    //! Return an exact match
297
    //! ...
298
    bool exactMatch( const TokenMap &iOther ) const
299
0
    {
300
0
        return m_map == iOther.m_map;
301
0
    }
302
303
protected:
304
    map_type m_map;
305
};
306
307
//-*****************************************************************************
308
inline bool operator==( const TokenMap &iA, const TokenMap &iB )
309
0
{
310
0
    return iA.exactMatch( iB );
311
0
}
312
313
} // End namespace ALEMBIC_VERSION_NS
314
315
using namespace ALEMBIC_VERSION_NS;
316
317
} // End namespace Util
318
} // End namespace Alembic
319
320
#endif