iOS 上 Objective-C 中的灵活数组成员

发布于 2024-10-31 09:09:40 字数 309 浏览 3 评论 0原文

我正在研究一些核心音频代码,并且有一个问题可以通过结构中的变量数组来解决 - 灵活数组成员。通过环顾四周,我发现有很多关于灵活成员数组的可移植性和可行性的对话。

据我了解,Objective-C 符合 C99。因此,我认为灵活的数组成员应该是一个很好的解决方案。我还发现灵活数组成员在 C++ 中不是一个好主意。

在 Objective-C++ 中做什么?从技术上讲,我不会在 Objective-C++ 中使用它。我正在编写基于 C 和 C++ 的回调...这似乎是一个反对点。

无论如何,我可以(应该)这样做吗?如果没有,是否有其他技术具有相同的结果?

I am working on some core audio code and have a problem that could be solved by a variable array in a struct--a la Flexible Array Members. In doing a bit of looking around, I see that there is a lot of dialogue about the portability and viability of Flexible Member Arrays.

From what I understand, Objective-C is C99 compliant. For this reason, I think Flexible Array Members should be a fine solution. I also see that Flexible Array Members are not a good idea in C++.

What to do in Objective-C++? Technically, I won't use it in Objective-C++. I am writing callbacks that are C and C++ based... That seems like a point against.

Anyway, can I (should I) do it? If not, is there another technique with the same results?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2024-11-07 09:09:40

您始终可以声明一个大小为 1 的尾随数组。在最坏的情况下,您会浪费相当少量的内存,并且计算 malloc 的正确大小会稍微复杂一些。

You can always just declare a trailing array of size 1. In the worst case here, you waste a pretty small amount of memory, and it is very slightly more complicated to compute the right size for malloc.

走过海棠暮 2024-11-07 09:09:40

别打扰。它不兼容。它很混乱并且容易出错。早在这个功能存在之前,c++ 就有更容易管理的解决方案。你要在结构的末尾添加什么?通常,您只需使用 std::vector、std::array 或固定大小数组之类的东西。

更新

我想要一个音符开始时间 (uint64_t) 列表,并遍历它们以查看正在播放的音符(如果有)。我打算向结构添加一个计数变量来跟踪灵活数组中有多少项。

好的,如果你有固定的复调音乐,那么固定大小的数组应该没问题。在大多数 iOS 合成器中,你不需要多个这样的数组。当然,“即将发布的笔记”数组大小可能会根据应用程序合成器的不同而有所不同?采样器?定序器?实时输入?

template <size_t NumNotes_>
class t_note_start_times {
public:
    static const size_t NumNotes = NumNotes_;
    typedef uint64_t t_timestamp;

    /*...*/

    const t_timestamp& timestampAt(const size_t& idx) const {
        assert(this->d_numFutureNotes <= NumNotes);
        assert(idx < NumNotes);
        assert(idx < this->d_numFutureNotes);
        return this->d_startTimes[idx];
    }
private:
    t_timestamp d_presentTime;
    size_t d_numFutureNotes; // presumably, this will be the number of active notes,
                             // and values will be compacted to [0...d_numFutureNotes)
    t_timestamp d_startTimes[NumNotes];
};

// in use       
const size_t Polyphony = 16;
t_note_start_times<Polyphony> startTimes;
startTimes.addNoteAtTime(noteTimestamp); // defined in the '...' ;)
startTimes.timestampAt(0);

如果您需要一个可能非常大的动态大小的数组,那么使用向量。如果您只需要一个实例,并且最大复调数是(比如说)64,那么就使用它。

don't bother. it's not compatible. it is messy and error prone. c++ had solutions which are managed more easily long before this feature existed. what are you tacking onto the end of your struct? normally, you'll just use something like a std::vector, std::array, or fixed size array.

UPDATE

I want to have a list of note start times (uint64_t) and iterate through them to see which, if any, is playing. i was going to add a count var to the struct to track how many items are in the flexible array.

ok, then a fixed size array should be fine if you have fixed polyphony. you will not need more than one such array in most iOS synths. of course, 'upcoming note' array sizes could vary based on the app synth? sampler? sequencer? live input?

template <size_t NumNotes_>
class t_note_start_times {
public:
    static const size_t NumNotes = NumNotes_;
    typedef uint64_t t_timestamp;

    /*...*/

    const t_timestamp& timestampAt(const size_t& idx) const {
        assert(this->d_numFutureNotes <= NumNotes);
        assert(idx < NumNotes);
        assert(idx < this->d_numFutureNotes);
        return this->d_startTimes[idx];
    }
private:
    t_timestamp d_presentTime;
    size_t d_numFutureNotes; // presumably, this will be the number of active notes,
                             // and values will be compacted to [0...d_numFutureNotes)
    t_timestamp d_startTimes[NumNotes];
};

// in use       
const size_t Polyphony = 16;
t_note_start_times<Polyphony> startTimes;
startTimes.addNoteAtTime(noteTimestamp); // defined in the '...' ;)
startTimes.timestampAt(0);

if you need a dynamically sized array which could be very large, then use a vector. if you need only one instance of this and the max polyphony is (say) 64, then just use this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文