How to store an Embind 'val' in a C++ class - error: call to deleted constructor

How to store an Embind 'val' in a C++ class - error: call to deleted constructor

我设法使用 Embind val transliteration 创建了一个网络音频 API AudioContext 并在一个函数中使用它。但我不明白如何将它存储起来供以后在 C++ class 实例字段 (mContext) 中使用。当我编译以下代码时,我得到

SC_WebAudio.cpp:62:20: error: call to deleted constructor of 'emscripten::val'
SC_WebAudioDriver::SC_WebAudioDriver(struct World* inWorld): SC_AudioDriver(inWorld) {}
                   ^
#include <emscripten.h>
#include <emscripten/val.h>

using namespace emscripten;

class SC_WebAudioDriver : public SC_AudioDriver {
    val mContext;   // this doesn't work

protected:
    virtual bool DriverSetup(int* outNumSamplesPerCallback, double* outSampleRate);
    virtual bool DriverStart();
    virtual bool DriverStop();

public:
    SC_WebAudioDriver(struct World* inWorld);
    virtual ~SC_WebAudioDriver();
};

SC_AudioDriver* SC_NewAudioDriver(struct World* inWorld) { return new SC_WebAudioDriver(inWorld); }

SC_WebAudioDriver::SC_WebAudioDriver(struct World* inWorld): SC_AudioDriver(inWorld) {}

SC_WebAudioDriver::~SC_WebAudioDriver() {
}

bool SC_WebAudioDriver::DriverSetup(int* outNumSamples, double* outSampleRate) {
    scprintf("SC_WebAudio: DriverSetup.\n");

    // this uses the experimental emscripten Embind 'val' transliteration,
    // see https://emscripten.org/docs/api_reference/val.h.html
    val AudioContext = val::global("AudioContext");
    if (!AudioContext.as<bool>()) {
        AudioContext = val::global("webkitAudioContext");
        if (!AudioContext.as<bool>()) {
            scprintf("SC_WebAudioDriver: could not get hold of AudioContext\n");
            return false;
        }
    }

    val context = AudioContext.new_();
    mContext    = context;   // how to do this?
    double sr   = context["sampleRate"].as<double>();
    val proc    = context.call<val>("createScriptProcessor", 0, 0, 2);
    int bufSize = proc["bufferSize"].as<int>();

    *outNumSamples = bufSize;
    *outSampleRate = sr;
    return true;
}

valhas no default constructor。从实现来看,您可以使用 null 或 undefined 作为初始化列表的一部分对其进行初始化:

class SC_WebAudioDriver : public SC_AudioDriver : mContext(val::undefined()) {...}

或者,您可以在 class 定义中设置默认值:

class SC_WebAudioDriver : public SC_AudioDriver {
    val mContext = val::undefined();