summaryrefslogtreecommitdiff
path: root/project2/common/transform.cpp
blob: 46ca42e9edd15ed15a32a84057a34d651e1495e0 (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
#include "pch.hpp"
#include "transform.h"
#include "logger.h"
#include "ostreamWrapper.h"
#include "scriptStorage.h"
#include <boost/foreach.hpp>

class TransformTargetStorer : public Storer {
	public:
		TransformTargetStorer(TransformSource * ts) :
			transformSource(ts)
		{
		}
		boost::intrusive_ptr<IntrusivePtrBase> create(ScriptNodePtr p) const
		{
			return LoaderBase::getLoader<TransformTargetLoader, NotSupported>(p->get_name())->create(p, Scripted);
		}
		bool save(boost::intrusive_ptr<IntrusivePtrBase> o, ScriptNodePtr s)
		{
			TransformChainLinkPtr O = boost::dynamic_pointer_cast<TransformChainLink>(o);
			if (O) {
				transformSource->addTarget(O, s);
			}
			return O;
		}
	private:
		TransformSource * transformSource;
};

TransformSource::TransformSource()
{
}

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

TransformChainLink::~TransformChainLink()
{
}

typedef std::map<std::string, boost::shared_ptr<TransformLoader> > TransformLoaderMap;
void
TransformSource::addTarget(TransformChainLinkPtr tcl, ScriptNodePtr e)
{
	BOOST_FOREACH(const TransformLoaderMap::value_type & tl, *LoaderBase::objLoaders<TransformLoader>()) {
		TransformPtr t = tl.second->create();
		if (t->canTransform(this, tcl.get())) {
			if (e) {
				t->configure(e);
			}
			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() const
{
	BOOST_FOREACH(const Targets::value_type & t, targets) {
		t.second->transform(this, t.first.get());
		if (const TransformSource * tr = dynamic_cast<const TransformSource *>(t.first.get())) {
			tr->doTransforms();
		}
	}
}

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

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

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