1 /************************************************************************
2  ************************************************************************
3     FAUST compiler
4     Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
5     ---------------------------------------------------------------------
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  ************************************************************************
20  ************************************************************************/
21 
22 #ifndef _JSON_INSTRUCTIONS_H
23 #define _JSON_INSTRUCTIONS_H
24 
25 #include <string>
26 #include <set>
27 
28 #include "faust/gui/JSONUI.h"
29 #include "instructions.hh"
30 #include "exception.hh"
31 
32 using namespace std;
33 
34 #ifdef WIN32
35 #pragma warning(disable : 4244)
36 #endif
37 
38 /*
39  FIR visitor to prepare the JSON representation.
40 */
41 
42 template <typename REAL>
43 struct JSONInstVisitor : public DispatchVisitor, public JSONUIReal<REAL> {
44     map<string, string> fPathTable; // Table : field_name, complete path
45     set<string> fControlPathSet;    // Set of already used control paths
46 
47     using DispatchVisitor::visit;
48 
checkPathJSONInstVisitor49     const string& checkPath(set<string>& table, const string& path)
50     {
51         if (table.find(path) != table.end()) {
52             throw faustexception("ERROR : path '" + path + "' is already used\n");
53         } else {
54             table.insert(path);
55         }
56         return path;
57     }
58 
JSONInstVisitorJSONInstVisitor59     JSONInstVisitor(const std::string& name, const std::string& filename, int inputs, int outputs, int sr_index,
60                     const std::string& sha_key, const std::string& dsp_code, const std::string& version,
61                     const std::string& compile_options, const std::vector<std::string>& library_list,
62                     const std::vector<std::string>& include_pathnames, int size,
63                     const std::map<std::string, int>& path_table)
64         : JSONUIReal<REAL>(name, filename, inputs, outputs, sr_index, sha_key, dsp_code, version, compile_options, library_list,
65                  include_pathnames, size, path_table)
66     {
67     }
68 
JSONInstVisitorJSONInstVisitor69     JSONInstVisitor(int inputs, int outputs) : JSONUIReal<REAL>(inputs, outputs) {}
70 
JSONInstVisitorJSONInstVisitor71     JSONInstVisitor() : JSONUIReal<REAL>() {}
72 
~JSONInstVisitorJSONInstVisitor73     virtual ~JSONInstVisitor() {}
74 
visitJSONInstVisitor75     virtual void visit(AddMetaDeclareInst* inst) { this->declare(NULL, inst->fKey.c_str(), inst->fValue.c_str()); }
76 
visitJSONInstVisitor77     virtual void visit(OpenboxInst* inst)
78     {
79         switch (inst->fOrient) {
80             case OpenboxInst::kVerticalBox:
81                 this->openVerticalBox(inst->fName.c_str());
82                 break;
83             case OpenboxInst::kHorizontalBox:
84                 this->openHorizontalBox(inst->fName.c_str());
85                 break;
86             case OpenboxInst::kTabBox:
87                 this->openTabBox(inst->fName.c_str());
88                 break;
89         }
90     }
91 
visitJSONInstVisitor92     virtual void visit(CloseboxInst* inst) { this->closeBox(); }
93 
visitJSONInstVisitor94     virtual void visit(AddButtonInst* inst)
95     {
96         switch (inst->fType) {
97             case AddButtonInst::kDefaultButton:
98                 this->addButton(inst->fLabel.c_str(), nullptr);
99                 break;
100             case AddButtonInst::kCheckButton:
101                 this->addCheckButton(inst->fLabel.c_str(), nullptr);
102                 break;
103             default:
104                 faustassert(false);
105                 break;
106         }
107         faustassert(fPathTable.find(inst->fZone) == fPathTable.end());
108         fPathTable[inst->fZone] = checkPath(fControlPathSet, this->buildPath(inst->fLabel));
109     }
110 
visitJSONInstVisitor111     virtual void visit(AddSliderInst* inst)
112     {
113         switch (inst->fType) {
114             case AddSliderInst::kHorizontal:
115                 this->addHorizontalSlider(inst->fLabel.c_str(), nullptr, inst->fInit, inst->fMin, inst->fMax, inst->fStep);
116                 break;
117             case AddSliderInst::kVertical:
118                 this->addVerticalSlider(inst->fLabel.c_str(), nullptr, inst->fInit, inst->fMin, inst->fMax, inst->fStep);
119                 break;
120             case AddSliderInst::kNumEntry:
121                 this->addNumEntry(inst->fLabel.c_str(), nullptr, inst->fInit, inst->fMin, inst->fMax, inst->fStep);
122                 break;
123             default:
124                 faustassert(false);
125                 break;
126         }
127         faustassert(fPathTable.find(inst->fZone) == fPathTable.end());
128         fPathTable[inst->fZone] = checkPath(fControlPathSet, this->buildPath(inst->fLabel));
129     }
130 
visitJSONInstVisitor131     virtual void visit(AddBargraphInst* inst)
132     {
133         switch (inst->fType) {
134             case AddBargraphInst::kHorizontal:
135                 this->addHorizontalBargraph(inst->fLabel.c_str(), nullptr, inst->fMin, inst->fMax);
136                 break;
137             case AddBargraphInst::kVertical:
138                 this->addVerticalBargraph(inst->fLabel.c_str(), nullptr, inst->fMin, inst->fMax);
139                 break;
140             default:
141                 faustassert(false);
142                 break;
143         }
144         faustassert(fPathTable.find(inst->fZone) == fPathTable.end());
145         fPathTable[inst->fZone] = checkPath(fControlPathSet, this->buildPath(inst->fLabel));
146     }
147 
visitJSONInstVisitor148     virtual void visit(AddSoundfileInst* inst)
149     {
150         this->addSoundfile(inst->fLabel.c_str(), inst->fURL.c_str(), nullptr);
151         faustassert(fPathTable.find(inst->fSFZone) == fPathTable.end());
152         fPathTable[inst->fSFZone] = checkPath(fControlPathSet, this->buildPath(inst->fLabel));
153     }
154 
setInputsJSONInstVisitor155     void setInputs(int input) { this->fInputs = input; }
setOutputsJSONInstVisitor156     void setOutputs(int output) { this->fOutputs = output; }
157 };
158 
159 #endif
160