123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- /* instance.cpp - explicit template instantiation
- * Copyright (C) 2017 caryoscelus
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <fmt/format.h>
- #include <core/node_info.h>
- #include <core/node/list.h>
- #include <core/type_info.h>
- #include <core/all_types.h>
- #include <core/serialize/node_writer.h>
- #include <core/time/parse.h>
- #include <core/time/format.h>
- #include <core/renderable.h>
- #include <core/document.h>
- #include <core/color/color.h>
- #include <core/nothing_io.h>
- #include <geom_helpers/knots_io.h>
- #include <geom_helpers/point_io.h>
- #include <geom_helpers/null_shape.h>
- #include <geom_helpers/rectangle.h>
- #include <geom_helpers/circle.h>
- #include <2geom/affine.h>
- namespace Geom {
- TYPE_INFO(BezierKnots, "BezierPath", [](auto&& s) {
- return parse_named_knots(s);
- });
- TYPE_INFO(Knot, "BezierKnot", [](auto&& s) {
- return parse_knot(s);
- });
- TYPE_INFO(Point, "Point", [](auto&& s) {
- return parse_point(s);
- });
- TYPE_INFO(NullShape, "NullShape", [](auto&& /*s*/) {
- return NullShape {};
- });
- TYPE_INFO(Rectangle, "Rectangle", [](auto&& s) {
- // TODO
- return Rectangle {};
- });
- TYPE_INFO(Circle, "Circle", [](auto&& s) {
- // TODO
- return Circle {};
- });
- TYPE_INFO(Affine, "Affine", [](auto&& s) {
- // TODO
- return Affine::identity();
- });
- } // namespace Geom
- namespace std {
- TYPE_INFO(string, "String", [](auto&& s) {
- return s;
- });
- } // namespace std
- namespace rainynite::core {
- namespace colors {
- TYPE_INFO(Color, "Color", [](auto&& s) {
- return parse_hex(s);
- });
- } // namespace color
- namespace nodes {
- TYPE_INFO(Nothing, "Nothing", [](auto&& /*s*/) {
- return Nothing();
- });
- TYPE_INFO(bool, "Boolean", [](auto&& s) {
- if (s == "true")
- return true;
- else if (s == "false")
- return false;
- throw serialize::DeserializationError("Cannot parse bool from \""+s+"\"");
- });
- TYPE_INFO(double, "Real", [](auto&& s) {
- // TODO: check correctness & locale issues
- return std::stod(s);
- });
- TYPE_INFO(TimePeriod, "TimePeriod", [](auto&& s) {
- return parse_time_period(s);
- });
- TYPE_INFO(Time, "Time", [](auto&& s) {
- return parse_time(s);
- });
- TYPE_INFO(Shading, "Shading", [](auto&& s) {
- // TODO
- return Shading::default_shading();
- });
- TYPE_INFO(Renderable, "Renderable", [](auto&& /*s*/) -> any {
- throw serialize::DeserializationError("Renderable type cannot be deserialized");
- });
- TYPE_INFO(Audio, "Audio", [](auto&& /*s*/) -> any {
- throw serialize::DeserializationError("Audio type cannot be deserialized");
- });
- TYPE_INFO(DocumentType, "Document", [](auto&& /*s*/) -> any {
- throw serialize::DeserializationError("Document type cannot be deserialized");
- });
- class ValueTypeInfoBase {
- public:
- virtual string operator()(any const& object) const = 0;
- };
- class ValueTypeInfo : public ValueTypeInfoBase, class_init::Registered<ValueTypeInfo, AbstractReference, ValueTypeInfoBase> {
- public:
- virtual string operator()(any const& object) const {
- auto value = any_cast<AbstractReference>(object);
- return get_primitive_type_name(value->get_type());
- }
- };
- NODE_INFO_TEMPLATE(Value, Value<T>, T);
- NODE_INFO_TEMPLATE(List, ListValue<T>, vector<T>);
- TYPE_INSTANCES_WO_RENDERABLE(ValueNodeInfo)
- TYPE_INSTANCES(ListNodeInfo)
- } // namespace nodes
- REGISTER_NODE(UntypedListValue);
- namespace serialize {
- template <class T>
- class AutoValueToString :
- public ValueToString,
- class_init::Registered<AutoValueToString<T>, T, ValueToString>
- {
- public:
- string operator()(any const& object) const override {
- auto value = any_cast<T>(object);
- std::ostringstream stream;
- stream << value;
- return stream.str();
- }
- };
- template <class T, char const* fmt_string>
- class FormatValueToString :
- public ValueToString,
- class_init::Registered<FormatValueToString<T, fmt_string>, T, ValueToString>
- {
- public:
- string operator()(any const& object) const override {
- auto value = any_cast<T>(object);
- return fmt::format(fmt_string, value);
- }
- };
- TYPE_INSTANCES_WO_RENDERABLE_AND_CUSTOM_IO(AutoValueToString)
- #define FORMAT_VALUE_SERIALIZE(Type, fmt_string) \
- static char const Type##_format_string[] = fmt_string; \
- template class FormatValueToString<Type, Type##_format_string>;
- FORMAT_VALUE_SERIALIZE(bool, "{}")
- template <typename T>
- struct FloatingValueToString :
- public ValueToString,
- class_init::Registered<FloatingValueToString<T>, T, ValueToString>
- {
- string operator()(any const& object) const override {
- auto value = any_cast<T>(object);
- if (value == 0)
- return "0";
- return "{0:.{1}g}"_format(value, std::numeric_limits<T>::digits10+(int)round(fabs(log10(fabs(value)))));
- }
- };
- template struct FloatingValueToString<double>;
- struct AffineValueToString :
- public ValueToString,
- private class_init::Registered<
- AffineValueToString,
- Geom::Affine,
- ValueToString
- >
- {
- string operator()(any const& object) const override {
- using namespace fmt::literals;
- auto v = any_cast<Geom::Affine>(object);
- return "matrix({}, {}, {}, {}, {}, {})"_format(
- v[0], v[1], v[2], v[3], v[4], v[5]
- );
- }
- };
- } // namespace serialize
- } // namespace rainynite::core
|