1 /*
2 
3   Copyright (C) 2011 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 
19   Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
20   research@grame.fr
21 
22 */
23 
24 #ifndef __jsoncontrol__
25 #define __jsoncontrol__
26 
27 #include <ostream>
28 #include <string>
29 #include <map>
30 #include "jsonnode.h"
31 
32 namespace httpdfaust
33 {
34 
35 //--------------------------------------------------------------------------
36 /*!
37 	\brief a faust control is a terminal node and represents a faust parameter controler
38 */
39 template <typename C> class jsoncontrol : public jsonnode
40 {
41 	std::string fName;
42 	std::string fType;
43 	C fInit, fMin, fMax, fStep;
44 	TMetas fMeta;
45 
46 	public:
47 
create(const char * name,const char * type,C min,C max,const TMetas & m)48 	static Sjsonnode create (const char* name, const char* type, C min, C max, const TMetas& m)
49 			{ return new jsoncontrol (name, type, min, max, m); }
create(const char * name,const char * type,C init,C min,C max,C step,const TMetas & m)50 	static Sjsonnode create (const char* name, const char* type, C init, C min, C max, C step, const TMetas& m)
51 			{ return new jsoncontrol (name, type, init, min, max, step, m); }
create(const char * name,const char * type,const TMetas & m)52 	static Sjsonnode create (const char* name, const char* type, const TMetas& m)
53 			{ return new jsoncontrol (name, type, m); }
54 
print(std::ostream & out,jsonendl & eol)55 		virtual void	print(std::ostream& out, jsonendl& eol) const
56 		{
57 			bool button = (fType == "button") || (fType == "checkbox");
58 			bool bargraph = (fType == "vbargraph") || (fType == "hbargraph");
59 
60 			out << eol << "{"; eol++;
61 			out << eol << "\"type\": \"" << fType << "\",";
62 			out << eol << "\"label\": \"" << fName << "\",";
63 			out << eol << "\"address\": \"" << getAddress() << "\"";
64 			if (fMeta.size()) {
65 				out << "," << eol << "\"meta\": " << "[ "; eol++;
66 				TMetas::const_iterator i=fMeta.begin();
67 				while (true) {
68 					out << eol << "{ \"" << i->first << "\": \"" << i->second << "\"}";
69 					if (++i == fMeta.end()) break;
70 					out << ",";
71 				}
72 				out << --eol << "]";
73 			}
74 
75 			if (button) { out << --eol << "}"; return; }		// done for buttons
76 
77 			if (!bargraph) {
78 				out << "," << eol << "\"init\": \"" << fInit << "\"";
79 			}
80 			out << "," << eol << "\"min\": \"" << fMin << "\",";
81 			out << eol << "\"max\": \"" << fMax << "\"";
82 			if (!bargraph) {
83 				out << "," << eol << "\"step\": \"" << fStep << "\"";
84             }
85 			out << --eol << "}";
86 		}
87 
88 	protected:
89 
jsoncontrol(const char * name,const char * type,const TMetas & m)90 				 jsoncontrol(const char* name, const char* type, const TMetas& m)
91 					: fName(name), fType(type), fInit(0), fMin(0), fMax(1), fStep(1), fMeta(m) {}
92 
jsoncontrol(const char * name,const char * type,C min,C max,const TMetas & m)93 				 jsoncontrol(const char* name, const char* type, C min, C max, const TMetas& m)
94 					: fName(name), fType(type), fMin(min), fMax(max), fStep(0), fMeta(m) {}
95 
jsoncontrol(const char * name,const char * type,C init,C min,C max,C step,const TMetas & m)96 				 jsoncontrol(const char* name, const char* type, C init, C min, C max, C step, const TMetas& m)
97 					: fName(name), fType(type), fInit(init), fMin(min), fMax(max), fStep(step), fMeta(m) {}
98 
~jsoncontrol()99 		virtual ~jsoncontrol() {}
100 };
101 
102 } // end namespoace
103 
104 #endif
105