You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.3 KiB

  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. #ifndef CONFIG_MAP__H_
  24. #define CONFIG_MAP__H_
  25. #include <map>
  26. namespace UTIL
  27. {
  28. /**
  29. * A config value table is a list of native values (usually enums)
  30. * to a different set of values, for example, the values used to
  31. * represent the enum in a config file, or the index used to represent
  32. * it in a selection list.
  33. *
  34. * It can be important to decouple from the internal representation,
  35. * especially in the case of persistent config files, as adding,
  36. * removing or modifying the order of items internally can easily
  37. * result in configs being read incorrectly, and, even if otherwise
  38. * carefully managed, results in obsolete values being kept in enums
  39. * as placeholders.
  40. *
  41. * The first item in the list is used default if no matching value is
  42. * found during lookup.
  43. */
  44. template<typename T>
  45. using CFG_MAP = std::vector<std::pair<T, long> >;
  46. /**
  47. * The "native" type of a CFG_MAP: probably an enum type
  48. */
  49. template<typename MAP>
  50. using CFG_NATIVE_VAL = typename MAP::value_type::first_type;
  51. /**
  52. * Get the mapped config value (the one to write to file, or use in
  53. * an index) from the given native (probably enum) value.
  54. *
  55. * The default (first item) is returned if the value is not found
  56. * in the list.
  57. *
  58. * @param aMap the value-config mapping table
  59. * @param aVal the value to look up
  60. */
  61. template<typename MAP>
  62. static long GetConfigForVal( const MAP& aMap, CFG_NATIVE_VAL<MAP> aVal )
  63. {
  64. // default is first entry
  65. long aConf = aMap[0].second;
  66. for( const auto& mapping : aMap )
  67. {
  68. if( mapping.first == aVal )
  69. {
  70. aConf = mapping.second;
  71. break;
  72. }
  73. }
  74. return aConf;
  75. }
  76. /**
  77. * Get the native value corresponding to the config value
  78. * (read from file or UI, probably) and find it in the mapping table.
  79. *
  80. * The default item is returned if the mapping fails.
  81. *
  82. * @param aMap the value-config mapping table
  83. * @param aConf the config value to look up
  84. */
  85. template<typename MAP>
  86. static CFG_NATIVE_VAL<MAP> GetValFromConfig( const MAP& aMap, long aConf )
  87. {
  88. // default is first entry
  89. CFG_NATIVE_VAL<MAP> aVal = aMap[0].first;
  90. for( const auto& mapping : aMap )
  91. {
  92. if( mapping.second == aConf )
  93. {
  94. aVal = mapping.first;
  95. break;
  96. }
  97. }
  98. return aVal;
  99. }
  100. }
  101. #endif /* CONFIG_MAP__H_ */