summaryrefslogtreecommitdiff
path: root/project2/common/transform.cpp
blob: cf32ead11c70339d1ed0014c569407cd3e578b7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <pch.hpp>
#include "transform.h"
#include "logger.h"
#include "ostreamWrapper.h"
#include "scriptStorage.h"
#include <factory.impl.h>

class TransformTargetStorer : public Storer {
	public:
		TransformTargetStorer(TransformSource * ts) :
			transformSource(ts)
		{
		}
		std::shared_ptr<Something> create(std::shared_ptr<const ScriptNode> p) const
		{
			return TransformTargetFactory::createNew(p->get_name(), p, Scripted);
		}
		bool save(std::shared_ptr<Something> o, std::shared_ptr<const ScriptNode> s)
		{
			TransformChainLinkPtr O = std::dynamic_pointer_cast<TransformChainLink>(o);
			if (O) {
				transformSource->addTarget(O, NULL, s);
			}
			return (O != NULL);
		}
	private:
		TransformSource * transformSource;
};

TransformSource::TransformSource()
{
}

TransformSource::TransformSource(ScriptNodePtr s, ObjectSource os)
{
	if (os == Scripted) {
		s->script.lock()->loader.addLoadTarget(s, std::make_shared<TransformTargetStorer>(this));
	}
}

TransformChainLink::~TransformChainLink()
{
}

typedef std::map<std::string, std::shared_ptr<TransformFactory> > TransformFactoryMap;
void
TransformSource::addTarget(TransformChainLinkPtr tcl, ExecContext * ec, ScriptNodePtr e)
{
	for (const auto & tl : AdHoc::PluginManager::getDefault()->getAll<TransformFactory>()) {
		TransformPtr t = tl->implementation()->create();
		if (t->canTransform(this, tcl.get())) {
			if (e) {
				t->configure(e, ec);
			}
			targets[tcl] = t;
			return;
		}
	}
	throw NotSupported("Couldn't find a suitable transformation");
}

void
TransformSource::clearTargets()
{
	targets.clear();
}

void
TransformSource::removeTarget(TransformChainLinkPtr tcl)
{
	targets.erase(tcl);
}

void
TransformSource::doTransforms(ExecContext * ec) const
{
	for (const Targets::value_type & t : targets) {
		t.second->transform(this, t.first.get(), ec);
		if (const TransformSource * tr = dynamic_cast<const TransformSource *>(t.first.get())) {
			tr->doTransforms(ec);
		}
	}
}

const Targets &
TransformSource::getTargets() const
{
	return targets;
}

class TransformWritableContentToStdStream : public TransformImpl<WritableContent, ostreamWrapper> {
	public:
		void transform(const WritableContent * wc, ostreamWrapper * o, ExecContext * ec) const
		{
			wc->writeTo(o->strm, "UTF-8", ec);
		}
};
DECLARE_TRANSFORM(TransformWritableContentToStdStream);

class TransformStaticContentToStdStream : public TransformImpl<StaticContent, ostreamWrapper> {
	public:
		void transform(const StaticContent * wc, ostreamWrapper * o, ExecContext *) const
		{
			wc->writeTo(o->strm);
		}
};
DECLARE_TRANSFORM(TransformStaticContentToStdStream);

INSTANTIATEVOIDFACTORY(Transform);
INSTANTIATEFACTORY(TransformChainLink, std::shared_ptr<const ScriptNode>, ObjectSource);