1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
///////////////////////////////////////////////////////////////////////////////
//
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is MP4v2.
//
// The Initial Developer of the Original Code is Kona Blend.
// Portions created by Kona Blend are Copyright (C) 2008.
// All Rights Reserved.
//
// Contributors:
// Kona Blend, kona8lend@@gmail.com
//
///////////////////////////////////////////////////////////////////////////////
#ifndef MP4V2_IMPL_ENUM_TCC
#define MP4V2_IMPL_ENUM_TCC
#include <sstream>
namespace mp4v2 { namespace impl {
///////////////////////////////////////////////////////////////////////////////
template <typename T, T UNDEFINED>
Enum<T,UNDEFINED>::Enum()
: mapToType ( _mapToType )
, mapToString ( _mapToString )
{
for( const Entry* p = data; p->type != UNDEFINED; p++ ) {
_mapToType.insert( typename MapToType::value_type( p->compact, p ));
_mapToString.insert( typename MapToString::value_type( p->type, p ));
}
}
//////////////////////////////////////////////////////////////////////////////
template <typename T, T UNDEFINED>
Enum<T,UNDEFINED>::~Enum()
{
}
//////////////////////////////////////////////////////////////////////////////
template <typename T, T UNDEFINED>
string
Enum<T,UNDEFINED>::toString( T value, bool formal ) const
{
string buffer;
return toString( value, buffer, formal );
}
//////////////////////////////////////////////////////////////////////////////
template <typename T, T UNDEFINED>
string&
Enum<T,UNDEFINED>::toString( T value, string& buffer, bool formal ) const
{
const typename MapToString::const_iterator found = _mapToString.find( value );
if( found != _mapToString.end() ) {
const Entry& entry = *(found->second);
buffer = formal ? entry.formal : entry.compact;
return buffer;
}
ostringstream oss;
oss << "UNDEFINED(" << value << ")";
buffer = oss.str();
return buffer;
}
//////////////////////////////////////////////////////////////////////////////
template <typename T, T UNDEFINED>
T
Enum<T,UNDEFINED>::toType( const string& value ) const
{
// if number perform enum lookup
int ivalue;
istringstream iss( value );
iss >> ivalue;
if( iss.rdstate() == ios::eofbit ) {
const typename MapToString::const_iterator found = _mapToString.find( static_cast<T>(ivalue) );
if( found != _mapToString.end() )
return found->second->type;
}
// exact match
const typename MapToType::const_iterator found = _mapToType.find( value );
if( found != _mapToType.end() )
return found->second->type;
// partial match
int matches = 0;
T matched = static_cast<T>( 0 );
const typename MapToType::const_iterator ie = _mapToType.end();
for( typename MapToType::const_iterator it = _mapToType.begin(); it != ie; it++ ) {
const Entry& entry = *(it->second);
if( entry.compact.find( value ) == 0 ) {
matches++;
matched = entry.type;
}
}
return (matches == 1) ? matched : UNDEFINED;
}
//////////////////////////////////////////////////////////////////////////////
}} // namespace mp4v2::impl
#endif // MP4V2_IMPL_ENUM_TCC
|