项目作者: LaboratoryOfPlasmaPhysics

项目描述 :
convenient tree of variants
高级语言: C++
项目地址: git://github.com/LaboratoryOfPlasmaPhysics/cppdict.git
创建时间: 2019-01-22T19:28:11Z
项目社区:https://github.com/LaboratoryOfPlasmaPhysics/cppdict

开源协议:

下载


License: GPL v3
CPP17
C/C++ CI
Coverage

cppdict (C++Dict)

C++Dict is a C++ single header fexible nested dictionary where keys are std::string and values are any of user defined types plus dictionary type.
This allows you to build trees where each node has a name and leaves holds values of user defined types.

How to use

Basic example

  1. #include "dict.hpp"
  2. #include <iostream>
  3. #include <string>
  4. // define a dictionary type which can hold either int or double or std::string
  5. using MyDict = cppdict::Dict<int, double, std::string>;
  6. int main()
  7. {
  8. MyDict md;
  9. md["test"]["super"] = 2;
  10. std::cout << md["test"]["super"].to<int>() << "\n";
  11. cppdict::add("toto/tata/titi", 2.5, md);
  12. std::cout << "at toto/tata/titi : " << md["toto"]["tata"]["titi"].to<double>() << "\n";
  13. }

Using visitors

Given a node you can visit all it’s children and provide a specific function to handle each diferent child type.

  1. #include "dict.hpp"
  2. #include <iostream>
  3. #include <string>
  4. using MyDict = cppdict::Dict<int, double, std::string>;
  5. int main()
  6. {
  7. MyDict md;
  8. md["test"]["super"] = 2;
  9. md["PI"] = 3.14;
  10. md["Key"] = std::string{"Value"};
  11. md["key2"] = 2;
  12. md["Empty leaf"];
  13. md.visit(
  14. [](const std::string& key, int value) {
  15. std::cout << "key: " << key << "\tvalue: " << value << "(int)"
  16. << "\n";
  17. },
  18. [](const std::string& key, double value) {
  19. std::cout << "key: " << key << "\tvalue: " << value << "(double)"
  20. << "\n";
  21. },
  22. [](const std::string& key, const std::string& value) {
  23. std::cout << "key: " << key << "\tvalue: " << value << "(std::string)"
  24. << "\n";
  25. });
  26. // By default visit only exposes value type nodes, if you want to visit also other node types:
  27. md.visit(
  28. cppdict::visit_all_nodes,
  29. [](const std::string& key, const MyDict::node_t&) {
  30. std::cout << "key: " << key << " Got a node"
  31. << "\n";
  32. },
  33. [](const std::string& key, const MyDict::empty_leaf_t&) {
  34. std::cout << "key: " << key << " Got an empty leaf"
  35. << "\n";
  36. },
  37. [](const std::string& key, int value) {
  38. std::cout << "key: " << key << "\tvalue: " << value << "(int)"
  39. << "\n";
  40. },
  41. [](const std::string& key, double value) {
  42. std::cout << "key: " << key << "\tvalue: " << value << "(double)"
  43. << "\n";
  44. },
  45. [](const std::string& key, const std::string& value) {
  46. std::cout << "key: " << key << "\tvalue: " << value << "(std::string)"
  47. << "\n";
  48. });
  49. // You can also use auto to either skip or group handling of some types:
  50. md.visit(
  51. cppdict::visit_all_nodes,
  52. [](const std::string& key, const MyDict::node_t&) {
  53. std::cout << "key: " << key << " Got a node"
  54. << "\n";
  55. },
  56. [](const std::string& key, const MyDict::empty_leaf_t&) {
  57. std::cout << "key: " << key << " Got an empty leaf"
  58. << "\n";
  59. },
  60. [](const std::string& key, const auto& value) {
  61. std::cout << "key: " << key << "\tvalue: " << value << "(int|double|string)"
  62. << "\n";
  63. });
  64. }