DataSource.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "../../Include/RmlUi/Controls/DataSource.h"
  29. #include "../../Include/RmlUi/Controls/DataSourceListener.h"
  30. #include "../../Include/RmlUi/Core/StringUtilities.h"
  31. #include "../../Include/RmlUi/Core/Log.h"
  32. #include <algorithm>
  33. namespace Rml {
  34. namespace Controls {
  35. const Rml::Core::String DataSource::CHILD_SOURCE("#child_data_source");
  36. const Rml::Core::String DataSource::DEPTH("#depth");
  37. const Rml::Core::String DataSource::NUM_CHILDREN("#num_children");
  38. typedef std::map< Rml::Core::String, DataSource* > DataSourceMap;
  39. static DataSourceMap data_sources;
  40. DataSource::DataSource(const Rml::Core::String& _name)
  41. {
  42. if (!_name.empty())
  43. {
  44. name = _name;
  45. }
  46. else
  47. {
  48. name = Core::CreateString(64, "%x", this);
  49. }
  50. data_sources[name] = this;
  51. }
  52. DataSource::~DataSource()
  53. {
  54. ListenerList listeners_copy = listeners;
  55. for (ListenerList::iterator i = listeners_copy.begin(); i != listeners_copy.end(); ++i)
  56. {
  57. (*i)->OnDataSourceDestroy(this);
  58. }
  59. DataSourceMap::iterator iterator = data_sources.find(name);
  60. if (iterator != data_sources.end() &&
  61. iterator->second == this)
  62. {
  63. data_sources.erase(name);
  64. }
  65. }
  66. const Rml::Core::String& DataSource::GetDataSourceName()
  67. {
  68. return name;
  69. }
  70. DataSource* DataSource::GetDataSource(const Rml::Core::String& data_source_name)
  71. {
  72. DataSourceMap::iterator i = data_sources.find(data_source_name);
  73. if (i == data_sources.end())
  74. {
  75. return nullptr;
  76. }
  77. return (*i).second;
  78. }
  79. void DataSource::AttachListener(DataSourceListener* listener)
  80. {
  81. if (find(listeners.begin(), listeners.end(), listener) != listeners.end())
  82. {
  83. RMLUI_ERROR;
  84. return;
  85. }
  86. listeners.push_back(listener);
  87. }
  88. void DataSource::DetachListener(DataSourceListener* listener)
  89. {
  90. ListenerList::iterator i = find(listeners.begin(), listeners.end(), listener);
  91. RMLUI_ASSERT(i != listeners.end());
  92. if (i != listeners.end())
  93. {
  94. listeners.erase(i);
  95. }
  96. }
  97. void* DataSource::GetScriptObject() const
  98. {
  99. return nullptr;
  100. }
  101. void DataSource::NotifyRowAdd(const Rml::Core::String& table, int first_row_added, int num_rows_added)
  102. {
  103. ListenerList listeners_copy = listeners;
  104. for (ListenerList::iterator i = listeners_copy.begin(); i != listeners_copy.end(); ++i)
  105. {
  106. (*i)->OnRowAdd(this, table, first_row_added, num_rows_added);
  107. }
  108. }
  109. void DataSource::NotifyRowRemove(const Rml::Core::String& table, int first_row_removed, int num_rows_removed)
  110. {
  111. ListenerList listeners_copy = listeners;
  112. for (ListenerList::iterator i = listeners_copy.begin(); i != listeners_copy.end(); ++i)
  113. {
  114. (*i)->OnRowRemove(this, table, first_row_removed, num_rows_removed);
  115. }
  116. }
  117. void DataSource::NotifyRowChange(const Rml::Core::String& table, int first_row_changed, int num_rows_changed)
  118. {
  119. ListenerList listeners_copy = listeners;
  120. for (ListenerList::iterator i = listeners_copy.begin(); i != listeners_copy.end(); ++i)
  121. {
  122. (*i)->OnRowChange(this, table, first_row_changed, num_rows_changed);
  123. }
  124. }
  125. void DataSource::NotifyRowChange(const Rml::Core::String& table)
  126. {
  127. ListenerList listeners_copy = listeners;
  128. for (ListenerList::iterator i = listeners_copy.begin(); i != listeners_copy.end(); ++i)
  129. {
  130. (*i)->OnRowChange(this, table);
  131. }
  132. }
  133. void DataSource::BuildRowEntries(Rml::Core::StringList& row, const RowMap& row_map, const Rml::Core::StringList& columns)
  134. {
  135. // Reserve the number of entries.
  136. row.resize(columns.size());
  137. for (size_t i = 0; i < columns.size(); i++)
  138. {
  139. // Look through our row_map for each entry and add it to the result set
  140. RowMap::const_iterator itr = row_map.find(columns[i]);
  141. if (itr != row_map.end())
  142. {
  143. row[i] = (*itr).second;
  144. }
  145. else
  146. {
  147. row[i] = "";
  148. Rml::Core::Log::Message(Rml::Core::Log::LT_ERROR, "Failed to find required data source column %s", columns[i].c_str());
  149. }
  150. }
  151. }
  152. }
  153. }