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 { 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; Module* inputModule = NULL;
int inputId; int inputId;
Module* outputModule = NULL; Module* outputModule = NULL;


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

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


plugin::Model* model = NULL; plugin::Model* model = NULL;
/** Unique ID for referring to the module in the engine. /** 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. Assigned when added to the engine.
*/ */
int id = -1;
int64_t id = -1;


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


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

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


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

// Parameter smoothing // Parameter smoothing
Module* smoothModule = NULL; Module* smoothModule = NULL;
int smoothParamId = 0; int smoothParamId = 0;
@@ -547,9 +543,6 @@ void Engine::clear() {
removeModule(module); removeModule(module);
delete 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 // Check that the module is not already added
auto it = std::find(internal->modules.begin(), internal->modules.end(), module); auto it = std::find(internal->modules.begin(), internal->modules.end(), module);
assert(it == internal->modules.end()); 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 // Add module
internal->modules.push_back(module); internal->modules.push_back(module);
@@ -846,20 +829,10 @@ void Engine::addCable(Cable* cable) {
if (cable2->outputModule == cable->outputModule && cable2->outputId == cable->outputId) if (cable2->outputModule == cable->outputModule && cable2->outputId == cable->outputId)
outputWasConnected = true; 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 // Add the cable
internal->cables.push_back(cable); internal->cables.push_back(cable);


Loading…
Cancel
Save