Forums > Dev

How to prevent the creation of an object when bad arguments are typed ?

April 1, 2013 | 10:07 am

I’d like to be sure the object is not created at all if either argument’s value or type is not correct.

I would like the object to appear the same way as when an object is not found.

Thank you in advance.


April 1, 2013 | 4:41 pm

Oh I see. Merci !


April 11, 2013 | 2:14 am

Using only x = NULL was not enough. I tried to use the return 0 instruction instead of return x to prevent object creation when required. However it seems that it was necessary only in one case but I can’t figure out why…

void *rb_randomize_new(t_symbol *s, long argc, t_atom *argv)
 {
	 //t_rb_randomize *x ; // local variable (pointer to a t_rb_randomize data structure)
	 t_rb_randomize *x = NULL ;
	 if (x = (t_rb_randomize *)object_alloc(rb_randomize_class)) {

		srand (time(NULL)) ; // seed rand with time

		x->proxy = proxy_new(x, 1, &x->proxy_inletnum);	// fully-flexible inlet for any type (remember one default inlet is always provided)
        x->outlet = outlet_new(x, NULL);				// fully-flexible outlet for any type

		// initialize L and R inlet atoms to (int) 0
		atom_setlong(&x->l, 0);
		atom_setlong(&x->range, 0);

		long i;

		// object instantiation, NEW STYLE

		if (argc > 2) { // more than two arguments
			object_warn((t_object *)x, "only two first arguments will be used", argc);
			x = NULL;
		} else if (argc == 2) { // two arguments
			for (i = 0; i < 2; i++) {
				if (i == 0) { // first argument sets range
					switch ((argv + i)->a_type) {
						case A_LONG:
							atom_setlong(&x->range, atom_getlong(argv+i));
							break;
						case A_FLOAT:
							atom_setfloat(&x->range, atom_getfloat(argv+i));
						default:
							object_error((t_object *)x, "forbidden argument type (int or float only)");
							x = NULL;
							return 0;
							//rb_randomize_free(x);
							break;
					}

				}
				else { // second argument makes second inlet triggerable
					if ((argv + i)->a_type == A_LONG) {
						if (atom_getlong(argv+i) == 0 || atom_getlong(argv+i) == 1) {
							x->rangetriggers = atom_getlong(argv+i) ;
							return x;
						}
						else {
							x->rangetriggers = 0;
							object_error((t_object *)x, "second argument's value should be either 1 or 0");
							x = NULL;
							//return 0;
							//rb_randomize_free(x);
						}
					}
					else {
						x->rangetriggers = 0;
						object_error((t_object *)x, "forbidden argument type (int only)");
						x = NULL;
						//return 0;
						//rb_randomize_free(x);
					}

				}
			}
		}
		else { // only one argument
			switch (argv->a_type) {
				case A_LONG:
					atom_setlong(&x->range, atom_getlong(argv));
					return x;
					break;
				case A_FLOAT:
					atom_setfloat(&x->range, atom_getfloat(argv));
					//return x;
					break;
				default:
					object_error((t_object *)x, "forbidden argument type");
					x = NULL;
					//return 0;
					//rb_randomize_free(x);
					break;
			}

		}
	}
	//post(" new rb.randomize object instance added to patch...",0); // post important info to the max window when new instance is created
	return x;
 }

April 11, 2013 | 9:58 am

For your purposes using:

x = NULL;
return x;

or simply:

return 0;

makes absolutely no difference.
Both constructs will prevent the creation of the object.

- Luigi


April 11, 2013 | 2:21 pm

Thanks Nicolas, thanks Luigi.

Is the object_free mandatory here ?


April 11, 2013 | 2:49 pm

if you called object_alloc, you should call object_free:

if (x = object_alloc(whatever)) {

    // decide that arguments
    if (args == nogood) {
        object_free(x);
        x = NULL;
    }
}

return x;

April 13, 2013 | 10:33 am

This is a great example Nicolas. Thanks!


Viewing 7 posts - 1 through 7 (of 7 total)