1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     util_SizedEnum.h
4 
5   Copyright (C) 2009-2011 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 27772 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_UTIL_UTIL_SIZEDENUM_H_
17 #define NN_UTIL_UTIL_SIZEDENUM_H_
18 
19 #include <nn/types.h>
20 #include <nn/util/util_StaticAssert.h>
21 
22 #ifdef __cplusplus
23 
24 namespace nn { namespace util {
25 
26 //---------------------------------------------------------------------------
27 //! :category   Utility class template.
28 //!
29 //! @tparam StorageT    Type to use for storing the <tt>enum</tt> value.
30 //! @tparam EnumT       Enumeration type of the target
31 //!
32 //! @brief  Class template for handling enumerated types with a fixed size.
33 //!
34 //! With this class, you can use <tt>enum</tt> values of explicit size.
35 //!
36 //! The <tt>EnumT</tt> type enumerated type is stored to a <tt>StorageT</tt> type variable for handling.
37 //---------------------------------------------------------------------------
38 template <typename StorageT, typename EnumT>
39 class SizedEnum
40 {
41 private:
42     typedef SizedEnum<StorageT, EnumT> Self;
43 //    <tt>NN_STATIC_ASSERT(sizeof(EnumT) <= sizeof(StorageT));</tt>
44 
45 private:
46     StorageT        m_EnumValue;
47 
48 public:
49     //---------------------------------------------------------------------------
50     //! @brief  Constructor.
51     //!
52     //! Constructor that does no initialization.
53     //---------------------------------------------------------------------------
SizedEnum()54     SizedEnum() {}
55 
56     //---------------------------------------------------------------------------
57     //! @brief
58     //!
59     //! Constructor that initializes using the specified initial value.
60     //!
61     //! @param[in]  e   Default instance values.
62     //---------------------------------------------------------------------------
SizedEnum(EnumT e)63     SizedEnum(EnumT e) { Set(e); }
64 
65 
66     //---------------------------------------------------------------------------
67     //! @brief  The type conversion operator for the enumeration type.
68     //!
69     //! It can directly read <tt>EnumT</tt> type values.
70     //---------------------------------------------------------------------------
EnumT()71     operator EnumT() const { return Get(); }
72 
73     //---------------------------------------------------------------------------
74     //! @brief  Assignment operator for assigning an enumerated type.
75     //!
76     //! With the assignment operator, you can directly assign an <tt>EnumT</tt> type value.
77     //!
78     //! @param[in]  e   New values to set.
79     //!
80     //! @return Returns a reference to self.
81     //---------------------------------------------------------------------------
82     Self& operator =(EnumT e) { Set(e); return *this; }
83 
84 
85     //---------------------------------------------------------------------------
86     //! @brief  Sets values.
87     //!
88     //! Updates the value being managed by this instance.
89     //!
90     //! @param[in]  e   New values to set.
91     //---------------------------------------------------------------------------
Set(EnumT e)92     void Set(EnumT e) { m_EnumValue = static_cast<EnumT>(e); }
93 
94     //---------------------------------------------------------------------------
95     //! @brief      Gets values.
96     //!
97     //! Gets the value being managed by this instance as an enumerated type.
98     //!
99     //! @return Returns the value of this instance.
100     //---------------------------------------------------------------------------
Get()101     EnumT Get() const { return static_cast<EnumT>(m_EnumValue); }
102 };
103 
104 //---------------------------------------------------------------------------
105 //! :category   Utility class template.
106 //!
107 //! @tparam EnumT       Enumeration type of the target
108 //!
109 //! @brief  Partially specialized class template for <tt>@ref SizedEnum</tt> of 1 byte.
110 //!
111 //! The class for handling <tt>EnumT</tt> type enumerated types as 1 byte.
112 //---------------------------------------------------------------------------
113 template <typename EnumT>
114 class SizedEnum1 : public SizedEnum<bit8, EnumT>
115 {
116 public:
117     //---------------------------------------------------------------------------
118     //! @brief  Constructor.
119     //!
120     //! Constructor that does no initialization.
121     //---------------------------------------------------------------------------
SizedEnum1()122     SizedEnum1() : SizedEnum<bit8, EnumT>() {}
123 
124     //---------------------------------------------------------------------------
125     //! @brief
126     //!
127     //! Constructor that initializes using the specified initial value.
128     //!
129     //! @param[in]  e   Default instance values.
130     //---------------------------------------------------------------------------
SizedEnum1(EnumT e)131     SizedEnum1(EnumT e) : SizedEnum<bit8, EnumT>(e) {}
132 };
133 
134 //---------------------------------------------------------------------------
135 //! :category   Utility class template.
136 //!
137 //! @tparam EnumT       Enumeration type of the target
138 //!
139 //! @brief  Partially specialized class template for <tt>@ref SizedEnum</tt> of 2 byte.
140 //!
141 //! The class for handling <tt>EnumT</tt> type enumerated types as 2 byte.
142 //---------------------------------------------------------------------------
143 template <typename EnumT>
144 class SizedEnum2 : public SizedEnum<bit16, EnumT>
145 {
146 public:
147     //---------------------------------------------------------------------------
148     //! @brief  Constructor.
149     //!
150     //! Constructor that does no initialization.
151     //---------------------------------------------------------------------------
SizedEnum2()152     SizedEnum2() : SizedEnum<bit16, EnumT>() {}
153 
154     //---------------------------------------------------------------------------
155     //! @brief
156     //!
157     //! Constructor that initializes using the specified initial value.
158     //!
159     //! @param[in]  e   Default instance values.
160     //---------------------------------------------------------------------------
SizedEnum2(EnumT e)161     SizedEnum2(EnumT e) : SizedEnum<bit16, EnumT>(e) {}
162 };
163 
164 //---------------------------------------------------------------------------
165 //! :category   Utility class template.
166 //!
167 //! @tparam EnumT       Enumeration type of the target
168 //!
169 //! @brief  Partially specialized class template for <tt>@ref SizedEnum</tt> of 4 byte.
170 //!
171 //! The class for handling <tt>EnumT</tt> type enumerated types as 4 byte.
172 //---------------------------------------------------------------------------
173 template <typename EnumT>
174 class SizedEnum4 : public SizedEnum<bit32, EnumT>
175 {
176 public:
177     //---------------------------------------------------------------------------
178     //! @brief  Constructor.
179     //!
180     //! Constructor that does no initialization.
181     //---------------------------------------------------------------------------
SizedEnum4()182     SizedEnum4() : SizedEnum<bit32, EnumT>() {}
183 
184     //---------------------------------------------------------------------------
185     //! @brief
186     //!
187     //! Constructor that initializes using the specified initial value.
188     //!
189     //! @param[in]  e   Default instance values.
190     //---------------------------------------------------------------------------
SizedEnum4(EnumT e)191     SizedEnum4(EnumT e) : SizedEnum<bit32, EnumT>(e) {}
192 };
193 
194 }}
195 
196 
197 #endif // __cplusplus
198 
199 #endif /* NN_UTIL_UTIL_SIZEDENUM_H_ */
200