Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #ifndef __VOM_ENUM_H__ |
| 17 | #define __VOM_ENUM_H__ |
| 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | namespace VOM { |
| 22 | /** |
| 23 | * A template base class for all enum types. |
| 24 | * This enum type exists to associate an enum value with a string for |
| 25 | * display/debug purposes. |
| 26 | * Concrete enum types use the CRTP. Derived classes thus inherit this |
| 27 | * base's function, but are not polymorphic. |
| 28 | */ |
| 29 | template <typename T> |
| 30 | class enum_base |
| 31 | { |
| 32 | public: |
| 33 | /** |
Neale Ranns | bc3c126 | 2017-11-10 04:03:08 -0800 | [diff] [blame] | 34 | * convert to string format for debug purposes |
| 35 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 36 | const std::string& to_string() const { return (m_desc); } |
| 37 | |
| 38 | /** |
Neale Ranns | bc3c126 | 2017-11-10 04:03:08 -0800 | [diff] [blame] | 39 | * Comparison operator |
| 40 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 41 | bool operator==(const enum_base& e) const { return (e.m_value == m_value); } |
| 42 | |
| 43 | /** |
Neale Ranns | bc3c126 | 2017-11-10 04:03:08 -0800 | [diff] [blame] | 44 | * Assignment |
| 45 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 46 | enum_base& operator=(const enum_base& e) |
| 47 | { |
| 48 | m_value = e.m_value; |
| 49 | m_desc = e.m_desc; |
| 50 | |
| 51 | return (*this); |
| 52 | } |
| 53 | |
| 54 | /** |
Neale Ranns | bc3c126 | 2017-11-10 04:03:08 -0800 | [diff] [blame] | 55 | * Comparison operator |
| 56 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 57 | bool operator!=(const enum_base& e) const { return (e.m_value != m_value); } |
| 58 | |
| 59 | /** |
Neale Ranns | bc3c126 | 2017-11-10 04:03:08 -0800 | [diff] [blame] | 60 | * integer conversion operator |
| 61 | */ |
| 62 | operator int() const { return (m_value); } |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
Neale Ranns | bc3c126 | 2017-11-10 04:03:08 -0800 | [diff] [blame] | 65 | * Return the value of the enum - same as integer conversion |
| 66 | */ |
| 67 | int value() const { return (m_value); } |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 68 | |
| 69 | protected: |
| 70 | /** |
Neale Ranns | bc3c126 | 2017-11-10 04:03:08 -0800 | [diff] [blame] | 71 | * Constructor of an enum - takes value and string description |
| 72 | */ |
| 73 | enum_base(int value, const std::string desc) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 74 | : m_value(value) |
| 75 | , m_desc(desc) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | /** |
Neale Ranns | bc3c126 | 2017-11-10 04:03:08 -0800 | [diff] [blame] | 80 | * Constructor |
| 81 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 82 | virtual ~enum_base() {} |
| 83 | |
| 84 | private: |
| 85 | /** |
Neale Ranns | bc3c126 | 2017-11-10 04:03:08 -0800 | [diff] [blame] | 86 | * Integer value of the enum |
| 87 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 88 | int m_value; |
| 89 | |
| 90 | /** |
Neale Ranns | bc3c126 | 2017-11-10 04:03:08 -0800 | [diff] [blame] | 91 | * String description |
| 92 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 93 | std::string m_desc; |
| 94 | }; |
| 95 | }; |
| 96 | |
| 97 | /* |
| 98 | * fd.io coding-style-patch-verification: ON |
| 99 | * |
| 100 | * Local Variables: |
| 101 | * eval: (c-set-style "mozilla") |
| 102 | * End: |
| 103 | */ |
| 104 | |
| 105 | #endif |