aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/flext/tutorial/bind1/main.cpp
blob: debfa68aa2e3da96471e44a157d957f4a58a4621 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* 
flext tutorial - bind 1 

Copyright (c) 2003 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

-------------------------------------------------------------------------

This is an example of a simple object demonstrating method to symbol binding and message forwarding
*/


// include flext header
#include <flext.h>

// check for appropriate flext version
#if !defined(FLEXT_VERSION) || (FLEXT_VERSION < 400)
#error You need at least flext version 0.4.0
#endif


// define the class that stands for a pd/Max object

class bind1:
	// inherit from basic flext class
	public flext_base
{
	// obligatory flext header (class name,base class name) featuring a setup function
	FLEXT_HEADER_S(bind1,flext_base,setup)
 
public:
	// constructor with no arguments
	bind1()
	{ 
		// define inlets:
		// first inlet must always be of type anything (or signal for dsp objects)
		AddInAnything("message inlet");  // add one inlet for any message
		AddInAnything("forwarding inlet");  // add one inlet for any message	
		
		AddOutAnything("bound message");  // output received bound message
	}	

    /*
        no destructor necessary here:
        flext frees all eventually remaining bound symbols when the object is destroyed
        (but NOT the data that can be passed via the FLEXT_BINDMETHOD call!)
    */

protected:
	const t_symbol *bufname;
	buffer *buf;

	// bind object
	void m_bind(const t_symbol *s)
	{
		if(!Bind(s)) {
			post("%s (%s) - Binding failed",thisName(),GetString(thisTag()));
		}
	}  

	// unbind object
	void m_unbind(const t_symbol *s)
	{
		if(!Unbind(s)) {
			post("%s (%s) - Binding failed",thisName(),GetString(thisTag()));
		}
	}

	// bind method
	void m_bindmethod(const t_symbol *s)
	{
		if(!FLEXT_BINDMETHOD(s,m_bound,NULL)) {
			post("%s (%s) - Binding failed",thisName(),GetString(thisTag()));
		}
	}

	// unbind method
	void m_unbindmethod(const t_symbol *s)
	{
		if(!FLEXT_UNBINDMETHOD(s)) {
			post("%s (%s) - Binding failed",thisName(),GetString(thisTag()));
		}
	}

	// forward message
	void m_forward(const t_symbol *s,int argc,const t_atom *argv)
	{
		Forward(s,argc,argv);
	}

	// method for symbol-bound messages
	void m_bound(const t_symbol *sym,int argc,const t_atom *argv,void *data)
	{
		ToOutAnything(0,sym,argc,argv);
	}

	// method for binding test
	void m_test(float value)
	{
		post("%s - TEST METHOD: value %f",thisName(),value);
	}

private:
	static void setup(t_classid c)
	{
		// register methods
		
		FLEXT_CADDMETHOD_(c,0,"bind",m_bind);  // register method "bind" for inlet 0
		FLEXT_CADDMETHOD_(c,0,"unbind",m_unbind);  // register method "unbind" for inlet 0
		FLEXT_CADDMETHOD_(c,0,"bindmethod",m_bindmethod);  // register method "bindmethod" for inlet 0
		FLEXT_CADDMETHOD_(c,0,"unbindmethod",m_unbindmethod);  // register method "unbindmethod" for inlet 0

		FLEXT_CADDMETHOD_(c,0,"test",m_test);  // register method m_test for inlet 0

		FLEXT_CADDMETHOD(c,1,m_forward);  // register method m_forward for inlet 1
	}

	FLEXT_CALLBACK_S(m_bind)  // wrapper for method m_bind (with symbol argument)
	FLEXT_CALLBACK_S(m_unbind)  // wrapper for method m_unbind (with symbol argument)
	FLEXT_CALLBACK_S(m_bindmethod)  // wrapper for method m_bindmethod (with symbol argument)
	FLEXT_CALLBACK_S(m_unbindmethod)  // wrapper for method m_unbindmethod (with symbol argument)

	FLEXT_CALLBACK_A(m_forward) // wrapper for method m_forward (with anything argument)
	
	FLEXT_CALLBACK_AX(m_bound)  // wrapper for method m_bound (anything+data arguments)
	FLEXT_CALLBACK_F(m_test)    // wrapper for method m_test (one float argument)
};

// instantiate the class
FLEXT_NEW("bind1",bind1)