KiCad PCB EDA Suite
config_map.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 Kicad Developers, see change_log.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #ifndef CONFIG_MAP__H_
25 #define CONFIG_MAP__H_
26 
27 #include <map>
28 
29 namespace UTIL
30 {
31 
48 template<typename T>
49 using CFG_MAP = std::vector<std::pair<T, long> >;
50 
54 template<typename MAP>
55 using CFG_NATIVE_VAL = typename MAP::value_type::first_type;
56 
57 
68 template<typename MAP>
69 static long GetConfigForVal( const MAP& aMap, CFG_NATIVE_VAL<MAP> aVal )
70 {
71  // default is first entry
72  long aConf = aMap[0].second;
73 
74  for( const auto& mapping : aMap )
75  {
76  if( mapping.first == aVal )
77  {
78  aConf = mapping.second;
79  break;
80  }
81  }
82 
83  return aConf;
84 }
85 
95 template<typename MAP>
97  const MAP& aMap, long aConf )
98 {
99  // default is first entry
100  CFG_NATIVE_VAL<MAP> aVal = aMap[0].first;
101 
102  for( const auto& mapping : aMap )
103  {
104  if( mapping.second == aConf )
105  {
106  aVal = mapping.first;
107  break;
108  }
109  }
110 
111  return aVal;
112 }
113 
114 }
115 
116 #endif /* CONFIG_MAP__H_ */
typename MAP::value_type::first_type CFG_NATIVE_VAL
The "native" type of a CFG_MAP: probably an enum type.
Definition: config_map.h:55
static CFG_NATIVE_VAL< MAP > GetValFromConfig(const MAP &aMap, long aConf)
Get the native value corresponding to the config value (read from file or UI, probably) and find it i...
Definition: config_map.h:96
std::vector< std::pair< T, long > > CFG_MAP
A config value table is a list of native values (usually enums) to a different set of values...
Definition: config_map.h:49
static long GetConfigForVal(const MAP &aMap, CFG_NATIVE_VAL< MAP > aVal)
Get the mapped config value (the one to write to file, or use in an index) from the given native (pro...
Definition: config_map.h:69