yast2-storage

Enum.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Novell, Inc.
00003  *
00004  * All Rights Reserved.
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of version 2 of the GNU General Public License as published
00008  * by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful, but WITHOUT
00011  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013  * more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, contact Novell, Inc.
00017  *
00018  * To contact Novell about this file by physical or electronic mail, you may
00019  * find current contact information at www.novell.com.
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         // Comparisons must not be done with type of enum since the enum may
00067         // define comparison operators.
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