instance.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. template <class T>
  125. class AutoValueToString :
  126. public ValueToString,
  127. class_init::Registered<AutoValueToString<T>, T, ValueToString>
  128. {
  129. public:
  130. string operator()(any const& object) const override {
  131. auto value = any_cast<T>(object);
  132. std::ostringstream stream;
  133. stream << value;
  134. return stream.str();
  135. }
  136. };
  137. template <class T, char const* fmt_string>
  138. class FormatValueToString :
  139. public ValueToString,
  140. class_init::Registered<FormatValueToString<T, fmt_string>, T, ValueToString>
  141. {
  142. public:
  143. string operator()(any const& object) const override {
  144. auto value = any_cast<T>(object);
  145. return fmt::format(fmt_string, value);
  146. }
  147. };
  148. TYPE_INSTANCES_WO_RENDERABLE_AND_CUSTOM_IO(AutoValueToString)
  149. #define FORMAT_VALUE_SERIALIZE(Type, fmt_string) \
  150. static char const Type##_format_string[] = fmt_string; \
  151. template class FormatValueToString<Type, Type##_format_string>;
  152. FORMAT_VALUE_SERIALIZE(bool, "{}")
  153. template <typename T>
  154. struct FloatingValueToString :
  155. public ValueToString,
  156. class_init::Registered<FloatingValueToString<T>, T, ValueToString>
  157. {
  158. string operator()(any const& object) const override {
  159. auto value = any_cast<T>(object);
  160. if (value == 0)
  161. return "0";
  162. return "{0:.{1}g}"_format(value, std::numeric_limits<T>::digits10+(int)round(fabs(log10(fabs(value)))));
  163. }
  164. };
  165. template struct FloatingValueToString<double>;
  166. struct AffineValueToString :
  167. public ValueToString,
  168. private class_init::Registered<
  169. AffineValueToString,
  170. Geom::Affine,
  171. ValueToString
  172. >
  173. {
  174. string operator()(any const& object) const override {
  175. using namespace fmt::literals;
  176. auto v = any_cast<Geom::Affine>(object);
  177. return "matrix({}, {}, {}, {}, {}, {})"_format(
  178. v[0], v[1], v[2], v[3], v[4], v[5]
  179. );
  180. }
  181. };
  182. } // namespace serialize
  183. } // namespace rainynite::core