config.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * WCDB available.
  4. *
  5. * Copyright (C) 2017 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include <WCDB/config.hpp>
  21. namespace WCDB {
  22. Configs::Configs() : m_configs(new ConfigList)
  23. {
  24. }
  25. Configs::Configs(std::initializer_list<const ConfigWrap> configs)
  26. : m_configs(new ConfigList)
  27. {
  28. for (const auto &config : configs) {
  29. m_configs->push_back(config);
  30. }
  31. }
  32. void Configs::setConfig(const std::string &name,
  33. const Config &config,
  34. Configs::Order order)
  35. {
  36. std::shared_ptr<ConfigList> configs = m_configs;
  37. std::shared_ptr<ConfigList> newConfigs(new ConfigList);
  38. bool inserted = false;
  39. for (const auto &wrap : *configs.get()) {
  40. if (!inserted && order < wrap.order) {
  41. newConfigs->push_back({name, config, order});
  42. inserted = true;
  43. } else if (name != wrap.name) {
  44. newConfigs->push_back(wrap);
  45. }
  46. }
  47. if (!inserted) {
  48. newConfigs->push_back({name, config, order});
  49. }
  50. m_configs = newConfigs;
  51. }
  52. void Configs::setConfig(const std::string &name, const Config &config)
  53. {
  54. std::shared_ptr<ConfigList> configs = m_configs;
  55. std::shared_ptr<ConfigList> newConfigs(new ConfigList);
  56. bool inserted = false;
  57. for (const auto &wrap : *configs.get()) {
  58. if (name != wrap.name) {
  59. newConfigs->push_back(wrap);
  60. } else {
  61. newConfigs->push_back({name, config, wrap.order});
  62. inserted = true;
  63. }
  64. }
  65. if (!inserted) {
  66. newConfigs->push_back({name, config, INT_MAX});
  67. }
  68. m_configs = newConfigs;
  69. }
  70. bool Configs::invoke(std::shared_ptr<Handle> &handle, Error &error)
  71. {
  72. std::shared_ptr<ConfigList> configs = m_configs;
  73. for (const auto &config : *configs.get()) {
  74. if (config.invoke && !config.invoke(handle, error)) {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. Config Configs::getConfigByName(const std::string &name) const
  81. {
  82. std::shared_ptr<ConfigList> configs = m_configs;
  83. for (const auto &config : *configs.get()) {
  84. if (config.name == name) {
  85. return config.invoke;
  86. }
  87. }
  88. return nullptr;
  89. }
  90. bool operator==(const Configs &left, const Configs &right)
  91. {
  92. return left.m_configs == right.m_configs;
  93. }
  94. bool operator!=(const Configs &left, const Configs &right)
  95. {
  96. return left.m_configs != right.m_configs;
  97. }
  98. } //namespace WCDB