diff --git a/src/Amalgam/Amalgam.h b/src/Amalgam/Amalgam.h index 2629d4d7..56671ea9 100644 --- a/src/Amalgam/Amalgam.h +++ b/src/Amalgam/Amalgam.h @@ -54,27 +54,6 @@ extern "C" //sets num_entities to the number of entities and allocates an array of string pointers for the handles loaded AMALGAM_EXPORT char **GetEntities(uint64_t *num_entities); - AMALGAM_EXPORT double GetNumberValue(char *handle, char *label); - AMALGAM_EXPORT void AppendNumberValue(char *handle, char *label, double value); - AMALGAM_EXPORT void SetNumberValue(char *handle, char *label, double value); - - AMALGAM_EXPORT void AppendStringValue(char *handle, char *label, char *value); - AMALGAM_EXPORT void SetStringValue(char *handle, char *label, char *value); - - AMALGAM_EXPORT double *GetNumberListPtr(char *handle, char *label); - AMALGAM_EXPORT size_t GetNumberListLength(char *handle, char *label); - AMALGAM_EXPORT void GetNumberList(char *handle, char *label, double *out_list); - AMALGAM_EXPORT void AppendNumberList(char *handle, char *label, double *list, size_t len); - AMALGAM_EXPORT void SetNumberList(char *handle, char *label, double *list, size_t len); - - // IMPORTANT: GetStringList assumes that the char ** array is unallocated - // If there are allocated char *s inside, they will become inacessable and be a memory leak. - AMALGAM_EXPORT size_t GetStringListLength(char *handle, char *label); - AMALGAM_EXPORT wchar_t **GetStringListPtrWide(char *handle, char *label); - AMALGAM_EXPORT char **GetStringListPtr(char *handle, char *label); - AMALGAM_EXPORT void AppendStringList(char *handle, char *label, char **list, size_t len); - AMALGAM_EXPORT void SetStringList(char *handle, char *label, char **list, size_t len); - AMALGAM_EXPORT void SetJSONToLabel(char *handle, char *label, char *json); AMALGAM_EXPORT wchar_t *GetJSONPtrFromLabelWide(char *handle, char *label); diff --git a/src/Amalgam/AmalgamAPI.cpp b/src/Amalgam/AmalgamAPI.cpp index 7703ac52..d799c755 100644 --- a/src/Amalgam/AmalgamAPI.cpp +++ b/src/Amalgam/AmalgamAPI.cpp @@ -238,210 +238,6 @@ extern "C" return return_entities; } - // ************************************ - // get, set, and append numbers - // ************************************ - - double GetNumberValue(char *handle, char *label) - { - std::string h(handle); - std::string l(label); - - return entint.GetNumber(h, l); - } - - void AppendNumberValue(char *handle, char *label, double value) - { - std::string h(handle); - std::string l(label); - - entint.AppendToLabel(h, l, value); - } - - void SetNumberValue(char *handle, char *label, double value) - { - std::string h(handle); - std::string l(label); - - entint.SetLabel(h, l, value); - } - - void AppendStringValue(char *handle, char *label, char *value) - { - std::string h(handle); - std::string l(label); - std::string v(value); - - entint.AppendToLabel(h, l, v); - } - - void SetStringValue(char *handle, char *label, char *value) - { - std::string h(handle); - std::string l(label); - std::string v(value); - - entint.SetLabel(h, l, v); - } - - void AppendNumberList(char *handle, char *label, double *list, size_t len) - { - std::string h(handle); - std::string l(label); - - for(size_t i = 0; i < len; i++) - { - entint.AppendToLabel(h, l, list[i]); - } - } - - void SetNumberList(char *handle, char *label, double *list, size_t len) - { - std::string h(handle); - std::string l(label); - - entint.SetNumberList(h, l, list, len); - } - - void SetNumberMatrix(char *handle, char *label, double *list, size_t rows, size_t columns) - { - std::string h(handle); - std::string l(label); - - entint.SetNumberMatrix(h, l, list, rows, columns); - } - - size_t GetNumberListLength(char *handle, char *label) - { - std::string h(handle); - std::string l(label); - - return entint.GetNumberListLength(h, l); - } - - size_t GetNumberMatrixWidth(char *handle, char *label) - { - std::string h(handle); - std::string l(label); - - return entint.GetNumberMatrixWidth(h, l); - } - - size_t GetNumberMatrixHeight(char *handle, char *label) - { - std::string h(handle); - std::string l(label); - - return entint.GetNumberMatrixHeight(h, l); - } - - double *GetNumberListPtr(char *handle, char *label) - { - std::string h(handle); - std::string l(label); - - size_t len = GetNumberListLength(handle, label); - - double *ret = new double[len]; - - entint.GetNumberList(h, l, ret, len); - return ret; - } - - double *GetNumberMatrixPtr(char *handle, char *label) - { - std::string h(handle); - std::string l(label); - - size_t width = GetNumberMatrixWidth(handle, label); - size_t height = GetNumberMatrixHeight(handle, label); - - double *ret = new double[width*height]; - - entint.GetNumberMatrix(h, l, ret, width, height); - return ret; - } - - void GetNumberList(char *handle, char *label, double *out_list) - { - std::string h(handle); - std::string l(label); - - size_t len = GetNumberListLength(handle, label); - - entint.GetNumberList(h, l, out_list, len); - } - - void AppendStringList(char *handle, char *label, char **list, size_t len) - { - std::string h(handle); - std::string l(label); - - for(size_t i = 0; i < len; i++) - { - std::string to_append(list[i]); - entint.AppendToLabel(h, l, to_append); - } - } - - void SetStringList(char *handle, char *label, char **list, size_t len) - { - std::string h(handle); - std::string l(label); - - entint.SetStringList(h, l, list, len); - } - - size_t GetStringListLength(char *handle, char *label) - { - std::string h(handle); - std::string l(label); - - return entint.GetStringListLength(h, l); - } - - // IMPORTANT: GetStringList assumes that the char ** array is unallocated - // If there are allocated char *s inside, they will become inacessable and be a memory leak. - wchar_t **GetStringListPtrWide(char *handle, char *label) - { - std::string h(handle); - std::string l(label); - - size_t len = GetStringListLength(handle, label); - - std::string *str_list = new std::string[len]; - - entint.GetStringList(h, l, str_list, len); - - wchar_t **wct = new wchar_t *[len]; - for(size_t i = 0; i < len; i++) - { - wct[i] = StringToWCharPtr(str_list[i]); - } - - return wct; - } - - // IMPORTANT: GetStringList assumes that the char ** array is unallocated - // If there are allocated char *s inside, they will become inacessable and be a memory leak. - char **GetStringListPtr(char *handle, char *label) - { - std::string h(handle); - std::string l(label); - - size_t len = GetStringListLength(handle, label); - - std::string *str_list = new std::string[len]; - - entint.GetStringList(h, l, str_list, len); - - char **ct = new char *[len]; - for(size_t i = 0; i < len; i++) - ct[i] = StringToCharPtr(str_list[i]); - - return ct; - } - void DeleteString(char *p) { delete[] p; diff --git a/src/Amalgam/entity/EntityExternalInterface.cpp b/src/Amalgam/entity/EntityExternalInterface.cpp index 12630abb..c233c080 100644 --- a/src/Amalgam/entity/EntityExternalInterface.cpp +++ b/src/Amalgam/entity/EntityExternalInterface.cpp @@ -169,279 +169,6 @@ std::vector EntityExternalInterface::GetEntities() return entities; } -void EntityExternalInterface::AppendToLabel(std::string &handle, std::string &label, double value) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return; - - //get the label - EvaluableNodeReference label_val = bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false); - - if(EvaluableNode::IsOrderedArray(label_val)) - { - //modify local copy - label_val->AppendOrderedChildNode(bundle->entity->evaluableNodeManager.AllocNode(value)); - - //overwrite the label with the modified copy - bundle->SetEntityValueAtLabel(label, label_val); - } - else - { - // wrap the existing and new element in a list - EvaluableNode list(ENT_LIST); - EvaluableNode initial_value(EvaluableNode::ToNumber(label_val)); - EvaluableNode parsed_input(value); - list.AppendOrderedChildNode(&initial_value); - list.AppendOrderedChildNode(&parsed_input); - - // overwrite the label with the list - EvaluableNodeReference list_reference(&list, false); - bundle->SetEntityValueAtLabel(label, list_reference); - } -} - -void EntityExternalInterface::AppendToLabel(std::string &handle, std::string &label, std::string &value) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return; - - EvaluableNodeReference label_val(bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false), false); - - if(EvaluableNode::IsOrderedArray(label_val)) - { - //modify local copy - label_val->AppendOrderedChildNode(bundle->entity->evaluableNodeManager.AllocNode(ENT_STRING, value)); - - //overwrite the label with the modified copy - bundle->SetEntityValueAtLabel(label, label_val); - } - else //need to transform it into a list - { - //wrap the existing and new element in a list - //can use local stack instead of heap because the entity will copy anyway - EvaluableNode list(ENT_LIST); - EvaluableNode initial_value(ENT_STRING, EvaluableNode::ToStringPreservingOpcodeType(label_val)); - EvaluableNode parsed_input(ENT_STRING, value); - list.AppendOrderedChildNode(&initial_value); - list.AppendOrderedChildNode(&parsed_input); - - // overwrite the label with the list - EvaluableNodeReference list_reference(&list, false); - bundle->SetEntityValueAtLabel(label, list_reference); - } -} - -void EntityExternalInterface::SetLabel(std::string &handle, std::string &label, double value) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return; - - EvaluableNode parsed_input(value); - EvaluableNodeReference parsed_input_reference(&parsed_input, false); - bundle->SetEntityValueAtLabel(label, parsed_input_reference); -} - -void EntityExternalInterface::SetLabel(std::string &handle, std::string &label, std::string &value) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return; - - EvaluableNode parsed_input(ENT_STRING, value); - EvaluableNodeReference parsed_input_reference(&parsed_input, false); - bundle->SetEntityValueAtLabel(label, parsed_input_reference); -} - -double EntityExternalInterface::GetNumber(std::string &handle, std::string &label) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return std::numeric_limits::quiet_NaN(); - - EvaluableNode *label_val = bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false); - - //Ensure you grab the return value before releasing resources - double ret = EvaluableNode::ToNumber(label_val); - return ret; -} - -std::string EntityExternalInterface::GetString(std::string &handle, std::string &label) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return ""; - - EvaluableNode *label_val = bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false); - - //Ensure you grab the return value before releasing resources - std::string ret = EvaluableNode::ToStringPreservingOpcodeType(label_val); - return ret; -} - -std::string EntityExternalInterface::GetStringFromList(std::string &handle, std::string &label, size_t index) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return ""; - - EvaluableNode *label_val = bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false); - - std::string ret = ""; - - if(EvaluableNode::IsOrderedArray(label_val)) - { - auto &children = label_val->GetOrderedChildNodes(); - if(index < children.size()) - ret = EvaluableNode::ToStringPreservingOpcodeType(children[index]); - } - else - { - ret = EvaluableNode::ToStringPreservingOpcodeType(label_val); - } - - return ret; -} - -// ************************************ -// get, set, and append lists -// ************************************ - -size_t EntityExternalInterface::GetNumberListLength(std::string &handle, std::string &label) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return 0; - - EvaluableNode *label_val = bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false); - - if(label_val == nullptr) - return 0; - - size_t ret = 1; - - if(EvaluableNode::IsOrderedArray(label_val)) - { - auto &children = label_val->GetOrderedChildNodes(); - ret = children.size(); - } - - return ret; -} - -void EntityExternalInterface::GetNumberList(std::string &handle, std::string &label, double *out_arr, size_t len) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return; - - EvaluableNode *label_val = bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false); - - if(label_val == nullptr) - return; - - if(EvaluableNode::IsOrderedArray(label_val)) - { - auto &children = label_val->GetOrderedChildNodes(); - size_t min = std::min(children.size(), len); - for(size_t i = 0; i < min; i++) - out_arr[i] = EvaluableNode::ToNumber(children[i]); - } - else - { - out_arr[0] = EvaluableNode::ToNumber(label_val); - } -} - -void EntityExternalInterface::GetNumberList(EvaluableNode *label_val, double *out_arr, size_t len) -{ - if(label_val == nullptr) - return; - - if(EvaluableNode::IsOrderedArray(label_val)) - { - auto &children = label_val->GetOrderedChildNodes(); - size_t min = std::min(children.size(), len); - for(size_t i = 0; i < min; i++) - out_arr[i] = EvaluableNode::ToNumber(children[i]); - } - else - { - out_arr[0] = EvaluableNode::ToNumber(label_val); - } -} - -size_t EntityExternalInterface::GetNumberMatrixWidth(std::string &handle, std::string &label) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return 0; - - EvaluableNode *label_val = bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false); - - if(label_val == nullptr) - return 0; - - std::size_t ret = 1; - - if(EvaluableNode::IsOrderedArray(label_val)) - { - auto &children = label_val->GetOrderedChildNodes(); - ret = children.size(); - } - - return ret; -} - -size_t EntityExternalInterface::GetNumberMatrixHeight(std::string &handle, std::string &label) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return 0; - - EvaluableNode *label_val = bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false); - - if(label_val == nullptr) - return 0; - - std::size_t ret = 1; - - if(EvaluableNode::IsOrderedArray(label_val)) - { - auto &children = label_val->GetOrderedChildNodes()[0]->GetOrderedChildNodes(); - ret = children.size(); - } - - return ret; -} - -void EntityExternalInterface::GetNumberMatrix(std::string &handle, std::string &label, double *out_arr, size_t w, size_t h) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return; - - EvaluableNode *label_val = bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false); - - if(label_val == nullptr) - return; - - if(EvaluableNode::IsOrderedArray(label_val)) - { - auto &children = label_val->GetOrderedChildNodes(); - double *column = new double[h]; - for(size_t x = 0; x < w; x++) - { - GetNumberList(children[x], column, h); - for(size_t y = 0; y < h; y++) - out_arr[x*h + y] = column[y]; - } - delete [] column; - } -} - EvaluableNode *NodifyNumberList(Entity *entity, double *arr, size_t len) { EvaluableNodeManager *enm = &entity->evaluableNodeManager; @@ -474,84 +201,6 @@ EvaluableNode *NodifyNumberMatrix(Entity *entity, double *arr, size_t w, size_t return matrix_node; } -void EntityExternalInterface::SetNumberList(std::string &handle, std::string &label, double *arr, size_t len) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return; - - EvaluableNode *list_node = NodifyNumberList(bundle->entity, arr, len); - EvaluableNodeReference list_node_reference(list_node, true); - bundle->SetEntityValueAtLabel(label, list_node_reference); -} - -void EntityExternalInterface::SetNumberMatrix(std::string &handle, std::string &label, double *arr, size_t w, size_t h) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return; - - EvaluableNode *list_node = NodifyNumberMatrix(bundle->entity, arr, w, h); - EvaluableNodeReference list_node_reference(list_node, true); - bundle->SetEntityValueAtLabel(label, list_node_reference); -} - -void EntityExternalInterface::AppendNumberList(std::string &handle, std::string &label, double *arr, size_t len) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return; - - EvaluableNode *list_node = NodifyNumberList(bundle->entity, arr, len); - - EvaluableNode *label_val = bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false); - label_val->AppendOrderedChildNode(list_node); -} - -size_t EntityExternalInterface::GetStringListLength(std::string &handle, std::string &label) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return 0; - - EvaluableNode *label_val = bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false); - - if(label_val == nullptr) - return 0; - - if(EvaluableNode::IsOrderedArray(label_val)) - { - auto &children = label_val->GetOrderedChildNodes(); - return children.size(); - } - - return 1; -} - -void EntityExternalInterface::GetStringList(std::string &handle, std::string &label, std::string *out_arr, size_t len) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return; - - EvaluableNode *label_val = bundle->entity->GetValueAtLabel(label, &bundle->entity->evaluableNodeManager, false); - - if(label_val == nullptr) - return; - - if(EvaluableNode::IsOrderedArray(label_val)) - { - auto &children = label_val->GetOrderedChildNodes(); - size_t min = std::min(children.size(), len); - for(size_t i = 0; i < min; i++) - out_arr[i] = EvaluableNode::ToStringPreservingOpcodeType(children[i]); - } - else - { - out_arr[0] = EvaluableNode::ToStringPreservingOpcodeType(label_val); - } -} - EvaluableNode *NodifyStringList(Entity *entity, char **arr, size_t len) { EvaluableNodeManager *enm = &entity->evaluableNodeManager; @@ -564,17 +213,6 @@ EvaluableNode *NodifyStringList(Entity *entity, char **arr, size_t len) return list_node; } -void EntityExternalInterface::SetStringList(std::string &handle, std::string &label, char **arr, size_t len) -{ - auto bundle = FindEntityBundle(handle); - if(bundle == nullptr) - return; - - EvaluableNode *list_node = NodifyStringList(bundle->entity, arr, len); - EvaluableNodeReference list_node_reference(list_node, true); - bundle->SetEntityValueAtLabel(label, list_node_reference); -} - bool EntityExternalInterface::SetJSONToLabel(std::string &handle, std::string &label, std::string_view json) { auto bundle = FindEntityBundle(handle); diff --git a/src/Amalgam/entity/EntityExternalInterface.h b/src/Amalgam/entity/EntityExternalInterface.h index b70dd8b8..f8229d11 100644 --- a/src/Amalgam/entity/EntityExternalInterface.h +++ b/src/Amalgam/entity/EntityExternalInterface.h @@ -52,31 +52,6 @@ class EntityExternalInterface bool SetRandomSeed(std::string &handle, std::string &rand_seed); std::vector GetEntities(); - void AppendToLabel(std::string &handle, std::string &label, double value); - void AppendToLabel(std::string &handle, std::string &label, std::string &value); - - void SetLabel(std::string &handle, std::string &label, double value); - void SetLabel(std::string &handle, std::string &label, std::string &value); - - double GetNumber(std::string &handle, std::string &label); - std::string GetString(std::string &handle, std::string &label); - std::string GetStringFromList(std::string &handle, std::string &label, size_t index); - - size_t GetNumberListLength(std::string &handle, std::string &label); - void GetNumberList(std::string &handle, std::string &label, double *out_arr, size_t len); - void GetNumberList(EvaluableNode *label_val, double *out_arr, size_t len); - void SetNumberList(std::string &handle, std::string &label, double *arr, size_t len); - void AppendNumberList(std::string &handle, std::string &label, double *arr, size_t len); - - size_t GetNumberMatrixWidth(std::string &handle, std::string &label); - size_t GetNumberMatrixHeight(std::string &handle, std::string &label); - void GetNumberMatrix(std::string &handle, std::string &label, double *out_arr, size_t w, size_t h); - void SetNumberMatrix(std::string &handle, std::string &label, double *arr, size_t w, size_t h); - - size_t GetStringListLength(std::string &handle, std::string &label); - void GetStringList(std::string &handle, std::string &label, std::string *out_arr, size_t len); - void SetStringList(std::string &handle, std::string &label, char **arr, size_t len); - bool SetJSONToLabel(std::string &handle, std::string &label, std::string_view json); std::string GetJSONFromLabel(std::string &handle, std::string &label); std::string ExecuteEntityJSON(std::string &handle, std::string &label, std::string_view json);