instance.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* instance.cpp - explicit template instantiation
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <fmt/format.h>
  18. #include <core/node_info.h>
  19. #include <core/node/list.h>
  20. #include <core/type_info.h>
  21. #include <core/all_types.h>
  22. #include <core/serialize/node_writer.h>
  23. #include <core/time/parse.h>
  24. #include <core/time/format.h>
  25. #include <core/renderable.h>
  26. #include <core/document.h>
  27. #include <core/color/color.h>
  28. #include <core/nothing_io.h>
  29. #include <geom_helpers/knots_io.h>
  30. #include <geom_helpers/point_io.h>
  31. #include <geom_helpers/null_shape.h>
  32. #include <geom_helpers/rectangle.h>
  33. #include <geom_helpers/circle.h>
  34. #include <2geom/affine.h>
  35. namespace Geom {
  36. TYPE_INFO(BezierKnots, "BezierPath", [](auto&& s) {
  37. return parse_named_knots(s);
  38. });
  39. TYPE_INFO(Knot, "BezierKnot", [](auto&& s) {
  40. return parse_knot(s);
  41. });
  42. TYPE_INFO(Point, "Point", [](auto&& s) {
  43. return parse_point(s);
  44. });
  45. TYPE_INFO(NullShape, "NullShape", [](auto&& /*s*/) {
  46. return NullShape {};
  47. });
  48. TYPE_INFO(Rectangle, "Rectangle", [](auto&& s) {
  49. // TODO
  50. return Rectangle {};
  51. });
  52. TYPE_INFO(Circle, "Circle", [](auto&& s) {
  53. // TODO
  54. return Circle {};
  55. });
  56. TYPE_INFO(Affine, "Affine", [](auto&& s) {
  57. // TODO
  58. return Affine::identity();
  59. });
  60. } // namespace Geom
  61. namespace std {
  62. TYPE_INFO(string, "String", [](auto&& s) {
  63. return s;
  64. });
  65. } // namespace std
  66. namespace rainynite::core {
  67. namespace colors {
  68. TYPE_INFO(Color, "Color", [](auto&& s) {
  69. return parse_hex(s);
  70. });
  71. } // namespace color
  72. namespace nodes {
  73. TYPE_INFO(Nothing, "Nothing", [](auto&& /*s*/) {
  74. return Nothing();
  75. });
  76. TYPE_INFO(bool, "Boolean", [](auto&& s) {
  77. if (s == "true")
  78. return true;
  79. else if (s == "false")
  80. return false;
  81. throw serialize::DeserializationError("Cannot parse bool from \""+s+"\"");
  82. });
  83. TYPE_INFO(double, "Real", [](auto&& s) {
  84. // TODO: check correctness & locale issues
  85. return std::stod(s);
  86. });
  87. TYPE_INFO(TimePeriod, "TimePeriod", [](auto&& s) {
  88. return parse_time_period(s);
  89. });
  90. TYPE_INFO(Time, "Time", [](auto&& s) {
  91. return parse_time(s);
  92. });
  93. TYPE_INFO(Shading, "Shading", [](auto&& s) {
  94. // TODO
  95. return Shading::default_shading();
  96. });
  97. TYPE_INFO(Renderable, "Renderable", [](auto&& /*s*/) -> any {
  98. throw serialize::DeserializationError("Renderable type cannot be deserialized");
  99. });
  100. TYPE_INFO(Audio, "Audio", [](auto&& /*s*/) -> any {
  101. throw serialize::DeserializationError("Audio type cannot be deserialized");
  102. });
  103. TYPE_INFO(DocumentType, "Document", [](auto&& /*s*/) -> any {
  104. throw serialize::DeserializationError("Document type cannot be deserialized");
  105. });
  106. class ValueTypeInfoBase {
  107. public:
  108. virtual string operator()(any const& object) const = 0;
  109. };
  110. class ValueTypeInfo : public ValueTypeInfoBase, class_init::Registered<ValueTypeInfo, AbstractReference, ValueTypeInfoBase> {
  111. public:
  112. virtual string operator()(any const& object) const {
  113. auto value = any_cast<AbstractReference>(object);
  114. return get_primitive_type_name(value->get_type());
  115. }
  116. };
  117. NODE_INFO_TEMPLATE(Value, Value<T>, T);
  118. NODE_INFO_TEMPLATE(List, ListValue<T>, vector<T>);
  119. TYPE_INSTANCES_WO_RENDERABLE(ValueNodeInfo)
  120. TYPE_INSTANCES(ListNodeInfo)
  121. } // namespace nodes
  122. REGISTER_NODE(UntypedListValue);
  123. namespace serialize {
  124. TYPE_INSTANCES_WO_RENDERABLE_AND_CUSTOM_IO(AutoValueToString)
  125. struct AffineValueToString :
  126. public ValueToString,
  127. private class_init::Registered<
  128. AffineValueToString,
  129. Geom::Affine,
  130. ValueToString
  131. >
  132. {
  133. string operator()(any const& object) const override {
  134. using namespace fmt::literals;
  135. auto v = any_cast<Geom::Affine>(object);
  136. return "matrix({}, {}, {}, {}, {}, {})"_format(
  137. v[0], v[1], v[2], v[3], v[4], v[5]
  138. );
  139. }
  140. };
  141. } // namespace serialize
  142. } // namespace rainynite::core