Mini string library for REs

This forum is for developers of Rack Extensions to discuss the RE SDK, share code, and offer tips to other developers.
Post Reply
User avatar
pongasoft
RE Developer
Posts: 486
Joined: 21 Apr 2016
Location: Las Vegas
Contact:

02 Feb 2021

If you follow the internal RE dev forum, you probably saw that I was asking about how to do string manipulation mostly to dynamically build property name. The answer was, there is no such thing...

So I just spent over a week or so building a mini string library for REs and I have integrated it in my re-common mini framework. It is mind boggling that such a concept is not part of the SDK, especially since it is such a basic need for a developer. It is like if you were building a house and the first thing you have to do is build your own hammer...

The main API implemented is fmt::printf which implements a very simplified version of the C api printf (only handles %d and %s with no other variants allowed at the moment).

With this basic API, I built a jbox namespace with convenient calls to replace JBox_GetMotherboardObjectRef and JBox_MakePropertyRef.

Example:

Code: Select all

// from VerySimpleSampler.cpp (example in RE SDK)

// code with new api
// N = 4
for(int i = 0; i < N; i++)
{
   fSampleNativeObjectRefs[i] = jbox::get_property_ref(fMotherBoardCustomPropertiesRef, "sample_sound_native_object%d", i);
   fSampleZoneObjectRefs[i] = jbox::get_object_ref("/user_samples/%d", i);
}

// vs original code
fSampleNativeObjectRefs[0] = JBox_MakePropertyRef(fMotherBoardCustomPropertiesRef, "sample_sound_native_object0");
fSampleNativeObjectRefs[1] = JBox_MakePropertyRef(fMotherBoardCustomPropertiesRef, "sample_sound_native_object1");
fSampleNativeObjectRefs[2] = JBox_MakePropertyRef(fMotherBoardCustomPropertiesRef, "sample_sound_native_object2");
fSampleNativeObjectRefs[3] = JBox_MakePropertyRef(fMotherBoardCustomPropertiesRef, "sample_sound_native_object3");
fSampleZoneObjectRefs[0] = JBox_GetMotherboardObjectRef("/user_samples/0");
fSampleZoneObjectRefs[1] = JBox_GetMotherboardObjectRef("/user_samples/1");
fSampleZoneObjectRefs[2] = JBox_GetMotherboardObjectRef("/user_samples/2");
fSampleZoneObjectRefs[3] = JBox_GetMotherboardObjectRef("/user_samples/3");


I also implemented a StaticString class which encapsulates a C-Style null terminated char array (note that in debug mode it uses std::array so it is safer, and in release mode it uses char[] which is faster) and defined the 3 main types as such:

Code: Select all

using ObjectPath = StaticString<kJBox_MaxObjectNameLen + 1>;
using PropertyName = StaticString<kJBox_MaxPropertyNameLen + 1>;
using PropertyPath = StaticString<kMaxPropertyPathLen + 1>;
Since this class can be implicitly cast to a "char const *" it can be used wherever a "char const *" is needed...

Code: Select all

JBox_GetMotherboardObjectRef(ObjectPath::printf("/user_samples/%d", 3));

// note that this is exactly what jbox::get_object_ref does ;)
If you want to use in your own project you can obviously depend on re-common. If you only want the library you can copy the files fmt.h, jbox.h/jbox.cpp and StaticString.h into your own project. They are not dependent on the rest of re-common. If you only care about fmt::printf then you can simply copy fmt.h only.

Here is the commit that shows the simplification that it brings to re-cva-7 (which had to initialize 256 properties just for the display by hand...)

Yan

User avatar
meowsqueak
RE Developer
Posts: 50
Joined: 21 Jan 2015

02 Feb 2021

Nice work. I had to do a similar thing back in 2012 for AutoTheory which had lots of enumerated property names.

If I'd known you were going to write your own I would have just given you my library though - sorry!

User avatar
Billy+
Posts: 4220
Joined: 09 Dec 2016

02 Feb 2021

I guess this is the beauty of allowing open discussion of the sdk.

And thanks for autotheory I got it straight away when it came out and it was awesome.

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests