@@ 62,13 62,13 @@ struct t_wrapper {
t_object x_obj;
float reserved;
Class* cls;
- Instance* inst = nullptr;
+ Instance inst;
};
class Class : public IConfigurator {
std::string name;
- krt_class* k;
- t_class* pd;
+ krt_class* k = nullptr;
+ t_class* pd = nullptr;
float sr = 44100.f;
/* configure from kronos metadata */
void Configure(krt_sym const& symbol) override {
@@ 160,16 160,14 @@ class Class : public IConfigurator {
}
void Dispatch(t_wrapper* w, Param& p, int argc, t_atom* argv) {
- if (!p.ksym->size) return;
-
CONSOLE("[%s] %s", name.c_str(), p.ksym->sym);
auto scratch = (const char* const)alloca(p.ksym->size);
auto writePtr = scratch;
PdToBlob(p.ksym->type_descriptor, writePtr, argc, argv);
- *k->var(w->inst->kstate.get(), p.slot) = (void*)scratch;
+ *k->var(w->inst.kstate.get(), p.slot) = (void*)scratch;
if (p.ksym->process) {
- p.ksym->process(w->inst->kstate.get(), outScratch.get(), 1);
+ p.ksym->process(w->inst.kstate.get(), outScratch.get(), 1);
}
}
@@ 216,11 214,16 @@ class Class : public IConfigurator {
}
static void PdResetProc(t_wrapper* w, t_symbol*, int, t_atom*) {
- w->cls->K()->construct(w->inst->kstate.get(), nullptr);
+ w->cls->K()->construct(w->inst.kstate.get(), nullptr);
}
public:
- Class(std::string name, krt_class* k, t_class *pd):k(k),name(name),pd(pd) {
+ Class() {}
+
+ void Init(std::string name, krt_class* k, t_class *pd) {
+ this->k = k;
+ this->pd = pd;
+ this->name = name;
post("Loading [%s]", name.c_str());
KReflect::Configure(*k, *this);
KReflect::Setup(*k, *this);
@@ 255,13 258,13 @@ public:
void AddConnections(t_wrapper* wrap) {
for (int i = 1; i < signalSlots.size(); ++i) {
- wrap->inst->inlets.emplace_front(
+ wrap->inst.inlets.emplace_front(
signalinlet_new(&wrap->x_obj, wrap->reserved),
inlet_free);
}
for (auto& p : parameters) {
- wrap->inst->inlets.emplace_front(
+ wrap->inst.inlets.emplace_front(
inlet_new(&wrap->x_obj, &wrap->x_obj.ob_pd,
p.dim > 1 ? &s_list : &s_float,
p.pdsym),
@@ 269,7 272,7 @@ public:
}
for (int i = 0; i < numOutChannels; ++i) {
- wrap->inst->outlets.emplace_front(
+ wrap->inst.outlets.emplace_front(
outlet_new(&wrap->x_obj, &s_signal),
outlet_free);
}
@@ 277,10 280,10 @@ public:
void* Construct(t_symbol* s, int argc, t_atom* argv) {
CONSOLE("Constructing %s (%i args)", name.c_str(), argc);
- t_wrapper* wrap = (t_wrapper*)pd_new(Pd());
+ t_wrapper* wrap = (t_wrapper*)pd_new(Pd());
wrap->cls = this;
- wrap->inst = new Instance;
- wrap->inst->kstate = ConstructInstance();
+ new (&wrap->inst) Instance;
+ wrap->inst.kstate = ConstructInstance();
AddConnections(wrap);
// constructor args
@@ 296,21 299,21 @@ public:
}
void Perform(t_wrapper* wrap, t_signal** sig) {
- auto ki = wrap->inst->kstate.get();
+ auto ki = wrap->inst.kstate.get();
for (int i = 0; i < signalSlots.size(); ++i) {
float* ptr = sig[i]->s_vec;
*k->var(ki, signalSlots[i].kslot) = ptr;
}
if (audioClock) {
- audioClock(ki, wrap->inst->outBuffer.data(), sig[0]->s_n);
+ audioClock(ki, wrap->inst.outBuffer.data(), sig[0]->s_n);
float** outChannels = (float**)alloca(sizeof(float*) * numOutChannels);
for (int i = 0; i < numOutChannels; ++i) {
outChannels[i] = sig[signalSlots.size() + i]->s_vec;
}
- Deinterleave(outChannels, wrap->inst->outBuffer.data(), sig[0]->s_n, numOutChannels);
+ Deinterleave(outChannels, wrap->inst.outBuffer.data(), sig[0]->s_n, numOutChannels);
}
}
@@ 326,7 329,7 @@ public:
std::vector<t_int> signals(1);
signals[0] = (t_int)wrap;
- wrap->inst->outBuffer.resize(sys_getblksize() * numOutChannels);
+ wrap->inst.outBuffer.resize(sys_getblksize() * numOutChannels);
for (auto& s : signalSlots) {
signals.emplace_back((t_int)*sp++);
@@ 350,7 353,7 @@ public:
};
void Destructor(t_wrapper* wrap) {
- delete wrap->inst;
+ wrap->inst.~Instance();
}
t_class* Declare(std::string name, krt_class* k, t_newmethod constructor) {
@@ 372,9 375,10 @@ static void CLASS ## _Declare() {\
auto tnew = (t_newmethod) CLASS ## _Constructor;\
auto name = Sanitize(ToLower(#CLASS)); \
if (HasAudioClock(*k)) name.push_back('~'); \
+ CLASS ## _Factory = std::make_unique<Class>();\
auto pd = Declare(name, k, tnew);\
- CLASS ## _Factory.reset(new Class(name,k,pd));\
-}
+ CLASS ## _Factory->Init(name, k, pd);\
+};
DSP_CLASSES
#undef F