/tide/master/hardware/ScreenControllerFactory.cpp

https://github.com/BlueBrain/Tide · C++ · 111 lines · 63 code · 10 blank · 38 comment · 13 complexity · 10384277ac13ced7d839ad25fb544bf4 MD5 · raw file

  1. /*********************************************************************/
  2. /* Copyright (c) 2017, EPFL/Blue Brain Project */
  3. /* Pawel Podhajski <pawel.podhajski@epfl.ch> */
  4. /* All rights reserved. */
  5. /* */
  6. /* Redistribution and use in source and binary forms, with or */
  7. /* without modification, are permitted provided that the following */
  8. /* conditions are met: */
  9. /* */
  10. /* 1. Redistributions of source code must retain the above */
  11. /* copyright notice, this list of conditions and the following */
  12. /* disclaimer. */
  13. /* */
  14. /* 2. Redistributions in binary form must reproduce the above */
  15. /* copyright notice, this list of conditions and the following */
  16. /* disclaimer in the documentation and/or other materials */
  17. /* provided with the distribution. */
  18. /* */
  19. /* THIS SOFTWARE IS PROVIDED BY THE ECOLE POLYTECHNIQUE */
  20. /* FEDERALE DE LAUSANNE ''AS IS'' AND ANY EXPRESS OR IMPLIED */
  21. /* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
  22. /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
  23. /* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ECOLE */
  24. /* POLYTECHNIQUE FEDERALE DE LAUSANNE OR CONTRIBUTORS BE */
  25. /* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
  26. /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
  27. /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
  28. /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) */
  29. /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
  30. /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE */
  31. /* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
  32. /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  33. /* */
  34. /* The views and conclusions contained in the software and */
  35. /* documentation are those of the authors and should not be */
  36. /* interpreted as representing official policies, either expressed */
  37. /* or implied, of Ecole polytechnique federale de Lausanne. */
  38. /*********************************************************************/
  39. #include "ScreenControllerFactory.h"
  40. #include "MultiScreenController.h"
  41. #include <QMap>
  42. #include <QStringList>
  43. namespace
  44. {
  45. PlanarController::Type _getType(const QString& name)
  46. {
  47. if (name == "UR9850")
  48. return PlanarController::Type::TV_UR9850;
  49. if (name == "UR9851")
  50. return PlanarController::Type::TV_UR9851;
  51. return PlanarController::Type::Matrix;
  52. }
  53. }
  54. std::unique_ptr<ScreenController> ScreenControllerFactory::create(
  55. const QString& ports)
  56. {
  57. const auto connections = parseInputString(ports);
  58. if (connections.empty())
  59. throw std::runtime_error("Invalid screen controller list: '" +
  60. ports.toStdString() + "'");
  61. std::vector<std::unique_ptr<ScreenController>> controllers;
  62. QMapIterator<QString, PlanarController::Type> i(connections);
  63. while (i.hasNext())
  64. {
  65. i.next();
  66. controllers.emplace_back(new PlanarController(i.key(), i.value()));
  67. }
  68. if (controllers.size() == 1)
  69. return std::move(controllers[0]);
  70. return std::unique_ptr<ScreenController>(
  71. new MultiScreenController(std::move(controllers)));
  72. }
  73. QMap<QString, PlanarController::Type> ScreenControllerFactory::parseInputString(
  74. const QString& ports)
  75. {
  76. QMap<QString, PlanarController::Type> map;
  77. const auto connections = ports.split(';');
  78. if (connections.empty())
  79. return map;
  80. for (const auto& connection : connections)
  81. {
  82. if (connection.isEmpty())
  83. continue;
  84. if (connection.contains("#"))
  85. {
  86. const auto serialEntity = connection.split("#");
  87. const auto& serialPort = serialEntity[0];
  88. if (serialPort.isEmpty())
  89. continue;
  90. const auto type = _getType(serialEntity[1]);
  91. map.insert(serialPort, type);
  92. }
  93. else
  94. {
  95. const auto& serialPort = connection;
  96. const auto type = _getType("");
  97. map.insert(serialPort, type);
  98. }
  99. }
  100. return map;
  101. }