aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/vasp/source/obj_sync.cpp
blob: bfb02515e79f792df88e2762ae44908215567644 (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
/* 

VASP modular - vector assembling signal processor / objects for Max/MSP and PD

Copyright (c) 2002 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.  

*/

#include "main.h"
#include "classes.h"
#include "util.h"


/*! \class vasp_sync
	\remark \b vasp.sync
	\brief Waits for all inlets to be hit (by vasps/anything) to trigger output.
	\since 0.0.1
	\param cmdln.1 int - number of sync inlets
	\param inlet.1 vasp - is stored 
	\param inlet.1 bang - triggers output
	\param inlet.1 set - vasp to be stored 
	\param inlet.1 reset - clear all hit flags
	\param inlet.+n vasp/anything - sets hit flag
	\retval outlet.* vasp - stored vasps

	\todo Message for selection if only vasp input triggers (or any one).
	\todo Message for selection of manual or auto reset upon trigger
*/
class vasp_sync:
	public vasp_op
{
	FLEXT_HEADER_S(vasp_sync,vasp_op,Setup)

public:
	vasp_sync(I argc,const t_atom *argv):
		autoreset(true),vasponly(false)
	{
		I cnt = -1;
		if(argc) {
			if(CanbeInt(argv[0])) cnt = GetAInt(argv[0]);
			if(cnt <= 1) {
				post("%s - integer argument invalid: set to 2",thisName());
				cnt = 2;
			}
		}
		else cnt = 2;

		flags = new BL[cnt];
		stored = new Vasp[cnt-1];

		AddInAnything(cnt);
		AddOutAnything(cnt);
	}

	static V Setup(t_classid c)
	{
		FLEXT_CADDMETHOD_(c,0,"reset",m_reset);
	}

	virtual BL Init() 
	{ 
		BL ret = vasp_op::Init(); 
		m_reset(); 
		return ret; 
	}

	~vasp_sync()	
	{ 
		if(flags) delete[] flags; 
		if(stored) delete[] stored; 
	}

	V chkbang(I n,Vasp *a = NULL) 
	{
		if(a && n > 0) { 
			stored[n-1] = *a;
			delete a;
		}

		BL f = flags[n]; 
		flags[n] = true; 
		if(!f) { // flags have changed
			
			BL all = true;
			for(I i = 0; i < CntIn(); ++i) all = all && flags[i];

			if(all) {
				if(ref.Ok()) {
					for(I i = CntIn()-1; i > 0; --i) ToOutVasp(i,stored[i-1]);
					ToOutVasp(0,ref);
				}
				else ToOutBang(0);

				if(autoreset) m_reset();
			}
		}
	}

	virtual V m_bang() { chkbang(0); }

	V m_reset() 
	{ 
		for(I i = 0; i < CntIn(); ++i) flags[i] = false;
	}

	virtual bool m_method_(I inlet,const t_symbol *s,I argc,const t_atom *argv)
	{
		if(inlet > 0 && (!vasponly || s == sym_vasp)) {
			Vasp *a = new Vasp(argc,argv);
			chkbang(inlet,a);
			return true;
		}
		else
			return vasp_op::m_method_(inlet,s,argc,argv);
	}

	virtual V m_help() { post("%s - Synchronize a number of vasps (default 2)",thisName()); }
private:
	BL autoreset,vasponly;
	BL *flags;
	Vasp *stored;

	FLEXT_CALLBACK(m_reset)
};

FLEXT_LIB_V("vasp.sync, vasp",vasp_sync)