Generics/Templates in Vex

   1713   6   2
User Avatar
Member
134 posts
Joined: Sept. 2021
Offline
Hi!

I have this Vex code:

float get_value_from_list(float list[]; int start_offset, int absolute_index) {
    int local_index = absolute_index - start_offset;
    return list[max(0, min(local_index, len(list) - 1))];
}


What if I want to make it so that my code works regardless of the datatype of the array? In C++, I know there's "templates" which allow you to write code that works with different data types without having to duplicate the implementation, e.g.


template <typename T>
T get_value_from_list(T list[], int start_offset, int absolute_index) {
    int local_index = absolute_index - start_offset;
    return list[max(0, min(local_index, sizeof(list) / sizeof(T) - 1))];
}

Can anything like this be done in Vex?

Thanks!
Anson
User Avatar
Member
9247 posts
Joined: July 2007
Offline
Sort of, you can use pre-processor #define() directive to do something similar

For example you can have a look at $HFS/houdini/vex/include/subdcurve.h
Tomas Slancik
CG Supervisor
Framestore, NY
User Avatar
Member
134 posts
Joined: Sept. 2021
Offline
Nice! Works great! For anyone else, this is what I had to switch my code to:

#define GENERIC_MACRO(TYPE) \
TYPE get_value_from_list(TYPE list[]; int start_offset, absolute_index) { \
    int local_index = absolute_index - start_offset; \
    return list[max(0, min(local_index, len(list) - 1))]; \
} \
// Apparently this blank line is important
GENERIC_MACRO(float)
GENERIC_MACRO(vector)
// etc, for all types you want to support.
#undef GENERIC_MACRO

Thanks again!
Anson
Edited by AnsonSavage - Aug. 10, 2023 14:38:29
User Avatar
Member
5041 posts
Joined: Feb. 2012
Offline
I have an RFE for generics in VEX. So if you want to see this feature, feel free to bump it up: RFE #59282.
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com] https://lnk.bio/animatrix [lnk.bio]
User Avatar
Member
134 posts
Joined: Sept. 2021
Offline
animatrix_
RFE #59282
I'd love to! I'm kind of new to this, where do I access that RFE? I couldn't figure it out.

Thanks,
Anson
User Avatar
Member
9247 posts
Joined: July 2007
Offline
AnsonSavage
animatrix_
RFE #59282
I'd love to! I'm kind of new to this, where do I access that RFE? I couldn't figure it out.

Thanks,
Anson
I don't think you can, I believe BUGs and RFEs are tied to an account so only profiles associated with that account can access them
which makes sense from the point of view that a lot of BUG/RFE reports can contain sensitive information


but you can always submit your own RFE and I believe they are being bundled together if they overlap
Tomas Slancik
CG Supervisor
Framestore, NY
User Avatar
Member
5041 posts
Joined: Feb. 2012
Offline
AnsonSavage
animatrix_
RFE #59282
I'd love to! I'm kind of new to this, where do I access that RFE? I couldn't figure it out.

Thanks,
Anson

You just have to create a new report and mention my RFE ID so they can link them.
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com] https://lnk.bio/animatrix [lnk.bio]
  • Quick Links