00001 /* 00002 * Copyright (C) 2006-2011, SRI International (R) 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU Lesser General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #include <vector> 00019 #include <algorithm> 00020 00021 #include <OpenKarto/MetaEnum.h> 00022 00023 namespace karto 00024 { 00025 00029 00030 struct MetaEnumPrivate 00031 { 00032 karto::String m_Name; 00033 00034 typedef std::vector<EnumPair> EnumPairVector; 00035 EnumPairVector m_EnumPairs; 00036 }; 00037 00041 00042 MetaEnum::MetaEnum(const karto::String& rName) 00043 : m_pPrivate(new MetaEnumPrivate) 00044 { 00045 m_pPrivate->m_Name = rName; 00046 } 00047 00048 MetaEnum::~MetaEnum() 00049 { 00050 delete m_pPrivate; 00051 } 00052 00053 kt_bool MetaEnum::HasName(const karto::String& rName) const 00054 { 00055 return std::find_if(m_pPrivate->m_EnumPairs.begin(), m_pPrivate->m_EnumPairs.end(), FindByName(rName)) != m_pPrivate->m_EnumPairs.end(); 00056 } 00057 00058 kt_bool MetaEnum::HasValue(kt_int64s value) const 00059 { 00060 return std::find_if(m_pPrivate->m_EnumPairs.begin(), m_pPrivate->m_EnumPairs.end(), FindByValue(value)) != m_pPrivate->m_EnumPairs.end(); 00061 } 00062 00063 kt_int64s MetaEnum::GetValue(const karto::String& rName) const 00064 { 00065 MetaEnumPrivate::EnumPairVector::const_iterator iter = std::find_if(m_pPrivate->m_EnumPairs.begin(), m_pPrivate->m_EnumPairs.end(), FindByName(rName)); 00066 if (iter == m_pPrivate->m_EnumPairs.end()) 00067 { 00068 assert(false); 00069 throw karto::Exception("No EnumPair with name: " +rName); 00070 } 00071 00072 return iter->value; 00073 } 00074 00075 const karto::String& MetaEnum::GetName() const 00076 { 00077 return m_pPrivate->m_Name; 00078 } 00079 00080 const karto::String& MetaEnum::GetName(kt_int64s value) const 00081 { 00082 MetaEnumPrivate::EnumPairVector::const_iterator iter = std::find_if(m_pPrivate->m_EnumPairs.begin(), m_pPrivate->m_EnumPairs.end(), FindByValue(value)); 00083 if (iter == m_pPrivate->m_EnumPairs.end()) 00084 { 00085 assert(false); 00086 throw karto::Exception("No EnumPair with value: " + karto::StringHelper::ToString(value)); 00087 } 00088 00089 return iter->name; 00090 } 00091 00092 kt_size_t MetaEnum::GetSize() const 00093 { 00094 return m_pPrivate->m_EnumPairs.size(); 00095 } 00096 00097 kt_bool MetaEnum::operator==(const MetaEnum& rOther) const 00098 { 00099 return m_pPrivate->m_Name == rOther.m_pPrivate->m_Name; 00100 } 00101 00102 kt_bool MetaEnum::operator!=(const MetaEnum& rOther) const 00103 { 00104 return m_pPrivate->m_Name != rOther.m_pPrivate->m_Name; 00105 } 00106 00107 const EnumPair& MetaEnum::GetPair(kt_size_t index) const 00108 { 00109 if (index >= m_pPrivate->m_EnumPairs.size()) 00110 { 00111 assert(false); 00112 throw karto::Exception("MetaEnum::GetPair() - Index out of range"); 00113 } 00114 00115 return m_pPrivate->m_EnumPairs[index]; 00116 } 00117 00118 void MetaEnum::AddEnumPair(const EnumPair& rPair) 00119 { 00120 m_pPrivate->m_EnumPairs.push_back(rPair); 00121 } 00122 00123 }