[Stk] typedef
Craig Sapp
craigsapp at gmail.com
Fri Sep 16 04:24:45 PDT 2011
Hello Thomas,
Function templates might work:
http://www.cplusplus.com/doc/tutorial/templates
although I have never tried using function pointers to template functions
before...
/////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
// template class where all of the work will be done:
template<class TYPE>
void printValue(TYPE value) {
cout << "The value is " << value << endl;
cout << "The squared value is: " << value * value << endl;
}
// create wrapper functions for each type needed:
void intcallback(int value) { printValue<int>(value); }
void floatcallback(float value) { printValue<float>(value); }
// two example functions which require different type of callback function:
void intaction(void* funptr, int value) {
void(*action)(int val) = (void (*)(int))funptr;
(*action)(value);
}
void floataction(void* funptr, float value) {
void(*action)(float val) = (void (*)(float))funptr;
(*action)(value);
}
// test run:
int main(void) {
intaction((void*)&intcallback, 55);
floataction((void*)&floatcallback, 2.4);
return 0;
}
/* output after running program:
The value is 55
The squared value is: 3025
The value is 2.4
The squared value is: 5.76
*/
/////////////////////////////////////////////////////////
#define pre-processor directives to create a macro might also work, but
probably not very useful to creating a complicated function inside of a
macro. Couldn't be used directly as a callback function (would use a
wrapper function as shown above).
////////////////////////////////////////
#include <iostream>
using namespace std;
#define printValue(type, value) {cout << (type)value << endl; }
int main(void) {
printValue(int, 5.5);
printValue(float, 5.5);
return 0;
}
/* Output from program:
5
5.5
*/
//////////////////////////////////////
-=+Craig
On Fri, Sep 16, 2011 at 2:56 AM, TJF <tjfoerster at web.de> wrote:
> Hi everybody,
>
> is there any idea how to make typedef of MY_TYPE variable (depending on
> "oput") in the following example? If I use more copies of the Callback
> function it works (output_1, output_2,...). But is there any other way
> in C++ to make this smarter?
>
> It must be defined during runtime. Is it possible to define it global
> (MY_TYPE_1, MY_TYPE_2,...) and do ... after oput ? I tried some ways
> with templates, but I didn't have success.
>
> ______________________________________
>
> // Callback
> int output( void *outputBuffer, void *inputBuffer, ...)
> {
> typedef float MY_TYPE;
> ...
> }
>
> int main( int argc, char *argv[] )
> {
> ...// here it depends on oput what kind of MY_TYPE will be used
> switch (oput)
> {
> case 1:
> try
> {
> openStream( ..., &output, ... ); // i.e. output with
> callback function , typedef double MY_TYPE
> ...
> }
> break;
> case 2:
> ...
> }
> ______________________________________
>
> Best regards
> Thomas
>
> _______________________________________________
> Stk mailing list
> Stk at ccrma.stanford.edu
> http://ccrma-mail.stanford.edu/mailman/listinfo/stk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://ccrma-mail.stanford.edu/pipermail/stk/attachments/20110916/3a9dafb3/attachment.html
More information about the Stk
mailing list