|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #ifndef IRENUMMAPPER_H |
|
18 #define IRENUMMAPPER_H |
|
19 |
|
20 /*! |
|
21 * Convenience macros to help define enum maps. |
|
22 * Note! It is important to define the values so that the UI value is on the left |
|
23 * and the engine value is on the right. The lookup functions expect them in this order. |
|
24 * |
|
25 * Example: |
|
26 * BEGIN_ENUM_MAP( KSomeMap ) |
|
27 * ENUM_MAP_ITEM( UiNamespace::UiValue1, EngineNamespace::EEngineValue1 ), |
|
28 * ENUM_MAP_ITEM( UiNamespace::UiValue2, EngineNamespace::EEngineValue2 ) |
|
29 * END_ENUM_MAP( KSomeMap ) |
|
30 */ |
|
31 #define BEGIN_ENUM_MAP(name) const IREnumMapper::EnumMap name[] = { |
|
32 #define ENUM_MAP_ITEM(ui_enum, engine_enum ) { ui_enum, engine_enum } |
|
33 #define END_ENUM_MAP(name) }; const TInt name ## Count = sizeof ( name ) / sizeof ( name[0] ); |
|
34 |
|
35 /*! |
|
36 * Convenience macros to do the mappings |
|
37 */ |
|
38 #define MAP_TO_ENGINE_ENUM(uitype,enginetype,uienum, map) IREnumMapper::ToEngineEnum<uitype,enginetype>( uienum, map, map ## Count ) |
|
39 #define MAP_TO_UI_ENUM(uitype,enginetype,engineenum, map) IREnumMapper::ToUiEnum<uitype,enginetype>( engineenum, map, map ## Count ) |
|
40 |
|
41 class IREnumMapper |
|
42 { |
|
43 public: |
|
44 |
|
45 /*! |
|
46 * Item in the enum map. Ties a value in the UI enum to its correspondent in the engine enum |
|
47 */ |
|
48 struct EnumMap |
|
49 { |
|
50 int iUiEnum; |
|
51 int iEngineEnum; |
|
52 }; |
|
53 |
|
54 /*! |
|
55 * Maps engine enum value to its ui correspondent. |
|
56 * |
|
57 * @param aValue Enum value in the engine |
|
58 * @param aMap Enum value map |
|
59 * @param aCount Amount of items in the map |
|
60 * @return Enum value in the ui |
|
61 */ |
|
62 template<typename UI_ENUM, typename ENGINE_ENUM> |
|
63 inline static UI_ENUM ToUiEnum( ENGINE_ENUM aValue, const EnumMap* aMap, int aCount ) |
|
64 { |
|
65 return static_cast<UI_ENUM>( MapToUiEnum( aValue, aMap, aCount ) ); |
|
66 } |
|
67 |
|
68 /*! |
|
69 * Maps ui enum value to its engine correspondent. |
|
70 * |
|
71 * @param aValue Enum value in the Ui |
|
72 * @param aMap Enum value map |
|
73 * @param aCount Amount of items in the map |
|
74 * @return Enum value in the engine |
|
75 */ |
|
76 template<typename UI_ENUM, typename ENGINE_ENUM> |
|
77 inline static ENGINE_ENUM ToEngineEnum( UI_ENUM aValue, const EnumMap* aMap, int aCount ) |
|
78 { |
|
79 return static_cast<ENGINE_ENUM>( MapToEngineEnum( aValue, aMap, aCount ) ); |
|
80 } |
|
81 |
|
82 private: |
|
83 |
|
84 /*! |
|
85 * Maps engine enum value to its ui correspondent. |
|
86 * Type-unsafe version. Not to be used directly! |
|
87 * |
|
88 * @param aValue Enum value in the engine |
|
89 * @param aMap Enum value map |
|
90 * @param aCount Amount of items in the map |
|
91 * @return Enum value in the ui |
|
92 */ |
|
93 static int MapToUiEnum( int aValue, const EnumMap* aMap, int aCount ) |
|
94 { |
|
95 for ( int i = 0; i < aCount; ++i ) { |
|
96 if ( aMap[i].iEngineEnum == aValue ) { |
|
97 return aMap[i].iUiEnum; |
|
98 } |
|
99 } |
|
100 return -1; |
|
101 } |
|
102 |
|
103 /*! |
|
104 * Maps ui enum value to its engine correspondent. |
|
105 * Type-unsafe version. Not to be used directly! |
|
106 * |
|
107 * @param aValue Enum value in the Ui |
|
108 * @param aMap Enum value map |
|
109 * @param aCount Amount of items in the map |
|
110 * @return Enum value in the engine |
|
111 */ |
|
112 static int MapToEngineEnum( int aValue, const EnumMap* aMap, int aCount ) |
|
113 { |
|
114 for ( int i = 0; i < aCount; ++i ) { |
|
115 if ( aMap[i].iUiEnum == aValue ) { |
|
116 return aMap[i].iEngineEnum; |
|
117 } |
|
118 } |
|
119 return -1; |
|
120 } |
|
121 |
|
122 /* |
|
123 * Hidden constructor and destructor to prevent instantiation |
|
124 */ |
|
125 IREnumMapper() {} |
|
126 ~IREnumMapper() {} |
|
127 |
|
128 }; |
|
129 |
|
130 #endif // IRENUMMAPPER_H |