00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef ENUM_H
00024 #define ENUM_H
00025
00026
00027 #include <assert.h>
00028 #include <string>
00029 #include <vector>
00030 #include <algorithm>
00031
00032 #include "storage/StorageInterface.h"
00033 #include "storage/AppUtil.h"
00034
00035
00036 namespace storage
00037 {
00038 using std::string;
00039 using std::vector;
00040
00041
00042 template <typename EnumType> struct EnumInfo {};
00043
00044 template <> struct EnumInfo<FsType> { static const vector<string> names; };
00045 template <> struct EnumInfo<PartitionType> { static const vector<string> names; };
00046 template <> struct EnumInfo<MountByType> { static const vector<string> names; };
00047 template <> struct EnumInfo<EncryptType> { static const vector<string> names; };
00048 template <> struct EnumInfo<MdType> { static const vector<string> names; };
00049 template <> struct EnumInfo<MdParity> { static const vector<string> names; };
00050 template <> struct EnumInfo<MdArrayState> { static const vector<string> names; };
00051 template <> struct EnumInfo<UsedByType> { static const vector<string> names; };
00052 template <> struct EnumInfo<CType> { static const vector<string> names; };
00053 template <> struct EnumInfo<Transport> { static const vector<string> names; };
00054 template <> struct EnumInfo<ImsmDriver> { static const vector<string> names; };
00055 template <> struct EnumInfo<MultipathAutostart> { static const vector<string> names; };
00056 template <> struct EnumInfo<PartAlign> { static const vector<string> names; };
00057
00058
00059 template <typename EnumType>
00060 const string& toString(EnumType value)
00061 {
00062 static_assert(std::is_enum<EnumType>::value, "not enum");
00063
00064 const vector<string>& names = EnumInfo<EnumType>::names;
00065
00066
00067
00068 assert((size_t)(value) < names.size());
00069
00070 return names[value];
00071 }
00072
00073
00074 template <typename EnumType>
00075 bool toValue(const string& str, EnumType& value, bool log_error = true)
00076 {
00077 static_assert(std::is_enum<EnumType>::value, "not enum");
00078
00079 const vector<string>& names = EnumInfo<EnumType>::names;
00080
00081 vector<string>::const_iterator it = find(names.begin(), names.end(), str);
00082
00083 if (it == names.end())
00084 {
00085 if (log_error)
00086 y2err("converting '" << str << "' to enum failed");
00087 return false;
00088 }
00089
00090 value = EnumType(it - names.begin());
00091 return true;
00092 }
00093
00094
00095 template <typename EnumType>
00096 EnumType toValueWithFallback(const string& str, EnumType fallback, bool log_error = true)
00097 {
00098 EnumType value;
00099
00100 if (toValue(str, value, log_error))
00101 return value;
00102
00103 return fallback;
00104 }
00105
00106 }
00107
00108
00109 #endif