Browse Source

Use randomly-generated 53-bit IDs for Module and Cable.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
50747839d4
3 changed files with 15 additions and 37 deletions
  1. +5
    -1
      include/engine/Cable.hpp
  2. +2
    -1
      include/engine/Module.hpp
  3. +8
    -35
      src/engine/Engine.cpp

+ 5
- 1
include/engine/Cable.hpp View File

@@ -8,7 +8,11 @@ namespace engine {


struct Cable {
int id = -1;
/** Unique ID for referring to the cable in the engine.
Between 0 and 2^53 since this is serialized with JSON.
Assigned when added to the engine.
*/
int64_t id = -1;
Module* inputModule = NULL;
int inputId;
Module* outputModule = NULL;


+ 2
- 1
include/engine/Module.hpp View File

@@ -31,9 +31,10 @@ struct Module {

plugin::Model* model = NULL;
/** Unique ID for referring to the module in the engine.
Between 0 and 2^53 since this is serialized with JSON.
Assigned when added to the engine.
*/
int id = -1;
int64_t id = -1;

/** Arrays of components.
Initialized with config().


+ 8
- 35
src/engine/Engine.cpp View File

@@ -202,10 +202,6 @@ struct Engine::Internal {
int stepFrames = 0;
Module* primaryModule = NULL;

// For generating new IDs for added objects
int nextModuleId = 0;
int nextCableId = 0;

// Parameter smoothing
Module* smoothModule = NULL;
int smoothParamId = 0;
@@ -547,9 +543,6 @@ void Engine::clear() {
removeModule(module);
delete module;
}
// Reset engine state
internal->nextModuleId = 0;
internal->nextCableId = 0;
}


@@ -693,20 +686,10 @@ void Engine::addModule(Module* module) {
// Check that the module is not already added
auto it = std::find(internal->modules.begin(), internal->modules.end(), module);
assert(it == internal->modules.end());
// Set ID
if (module->id < 0) {
// Automatically assign ID
module->id = internal->nextModuleId++;
}
else {
// Manual ID
// Check that the ID is not already taken
auto it = internal->modulesCache.find(module->id);
assert(it == internal->modulesCache.end());
// If ID is higher than engine's next assigned ID, enlarge it.
if (module->id >= internal->nextModuleId) {
internal->nextModuleId = module->id + 1;
}
// Set ID if unset or collides with an existing ID
while (module->id < 0 || internal->modulesCache.find(module->id) != internal->modulesCache.end()) {
// Randomly generate ID
module->id = random::u64() % (1ul << 53);
}
// Add module
internal->modules.push_back(module);
@@ -846,20 +829,10 @@ void Engine::addCable(Cable* cable) {
if (cable2->outputModule == cable->outputModule && cable2->outputId == cable->outputId)
outputWasConnected = true;
}
// Set ID
if (cable->id < 0) {
// Automatically assign ID
cable->id = internal->nextCableId++;
}
else {
// Manual ID
// Check that the ID is not already taken
for (Cable* w : internal->cables) {
assert(cable->id != w->id);
}
if (cable->id >= internal->nextCableId) {
internal->nextCableId = cable->id + 1;
}
// Set ID if unset or collides with an existing ID
while (cable->id < 0 || internal->cablesCache.find(cable->id) != internal->cablesCache.end()) {
// Randomly generate ID
cable->id = random::u64() % (1ul << 53);
}
// Add the cable
internal->cables.push_back(cable);


Loading…
Cancel
Save