1 /*
2 GUIDO Library
3 Copyright (C) 2006 Grame
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
20 research@grame.fr
21
22 */
23
24 #ifdef WIN32
25 # pragma warning (disable : 4786)
26 #endif
27
28 #include <iostream>
29
30 #include "guidoExpFactory.h"
31
32 #include "guidoexpression.h"
33 #include "guidoScoreExpr.h"
34 #include "guidoNamedExpr.h"
35
36 using namespace std;
37 using namespace guido;
38
39 namespace guidolang
40 {
41
42 //______________________________________________________________________________
43 template<int elt>
44 class newNodeFunctor : public rfunctor<Sguidoexpression> {
45 public:
operator ()()46 Sguidoexpression operator ()() { return guidonode<elt>::create(); }
47 };
48
49 //______________________________________________________________________________
guidoExpFactory()50 guidoExpFactory::guidoExpFactory()
51 {
52 fMap["."] = new newNodeFunctor<kAbstract>;
53 fMap["@"] = new newNodeFunctor<kApply>;
54 fMap[":"] = new newNodeFunctor<kSeqOp>;
55 fMap["/"] = new newNodeFunctor<kParOp>;
56 fMap["-|"] = new newNodeFunctor<kHeadOp>;
57 fMap["|-"] = new newNodeFunctor<kTailOp>;
58 fMap["-/"] = new newNodeFunctor<kTopOp>;
59 fMap["/-"] = new newNodeFunctor<kTopOp>;
60 fMap["transp"] = new newNodeFunctor<kTransp>;
61 fMap["stretch"] = new newNodeFunctor<kStretch>;
62 fMap["ident"] = new newNodeFunctor<kIdent>;
63 fMap["group"] = new newNodeFunctor<kGrouped>;
64 }
65
66 //______________________________________________________________________________
create(const string & name) const67 Sguidoexpression guidoExpFactory::create(const string& name) const
68 {
69 map<std::string, NewNodeFunctor*>::const_iterator i = fMap.find( name );
70 if (i != fMap.end()) {
71 NewNodeFunctor* f= i->second;
72 if (f) {
73 Sguidoexpression elt = (*f)();
74 if (elt) elt->setName(name);
75 return elt;
76 }
77 }
78 cerr << "Sguidoelement factory::create called with unknown element \"" << name << "\"" << endl;
79 return 0;
80 }
81
82 //______________________________________________________________________________
create(guido::Sguidoelement & score) const83 Sguidoexpression guidoExpFactory::create(guido::Sguidoelement& score) const
84 {
85 SguidoScoreExpr exp = guidoScoreExpr::create (score);
86 if (exp) exp->setName("gmn");
87 return exp;
88 }
89
90 //______________________________________________________________________________
create(const std::string & name,Sguidoexpression & e) const91 Sguidoexpression guidoExpFactory::create(const std::string& name, Sguidoexpression& e) const
92 {
93 Sguidoexpression exp = create (name);
94 if (exp) exp->push(e);
95 return exp;
96 }
97
98 //______________________________________________________________________________
createNamed(const std::string & name,Sguidoexpression & e) const99 Sguidoexpression guidoExpFactory::createNamed(const std::string& name, Sguidoexpression& e) const
100 {
101 Sguidoexpression exp = guidoNamedExpr::create();
102 if (exp) {
103 if (exp) exp->setName(name);
104 exp->push(e);
105 }
106 return exp;
107 }
108
109 //______________________________________________________________________________
create(const std::string & name,Sguidoexpression & e1,Sguidoexpression & e2) const110 Sguidoexpression guidoExpFactory::create(const std::string& name, Sguidoexpression& e1, Sguidoexpression& e2) const
111 {
112 Sguidoexpression exp = create (name);
113 if (exp) {
114 exp->push(e1);
115 exp->push(e2);
116 }
117 return exp;
118 }
119
120 //______________________________________________________________________________
createAbstract(const std::string & name,Sguidoexpression & e1,Sguidoexpression & e2) const121 Sguidoexpression guidoExpFactory::createAbstract(const std::string& name, Sguidoexpression& e1, Sguidoexpression& e2) const
122 {
123 Sguidoexpression exp = create (name);
124 Sguidoexpression ident = create("ident", e1);
125 Sguidoexpression e = e2->replace(e1,ident);
126 if (exp && ident && e) {
127 #if 0
128 exp->push(e1);
129 exp->push(e2);
130 #else
131 exp->push(ident);
132 exp->push(e);
133 #endif
134 }
135 return exp;
136 }
137
138 } // namespace
139