conversions.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <cstdint>
  2. #include <iostream>
  3. #include "cpptoml.h"
  4. int main()
  5. {
  6. auto root = cpptoml::make_table();
  7. root->insert("small-integer", int64_t{12});
  8. auto si = *root->get_as<int8_t>("small-integer");
  9. root->insert("small-integer2", si);
  10. try
  11. {
  12. root->insert("too-big", std::numeric_limits<uint64_t>::max());
  13. }
  14. catch (std::overflow_error&)
  15. {
  16. }
  17. root->insert("medium-integer", std::numeric_limits<int32_t>::max());
  18. try
  19. {
  20. root->get_as<int16_t>("medium-integer");
  21. }
  22. catch (std::overflow_error&)
  23. {
  24. }
  25. root->get_as<uint32_t>("medium-integer"); // signed as unsigned, checked
  26. root->insert("medium-negative", std::numeric_limits<int32_t>::min());
  27. try
  28. {
  29. root->get_as<int16_t>("medium-negative");
  30. }
  31. catch (std::underflow_error&)
  32. {
  33. }
  34. try
  35. {
  36. root->get_as<uint64_t>("medium-negative");
  37. }
  38. catch (std::underflow_error&)
  39. {
  40. }
  41. root->get_as<int64_t>("medium-negative");
  42. root->insert("float", 0.1f);
  43. root->get_as<double>("float");
  44. return 0;
  45. }