/* * GDevelop Core * Copyright 2008-2015 Florian Rival (Florian.Rival@gmail.com). All rights reserved. * This project is released under the MIT License. */ #include #include "GDCore/String.h" #include #include "GDCore/Project/Variable.h" #include "GDCore/TinyXml/tinyxml.h" #include "GDCore/Project/VariablesContainer.h" #include "GDCore/Serialization/SerializerElement.h" namespace gd { std::pair VariablesContainer::badVariable; namespace { //Tool functor used below class VariableHasName { public: VariableHasName(gd::String const& name_) : name(name_) { } bool operator () (const std::pair & p) { return (p.first == name); } gd::String name; }; } VariablesContainer::VariablesContainer() { } bool VariablesContainer::Has(const gd::String & name) const { std::vector < std::pair >::const_iterator i = std::find_if(variables.begin(), variables.end(), VariableHasName(name)); return (i != variables.end()); } std::pair & VariablesContainer::Get(std::size_t index) { if ( index < variables.size() ) return variables[index]; return badVariable; } const std::pair & VariablesContainer::Get(std::size_t index) const { if ( index < variables.size() ) return variables[index]; return badVariable; } Variable & VariablesContainer::Get(const gd::String & name) { std::vector < std::pair >::iterator i = std::find_if(variables.begin(), variables.end(), VariableHasName(name)); if (i != variables.end()) return i->second; return badVariable.second; } const Variable & VariablesContainer::Get(const gd::String & name) const { std::vector < std::pair >::const_iterator i = std::find_if(variables.begin(), variables.end(), VariableHasName(name)); if (i != variables.end()) return i->second; return badVariable.second; } Variable & VariablesContainer::Insert(const gd::String & name, const gd::Variable & variable, std::size_t position) { if (position >::iterator i = std::find_if(variables.begin(), variables.end(), VariableHasName(oldName)); if (i != variables.end()) i->first = newName; } void VariablesContainer::Swap(std::size_t firstVariableIndex, std::size_t secondVariableIndex) { if ( firstVariableIndex >= variables.size() || secondVariableIndex >= variables.size() ) return; std::pair temp = variables[firstVariableIndex]; variables[firstVariableIndex] = variables[secondVariableIndex]; variables[secondVariableIndex] = temp; } void VariablesContainer::SerializeTo(SerializerElement & element) const { element.ConsiderAsArrayOf("variable"); for ( std::size_t j = 0;j < variables.size();j++ ) { SerializerElement & variableElement = element.AddChild("variable"); variableElement.SetAttribute("name", variables[j].first); variables[j].second.SerializeTo(variableElement); } } #endif void VariablesContainer::UnserializeFrom(const SerializerElement & element) { Clear(); element.ConsiderAsArrayOf("variable", "Variable"); for ( std::size_t j = 0;j < element.GetChildrenCount();j++ ) { const SerializerElement & variableElement = element.GetChild(j); Variable variable; variable.UnserializeFrom(variableElement); Insert(variableElement.GetStringAttribute("name", "", "Name" ), variable, -1); } } }