aboutsummaryrefslogtreecommitdiff
path: root/PDContainer/src/h_deque.cpp
blob: 7ab9499ad003d93fc2dbc9112f37391cca16f460 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// *********************(c)*2004*********************>
// -holzilib--holzilib--holzilib--holzilib--holzilib->
// ++++PD-External++by+Georg+Holzmann++grh@gmx.at++++>
//
// PDContainer: 
// this is a port of the containers from the C++ STL
// (Standard Template Library)
// for usage see the documentation and PD help files
// for license see readme.txt
//
// h_deque.cpp


#include "include/HDeque.h"


static t_class *h_deque_class;
static t_class *proxy_class;

typedef struct _h_deque 
{
  t_object  x_obj;
  t_canvas *x_canvas;
  t_outlet *out0, *out1, *out2;
  HDeque *hdeque;
  Element value;
  bool event_set;
} t_h_deque;

typedef struct proxy
{
  t_object obj;
  t_int index;  // number of proxy inlet(s)
  t_h_deque *x;	// we'll put the other struct in here
} t_proxy;

static void h_deque_set(t_h_deque *x, t_symbol *s, int argc, t_atom *argv)
{
  int index;
  if(argc && (argv[0].a_type == A_FLOAT))
    index = static_cast<int>(argv[0].a_w.w_float);
  else
    {
      post("h_deque, set: invalid index!");
      return;
    }

  if ( index >= x->hdeque->getSize() || index < 0 )
    {
      post("h_deque, set: invalid index!");
      return;
    }

  if(!x->event_set)
    {
      post("h_deque, set: you must first set a value at right inlet!");
      return;
    }
  
  x->hdeque->set( index , x->value); 
  x->event_set = false;
}

static void h_deque_insert(t_h_deque *x, t_symbol *s, int argc, t_atom *argv)
{
  int index;
  if(argc && (argv[0].a_type == A_FLOAT))
    index = static_cast<int>(argv[0].a_w.w_float);
  else
    {
      post("h_deque, insert: invalid index!");
      return;
    }

  if ( index >= x->hdeque->getSize() || index < 0 )
    {
      post("h_deque, insert: invalid index!");
      return;
    }

  if(!x->event_set)
    {
      post("h_deque, insert: you must first set a value at right inlet!");
      return;
    }
  
  x->hdeque->insert( index , x->value); 
  x->event_set = false;
}

static void h_deque_value(t_proxy *p, t_symbol *s, int argc, t_atom *argv)
{
  t_h_deque *x = (t_h_deque *)(p->x);
  
  // symbol without selector "symbol":
  if(argc == 0)
  {
    t_atom tmp;
    SETSYMBOL(&tmp, s);
    x->value.setAtoms(1, &tmp);
    x->event_set = true;
    return;
  }

  // input is a list without selector "list":
  if ( argc && (strcmp(s->s_name,"list")!=0) 
       && (strcmp(s->s_name,"float")!=0) 
       && (strcmp(s->s_name,"symbol")!=0)
       && (strcmp(s->s_name,"pointer")!=0) )
  {
    t_atom *atoms = (t_atom*)getbytes( (argc+1)*sizeof(t_atom) );

      // add the selector symbol to the list:
    SETSYMBOL(atoms, s);

    for(int i=0; i<argc; i++)
    {
      if(argv[i].a_type == A_FLOAT)
	SETFLOAT(&atoms[i+1],argv[i].a_w.w_float);
      if(argv[i].a_type == A_SYMBOL)
	SETSYMBOL(&atoms[i+1],argv[i].a_w.w_symbol);
      if(argv[i].a_type == A_POINTER)
	SETPOINTER(&atoms[i+1],argv[i].a_w.w_gpointer);
    }

    x->value.setAtoms(argc+1, atoms);

    x->event_set = true;
    freebytes(atoms, (argc+1)*sizeof(t_atom));
    return;
  }

  // "normal" input (list, float or symbol):
  if (argc)
  {
    x->value.setAtoms(argc, argv);
    x->event_set = true;
    return;
  }
}

static void h_deque_get(t_h_deque *x, t_symbol *s, int argc, t_atom *argv)
{
  int index;
  if(argc && (argv[0].a_type == A_FLOAT))
    index = static_cast<int>(argv[0].a_w.w_float);
  else
    {
      post("h_deque, get: invalid index!");
      return;
    }

  if ( index >= x->hdeque->getSize() || index < 0 )
    {
      post("h_deque, get: invalid index!");
      return;
    }

  Element output = x->hdeque->get( index );
 
  if(output.getLength() == 1) // symbol or float
    {
      if (output.getAtom()[0].a_type == A_FLOAT)
	outlet_float(x->out0, output.getAtom()[0].a_w.w_float);
      if (output.getAtom()[0].a_type == A_SYMBOL)
	outlet_symbol(x->out0, output.getAtom()[0].a_w.w_symbol);
      if (output.getAtom()[0].a_type == A_POINTER)
	outlet_pointer(x->out0, output.getAtom()[0].a_w.w_gpointer);
      return;
    }
  if(output.getLength() > 1) // list
    {
      outlet_list(x->out0,&s_list,output.getLength(),output.getAtom());
      return;
    }

  // if there was no Element found, put out a bang at the right outlet
  outlet_bang(x->out2);
}

static void h_deque_push_back(t_h_deque *x, t_symbol *s, int argc, t_atom *argv)
{
  if(argc)
    {
      Element key(argc,argv);
      x->hdeque->pushBack(key);
    }
  else
    post("h_deque, pushback: no arguments");
}

static void h_deque_pop_back(t_h_deque *x)
{
  if(x->hdeque->getSize()<=0)
    {
      post("h_deque, popback: size is already 0 !");
      return;
    }

  x->hdeque->popBack();
}

static void h_deque_back(t_h_deque *x)
{
  if(x->hdeque->getSize() == 0)
    {
      post("h_deque, back: size is 0 !");
      return;
    }

  Element output = x->hdeque->back();
 
  if(output.getLength() == 1) // symbol or float
    {
      if (output.getAtom()[0].a_type == A_FLOAT)
	outlet_float(x->out0, output.getAtom()[0].a_w.w_float);
      if (output.getAtom()[0].a_type == A_SYMBOL)
	outlet_symbol(x->out0, output.getAtom()[0].a_w.w_symbol);
      if (output.getAtom()[0].a_type == A_POINTER)
	outlet_pointer(x->out0, output.getAtom()[0].a_w.w_gpointer);
      return;
    }
  if(output.getLength() > 1) // list
    {
      outlet_list(x->out0,&s_list,output.getLength(),output.getAtom());
      return;
    }

  // if there was no Element found, put out a bang at the right outlet
  outlet_bang(x->out2);
}

static void h_deque_push_front(t_h_deque *x, t_symbol *s, int argc, t_atom *argv)
{
  if(argc)
    {
      Element key(argc,argv);
      x->hdeque->pushFront(key);
    }
  else
    post("h_deque, pushfront: no arguments");
}

static void h_deque_pop_front(t_h_deque *x)
{
  if(x->hdeque->getSize()<=0)
    {
      post("h_deque, popfront: size is already 0 !");
      return;
    }

  x->hdeque->popFront();
}

static void h_deque_front(t_h_deque *x)
{
  if(x->hdeque->getSize() == 0)
    {
      post("h_deque, front: size is 0 !");
      return;
    }

  Element output = x->hdeque->front();
 
  if(output.getLength() == 1) // symbol or float
    {
      if (output.getAtom()[0].a_type == A_FLOAT)
	outlet_float(x->out0, output.getAtom()[0].a_w.w_float);
      if (output.getAtom()[0].a_type == A_SYMBOL)
	outlet_symbol(x->out0, output.getAtom()[0].a_w.w_symbol);
      if (output.getAtom()[0].a_type == A_POINTER)
	outlet_pointer(x->out0, output.getAtom()[0].a_w.w_gpointer);
      return;
    }
  if(output.getLength() > 1) // list
    {
      outlet_list(x->out0,&s_list,output.getLength(),output.getAtom());
      return;
    }

  // if there was no Element found, put out a bang at the right outlet
  outlet_bang(x->out2);
}

static void h_deque_remove(t_h_deque *x, t_symbol *s, int argc, t_atom *argv)
{
  int index;
  if(argc && (argv[0].a_type == A_FLOAT))
    index = static_cast<int>(argv[0].a_w.w_float);
  else
    {
      post("h_deque, remove: invalid index!");
      return;
    }

  if ( index >= x->hdeque->getSize() || index < 0 )
    {
      post("h_deque, remove: invalid index!");
      return;
    }
  
  x->hdeque->remove(index); 
  x->event_set = false;
}

static void h_deque_resize(t_h_deque *x, t_symbol *s, int argc, t_atom *argv)
{
  int size;
  if(argc && (argv[0].a_type == A_FLOAT))
    size = static_cast<int>(argv[0].a_w.w_float);
  else
    {
      post("h_deque, resize: invalid index!");
      return;
    }

  if ( size < 0 )
    {
      post("h_deque, size: invalid size!");
      return;
    }

  x->hdeque->resize( size );
}

static void h_deque_getsize(t_h_deque *x)
{
  outlet_float(x->out1,x->hdeque->getSize());
}

static void h_deque_help(t_h_deque *x)
{
  x->hdeque->help();
}

static void h_deque_set_namespace(t_h_deque *x, t_symbol *s)
{
  x->hdeque->setNamespace(s->s_name);
}

static void h_deque_get_namespace(t_h_deque *x)
{
  post("h_deque current namespace: %s",x->hdeque->getNamespace().c_str());
}

static void h_deque_getall(t_h_deque *x)
{
  deque<Element>::iterator iter  = x->hdeque->getAll().begin();
  
  while(iter != x->hdeque->getAll().end())
  {
    Element output = *iter;
 
    if(output.getLength() == 1) // symbol or float
    {
      if (output.getAtom()[0].a_type == A_FLOAT)
	outlet_float(x->out0, output.getAtom()[0].a_w.w_float);
      if (output.getAtom()[0].a_type == A_SYMBOL)
	outlet_symbol(x->out0, output.getAtom()[0].a_w.w_symbol);
      if (output.getAtom()[0].a_type == A_POINTER)
	outlet_pointer(x->out0, output.getAtom()[0].a_w.w_gpointer);
    }
    if(output.getLength() > 1) // list
      outlet_list(x->out0,&s_list,output.getLength(),output.getAtom());

    iter++;
  }
}

static void h_deque_print(t_h_deque *x)
{
  x->hdeque->printAllIndex();
}

static void h_deque_clear(t_h_deque *x)
{
  x->hdeque->clearNamespace();
}

static void h_deque_clear_all(t_h_deque *x)
{
  x->hdeque->clearAll();
}

static void h_deque_save(t_h_deque *x, t_symbol *s)
{
  // make correct path
  char filnam[MAXPDSTRING];
  char filename[MAXPDSTRING];
  canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING);
  sys_bashfilename(filnam, filename);

  if(x->hdeque->saveToFile(filename))
    post("h_deque: data of namespace %s written to file %s",
	 x->hdeque->getNamespace().c_str(),s->s_name);
  else
    post("h_deque: couldn't write to file %s",s->s_name);
}

static void h_deque_read(t_h_deque *x, t_symbol *s, int argc, t_atom *argv)
{
  // make correct path
  char filnam[MAXPDSTRING];
  char filename[MAXPDSTRING];
  canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING);
  sys_bashfilename(filnam, filename);

  if(!x->hdeque->readFromFile(filename))
    post("h_deque: couldn't read from file %s",s->s_name);
}

static void h_deque_read_at(t_h_deque *x, t_symbol *s, int argc, t_atom *argv)
{
  string symbol;
  int index=0;

  switch(argc)
    {
    default:
      post("h_deque read: only two argument are possible!");
      break;
    case 2:
      symbol = argv[0].a_w.w_symbol->s_name;
      index = (int)argv[1].a_w.w_float;
      break; 
    case 1:
      symbol = argv[0].a_w.w_symbol->s_name;
      index = 0;
      break;
    case 0:
      post("h_deque read: no filename!");
    }

  // make correct path
  char filnam[MAXPDSTRING];
  char filename[MAXPDSTRING];
  canvas_makefilename(x->x_canvas, (char*)symbol.c_str(), filnam, MAXPDSTRING);
  sys_bashfilename(filnam, filename);

  if(!x->hdeque->readFromFile2(filename,index))
    post("h_deque: couldn't read from file %s",s->s_name);
}

static void h_deque_save_xml(t_h_deque *x, t_symbol *s)
{
  // make correct path
  char filnam[MAXPDSTRING];
  char filename[MAXPDSTRING];
  canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING);
  sys_bashfilename(filnam, filename);

  if(x->hdeque->saveToFileXML(filename))
    post("h_deque: data of namespace %s written to file %s",
         x->hdeque->getNamespace().c_str(),s->s_name);
  else
    post("h_deque: couldn't write to file %s",s->s_name);
}

static void h_deque_read_xml(t_h_deque *x, t_symbol *s)
{
  // make correct path
  char filnam[MAXPDSTRING];
  char filename[MAXPDSTRING];
  canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING);
  sys_bashfilename(filnam, filename);

  if(!x->hdeque->readFromFileXML(filename))
    post("h_deque: couldn't read from file %s",s->s_name);
}

static void h_deque_read_at_xml(t_h_deque *x, t_symbol *s, int argc, t_atom *argv)
{
  string symbol;
  int index=0;

  switch(argc)
    {
    default:
      post("h_deque read: only two argument are possible!");
      break;
    case 2:
      symbol = argv[0].a_w.w_symbol->s_name;
      index = (int)argv[1].a_w.w_float;
      break; 
    case 1:
      symbol = argv[0].a_w.w_symbol->s_name;
      index = 0;
      break;
    case 0:
      post("h_deque read: no filename!");
    }

  // make correct path
  char filnam[MAXPDSTRING];
  char filename[MAXPDSTRING];
  canvas_makefilename(x->x_canvas, (char*)symbol.c_str(), filnam, MAXPDSTRING);
  sys_bashfilename(filnam, filename);

  if(!x->hdeque->readFromFile2XML(filename,index))
    post("h_deque: couldn't read from file %s",s->s_name);
}

static void *h_deque_new(t_symbol *s, int argc, t_atom *argv) 
{
  t_h_deque *x = (t_h_deque *)pd_new(h_deque_class);
  t_proxy *inlet = (t_proxy *)pd_new(proxy_class); // for the proxy inlet

  inlet->x = x;	// make x visible to the proxy inlets
  
  switch(argc)
    {
    default:
      post("h_deque warning: only one argument for namespace is possible!");
   case 1:
      x->hdeque = new HDeque(atom_getsymbol(argv)->s_name);
      break;
    case 0:
      x->hdeque = new HDeque();
      break;
    }

  // we are going to create a proxy inlet no. 0
  // it belongs to the object x but the destination is t_proxy
  inlet->index = 0;
  inlet_new(&x->x_obj, &inlet->obj.ob_pd, 0,0);

  x->out0 = outlet_new(&x->x_obj, 0);
  x->out1 = outlet_new(&x->x_obj, &s_float);
  x->out2 = outlet_new(&x->x_obj, &s_bang);
  x->x_canvas = canvas_getcurrent();
    
  return (void *)x;
}

static void *h_deque_free(t_h_deque *x)
{
  delete x->hdeque;
  return (void *)x;
}


#if defined(PDCONTAINER_SINGLE_OBJECT)
// for PD-Extended
extern "C" {
#endif

void h_deque_setup(void) 
{
  // the object class
  h_deque_class = class_new(gensym("h_deque"), (t_newmethod)h_deque_new,
				(t_method)h_deque_free, sizeof(t_h_deque), 
				CLASS_DEFAULT, A_GIMME, 0);

  // a class for the proxy-inlet
  proxy_class = class_new(gensym("h_deque_proxy"), NULL, NULL, sizeof(t_proxy),
			  CLASS_PD|CLASS_NOINLET, A_NULL);

  class_addmethod(h_deque_class, (t_method)h_deque_set, 
		  gensym("set"), A_GIMME, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_insert, 
		  gensym("insert"), A_GIMME, 0);
  class_addanything(proxy_class, (t_method)h_deque_value); // the right inlet
  class_addmethod(h_deque_class, (t_method)h_deque_get, 
		  gensym("get"), A_GIMME, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_push_back, 
		  gensym("pushback"), A_GIMME, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_pop_back, 
		  gensym("popback"), A_DEFFLOAT, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_back, 
		  gensym("back"), A_DEFFLOAT, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_push_front, 
		  gensym("pushfront"), A_GIMME, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_pop_front, 
		  gensym("popfront"), A_DEFFLOAT, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_front, 
		  gensym("front"), A_DEFFLOAT, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_remove, 
		  gensym("remove"), A_GIMME, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_resize, 
		  gensym("resize"), A_GIMME , 0);
  class_addmethod(h_deque_class, (t_method)h_deque_getsize, 
		  gensym("getsize"), A_DEFFLOAT , 0);
  class_addmethod(h_deque_class, (t_method)h_deque_set_namespace, 
		  gensym("namespace"), A_DEFSYMBOL , 0);
  class_addmethod(h_deque_class, (t_method)h_deque_get_namespace, 
		  gensym("getnamespace"), A_DEFFLOAT, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_getall,
		  gensym("getall"), A_DEFFLOAT, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_print,
		  gensym("print"), A_DEFFLOAT, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_clear,  
		  gensym("clear"), A_DEFFLOAT, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_clear_all,  
		  gensym("clearall"), A_DEFFLOAT, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_save, 
		  gensym("save"), A_DEFSYMBOL, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_read, 
		  gensym("read"), A_DEFSYMBOL, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_read_at, 
		  gensym("readat"), A_GIMME, 0);
  class_addmethod(h_deque_class, (t_method)h_deque_save_xml, 
		  gensym("saveXML"), A_DEFSYMBOL , 0);
  class_addmethod(h_deque_class, (t_method)h_deque_read_xml, 
		  gensym("readXML"), A_DEFSYMBOL , 0);
  class_addmethod(h_deque_class, (t_method)h_deque_read_at_xml, 
		  gensym("readatXML"), A_GIMME, 0);

  // without an argument the following two methods wont work ??? why?? because of c++?
  class_addmethod(h_deque_class, (t_method)h_deque_help, gensym("help"),A_DEFFLOAT, 0);
}

#if defined(PDCONTAINER_SINGLE_OBJECT)
// for PD-Extended
}
#endif