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.

81 lines
2.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2021 Jon Evans <jon@craftyjon.com>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef KICAD_JSON_SETTINGS_INTERNALS_H
  21. #define KICAD_JSON_SETTINGS_INTERNALS_H
  22. #include <json_common.h>
  23. class KICOMMON_API JSON_SETTINGS_INTERNALS : public nlohmann::json
  24. {
  25. friend class JSON_SETTINGS;
  26. public:
  27. JSON_SETTINGS_INTERNALS() :
  28. nlohmann::json()
  29. {}
  30. /**
  31. * Builds a JSON pointer based on a given string
  32. * @param aPath is the path in the form "key1.key2.key3"
  33. * @return a JSON pointer that can be used to index into a JSON object
  34. */
  35. static nlohmann::json::json_pointer PointerFromString( std::string aPath );
  36. template<typename ValueType>
  37. void SetFromString( const std::string& aPath, ValueType aVal )
  38. {
  39. // Calls the overload below, which will convert from dotted string to JSON pointer
  40. ( *this )[aPath] = std::move( aVal );
  41. }
  42. template<typename ValueType>
  43. ValueType Get( const std::string& aPath ) const
  44. {
  45. return at( PointerFromString( aPath ) ).get<ValueType>();
  46. }
  47. nlohmann::json& At( const std::string& aPath )
  48. {
  49. return at( PointerFromString( aPath ) );
  50. }
  51. nlohmann::json& operator[]( const nlohmann::json::json_pointer& aPointer )
  52. {
  53. return nlohmann::json::operator[]( aPointer );
  54. }
  55. nlohmann::json& operator[]( const std::string& aPath )
  56. {
  57. return nlohmann::json::operator[]( PointerFromString( aPath ) );
  58. }
  59. void CloneFrom( const JSON_SETTINGS_INTERNALS& aOther )
  60. {
  61. nlohmann::json::json_pointer root( "" );
  62. this->nlohmann::json::operator[]( root ) = aOther.nlohmann::json::operator[]( root );
  63. }
  64. private:
  65. nlohmann::json m_original;
  66. };
  67. #endif // KICAD_JSON_SETTINGS_INTERNALS_H