DataSource.cpp 4.7 KB

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