[PD-dev] Flext and array allocation

Frank Barknecht fbar at footils.org
Thu Jul 31 10:25:13 CEST 2003


Hallo,
Thomas Grill hat gesagt: // Thomas Grill wrote:

> Ben wrote:
> 
> > I'm working on my first flext project. I'm slowly getting the hang on
> > things (c++ in particular) I need to create an array with the number of
> > elements specified by the creation arguments of the object. I also need this
> > array to be available to all functions in the class.
> 
> Of course then you'll have to know the size of the array at object creation
> time, either because it is constant (e.g. defined by a #define) or because
> you get it with the object creation arguments.

With flext, because it's C++, you can also use one of the resizeable
containers in the C++ STL. Depending on how you want to access them, a
"vector" or a "list" might be the right container to use. Then you
won't have to deal with memory management using new/delete. Just
define e.g. a vector as class member. Example (untested, too): 

	#include <vector>
	
	class myclass:
		public flext_base
		{
			FLEXT_HEADER(myclass,flext_base)

			public:
				   
				myclass(int size) {
					
					// Init vec with zeros:
					while (size--)
						vec.push_back(0.0);
					AddOutInt();
					FLEXT_ADDMETHOD_(0,"poke",poke);
				}

				// use default destructor!
				// ~myclass() {
				// }
				
				// get an indexed sample of the array
				void poke(int ix) {
					ToOutFloat(0,vec[ix]);
				}


				FLEXT_CALLBACK_I(poke)
				
			// vec could also be private:
			private:	
				vector<float> vec;
	};

	FLEXT_NEW_1("myclass",myclass,int)



This might not be necessary in your example but comes in handy for
more complex uses (like defining a vector<some_synth> to hold a
changing voice count of a synthesizer dynamically)

ciao
-- 
 Frank Barknecht                               _ ______footils.org__




More information about the Pd-dev mailing list