DataModel.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "../../Include/RmlUi/Core/DataModel.h"
  30. namespace Rml {
  31. namespace Core {
  32. bool DataModel::GetValue(const String& name, Variant& out_value) const
  33. {
  34. bool success = true;
  35. auto pos = name.find('[');
  36. if (pos != String::npos)
  37. {
  38. auto pos_end = name.find(']', pos + 2);
  39. if(pos_end != String::npos)
  40. {
  41. const String container_name = name.substr(0, pos);
  42. const int index = FromString(name.substr(pos + 1, pos_end - pos + 1), -1);
  43. if (index >= 0)
  44. {
  45. auto it_container = containers.find(container_name);
  46. if (it_container != containers.end())
  47. {
  48. DataBindingContext context({ DataBindingContext::Item{ "", index} });
  49. if (it_container->second->Get(out_value, context))
  50. return true;
  51. }
  52. }
  53. }
  54. return false;
  55. }
  56. auto it = bindings.find(name);
  57. if (it != bindings.end())
  58. {
  59. DataBinding& binding = *it->second;
  60. success = binding.Get(out_value);
  61. if (!success)
  62. Log::Message(Log::LT_WARNING, "Could not get value from '%s' in data model.", name.c_str());
  63. }
  64. else
  65. {
  66. Log::Message(Log::LT_WARNING, "Could not find value named '%s' in data model.", name.c_str());
  67. success = false;
  68. }
  69. return success;
  70. }
  71. bool DataModel::SetValue(const String& name, const Variant& value) const
  72. {
  73. bool success = true;
  74. auto it = bindings.find(name);
  75. if (it != bindings.end())
  76. {
  77. DataBinding& binding = *it->second;
  78. success = binding.Set(value);
  79. if (!success)
  80. Log::Message(Log::LT_WARNING, "Could not set value to '%s' in data model.", name.c_str());
  81. }
  82. else
  83. {
  84. Log::Message(Log::LT_WARNING, "Could not find value named '%s' in data model.", name.c_str());
  85. success = false;
  86. }
  87. return success;
  88. }
  89. String DataModel::ResolveVariableName(const String& raw_name, Element* parent) const
  90. {
  91. auto it = bindings.find(raw_name);
  92. if (it != bindings.end())
  93. return raw_name;
  94. Element* ancestor = parent;
  95. while (ancestor && ancestor->GetDataModel() == this)
  96. {
  97. auto it_element = aliases.find(ancestor);
  98. if (it_element != aliases.end())
  99. {
  100. auto& alias_names = it_element->second;
  101. auto it_alias_name = alias_names.find(raw_name);
  102. if (it_alias_name != alias_names.end())
  103. {
  104. return it_alias_name->second;
  105. }
  106. }
  107. ancestor = ancestor->GetParentNode();
  108. }
  109. Log::Message(Log::LT_WARNING, "Could not find variable name '%s' in data model.", raw_name.c_str());
  110. return String();
  111. }
  112. }
  113. }