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 |
|
18 #ifndef RADIOENUMMAPPER_H_ |
|
19 #define RADIOENUMMAPPER_H_ |
|
20 |
|
21 /*! |
|
22 * Convenience macros to help define enum maps. |
|
23 * Note! It is important to define the values so that the UI value is on the left |
|
24 * and the engine value is on the right. The lookup functions expect them in this order. |
|
25 * |
|
26 * Example: |
|
27 * BEGIN_ENUM_MAP( KSomeMap ) |
|
28 * ENUM_MAP_ITEM( UiNamespace::UiValue1, EngineNamespace::EEngineValue1 ), |
|
29 * ENUM_MAP_ITEM( UiNamespace::UiValue2, EngineNamespace::EEngineValue2 ) |
|
30 * END_ENUM_MAP( KSomeMap ) |
|
31 */ |
|
32 #define BEGIN_ENUM_MAP(name) const RadioEnumMapper::EnumMap name[] = { |
|
33 #define ENUM_MAP_ITEM(ui_enum, engine_enum ) { ui_enum, engine_enum } |
|
34 #define END_ENUM_MAP(name) }; const TInt name ## Count = sizeof ( name ) / sizeof ( name[0] ); |
|
35 |
|
36 /*! |
|
37 * Convenience macros to do the mappings |
|
38 */ |
|
39 #define MAP_FROM_UI_ENUM(type,uienum, map) RadioEnumMapper::FromUiEnum<type>( uienum, map, map ## Count ) |
|
40 #define MAP_TO_UI_ENUM(type,uienum, map) RadioEnumMapper::ToUiEnum<type>( uienum, map, map ## Count ) |
|
41 |
|
42 class RadioEnumMapper |
|
43 { |
|
44 public: |
|
45 |
|
46 /*! |
|
47 * Item in the enum map. Ties a value in the UI enum to its correspondent in the engine enum |
|
48 */ |
|
49 struct EnumMap |
|
50 { |
|
51 int iUiEnum; |
|
52 int iEngineEnum; |
|
53 }; |
|
54 |
|
55 /*! |
|
56 * Maps engine enum value to its ui correspondent. |
|
57 * |
|
58 * @param aValue Enum value in the engine |
|
59 * @param aMap Enum value map |
|
60 * @param aCount Amount of items in the map |
|
61 * @return Enum value in the ui |
|
62 */ |
|
63 template<typename UI_ENUM, typename ENGINE_ENUM> |
|
64 inline static UI_ENUM ToUiEnum( ENGINE_ENUM aValue, const EnumMap* aMap, int aCount ) |
|
65 { |
|
66 return static_cast<UI_ENUM>( MapToUiEnum( aValue, aMap, aCount ) ); |
|
67 } |
|
68 |
|
69 /*! |
|
70 * Maps ui enum value to its engine correspondent. |
|
71 * |
|
72 * @param aValue Enum value in the Ui |
|
73 * @param aMap Enum value map |
|
74 * @param aCount Amount of items in the map |
|
75 * @return Enum value in the engine |
|
76 */ |
|
77 template<typename ENGINE_ENUM, typename UI_ENUM> |
|
78 inline static ENGINE_ENUM FromUiEnum( UI_ENUM aValue, const EnumMap* aMap, int aCount ) |
|
79 { |
|
80 return static_cast<ENGINE_ENUM>( MapFromUiEnum( aValue, aMap, aCount ) ); |
|
81 } |
|
82 |
|
83 private: |
|
84 |
|
85 /*! |
|
86 * Maps engine enum value to its ui correspondent. |
|
87 * Type-unsafe version. Not to be used directly! |
|
88 * |
|
89 * @param aValue Enum value in the engine |
|
90 * @param aMap Enum value map |
|
91 * @param aCount Amount of items in the map |
|
92 * @return Enum value in the ui |
|
93 */ |
|
94 static int MapToUiEnum( int aValue, const EnumMap* aMap, int aCount ) |
|
95 { |
|
96 for ( int i = 0; i < aCount; ++i ) { |
|
97 if ( aMap[i].iUiEnum == aValue ) { |
|
98 return aMap[i].iEngineEnum; |
|
99 } |
|
100 } |
|
101 return -1; |
|
102 } |
|
103 |
|
104 /*! |
|
105 * Maps ui enum value to its engine correspondent. |
|
106 * Type-unsafe version. Not to be used directly! |
|
107 * |
|
108 * @param aValue Enum value in the Ui |
|
109 * @param aMap Enum value map |
|
110 * @param aCount Amount of items in the map |
|
111 * @return Enum value in the engine |
|
112 */ |
|
113 static int MapFromUiEnum( int aValue, const EnumMap* aMap, int aCount ) |
|
114 { |
|
115 for ( int i = 0; i < aCount; ++i ) { |
|
116 if ( aMap[i].iEngineEnum == aValue ) { |
|
117 return aMap[i].iUiEnum; |
|
118 } |
|
119 } |
|
120 return -1; |
|
121 } |
|
122 |
|
123 /* |
|
124 * Hidden constructor and destructor to prevent instantiation |
|
125 */ |
|
126 RadioEnumMapper() {} |
|
127 ~RadioEnumMapper() {} |
|
128 |
|
129 }; |
|
130 |
|
131 #endif // RADIOENUMMAPPER_H_ |
|