RDK Documentation (Open Sourced RDK Components)
Enum.h
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 /**
22 * @defgroup trm
23 * @{
24 * @defgroup common
25 * @{
26 **/
27 
28 
29 #ifndef TRM_ENUM_H_
30 #define TRM_ENUM_H_
31 
32 #include <string>
33 #include <iostream>
34 #include <vector>
35 #include <limits>
36 
37 #include "TRM.h"
38 TRM_BEGIN_NAMESPACE
39 
40 
41 template<class T>
42 class Enum {
43 public:
44  typedef const char * CString;
45  typedef typename T::EnumType eT;
46 
47  static const Enum<T> & at(eT t) {
48  //@TODO: use array indexing
49  typedef const std::vector<const Enum<T> * > CT;
50  CT & enums = T::getEnums();
51  for (typename CT::const_iterator it = enums.begin(); it != enums.end(); it++) {
52  if (((eT)t) == ((eT)(*it))) {
53  return *(*it);
54  }
55  }
56  return *(enums[0]);
57  }
58 
59  static const Enum<T> & at(const char * t) {
60  //@TODO: use std::map RB tree
61  typedef const std::vector<const Enum<T> * > CT;
62  CT & enums = T::getEnums();
63  for (typename CT::const_iterator it = enums.begin(); it != enums.end(); it++) {
64  if ((*it)->name.compare(t) == 0) {
65  return *(*it);
66  }
67  }
68  return *(enums[0]);
69  }
70 
71  Enum(const eT &value, const char *name) : value(value), name(name) {}
72 
73  operator eT (void) const {
74  return value;
75  }
76 
77  operator CString(void) const {
78  return name.c_str();
79  }
80 
81  bool operator == (const Enum<T> &that) const {
82  return this->value == that.value;
83  }
84 
85  bool operator != (const Enum<T> &that) const {
86  return this->value != that.value;
87  }
88 
89  void print(void) const {
90  std::cout << "[OBJ]Enum<" << T::klassName() << ">" << "[k" << name << "] = " << value << std::endl;
91  }
92 private:
93  eT value;
94  std::string name;
95 };
96 
97 
98 TRM_END_NAMESPACE
99 #endif
100 
101 
102 
103 /** @} */
104 /** @} */
TRM::Enum
Definition: Enum.h:42