From 40f7d5cb525cb07f8feb60394795e5e2feeb8f75 Mon Sep 17 00:00:00 2001 From: Alex Shvartzkop Date: Thu, 5 Oct 2023 10:54:41 +0300 Subject: [PATCH] Add STRING_ANY_MAP. --- include/string_any_map.h | 115 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 include/string_any_map.h diff --git a/include/string_any_map.h b/include/string_any_map.h new file mode 100644 index 0000000000..517e96ba35 --- /dev/null +++ b/include/string_any_map.h @@ -0,0 +1,115 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2023 Alex Shvartzkop + * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef STRING_ANY_MAP_H_ +#define STRING_ANY_MAP_H_ + +#include +#include +#include + +#include + + +/** + * A name/value tuple with unique names and wxAny values. The names + * may be iterated alphabetically. + */ +class STRING_ANY_MAP : public std::map +{ + double m_iuScale; + +public: + + STRING_ANY_MAP( double aIUScale = 1.0 ) : m_iuScale( aIUScale ) {} + + template + bool get_to( const std::string& aKey, T& aVar ) const + { + if( !contains( aKey ) ) + return false; + + return at( aKey ).GetAs( &aVar ); + } + + template + bool get_to_iu( const std::string& aKey, T& aVar ) const + { + if( !contains( aKey ) ) + return false; + + const wxAny& value = at( aKey ); + + if( value.CheckType() || value.CheckType() || value.CheckType() + || value.CheckType() ) + { + double number; + + if( !value.GetAs( &number ) ) + return false; + + number *= m_iuScale; + aVar = number; + } + else + { + if( !value.GetAs( &aVar ) ) + return false; + } + + return true; + } + + template + void set( const std::string& aKey, const T& aVar ) + { + emplace( aKey, aVar ); + } + + template + void set_iu( const std::string& aKey, const T& aVar) + { + emplace( aKey, aVar / m_iuScale ); + } + + bool contains( const std::string& aKey ) const + { // + return find( aKey ) != end(); + } + + template + std::optional get_opt( const std::string& aKey ) const + { + if( contains( aKey ) ) + { + T val; + + if( !at( aKey ).GetAs( &val ) ) + return std::nullopt; + + return val; + } + + return std::nullopt; + } +}; + + +#endif // STRING_ANY_MAP_H_ \ No newline at end of file