ExternalEvent.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * @file common/ExternalEvent.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef COMMON_EXTERNAL_EVENT
  26. #define COMMON_EXTERNAL_EVENT
  27. #include <artis-star/common/Model.hpp>
  28. #include <artis-star/common/Node.hpp>
  29. #include <artis-star/common/Value.hpp>
  30. #include <boost/serialization/serialization.hpp>
  31. #include <boost/serialization/nvp.hpp>
  32. #include <sstream>
  33. #include <string>
  34. #include <vector>
  35. namespace artis {
  36. namespace common {
  37. template<class Time>
  38. class Node;
  39. template<class Time>
  40. class Model;
  41. template<class Time>
  42. class ExternalEvent
  43. {
  44. public:
  45. ExternalEvent(const Value &data)
  46. :
  47. _port_index(-1), _model(nullptr), _data(data)
  48. {}
  49. ExternalEvent(int port_index)
  50. :
  51. _port_index(port_index), _model(nullptr), _data(Value())
  52. {}
  53. ExternalEvent(int port_index, const Value &data)
  54. :
  55. _port_index(port_index), _model(nullptr), _data(data)
  56. {}
  57. ExternalEvent(const Node<Time> &node, const Value &data)
  58. :
  59. _port_index(node.get_port_index()),
  60. _model(node.get_model()),
  61. _data(data)
  62. {}
  63. ExternalEvent(const ExternalEvent &event)
  64. :
  65. _port_index(event._port_index), _model(event._model),
  66. _data(event._data)
  67. {}
  68. ExternalEvent()
  69. : _port_index(-2), _model(nullptr)
  70. {}
  71. virtual ~ExternalEvent() = default;
  72. const Value &data() const
  73. { return _data; }
  74. void data(const Value &data)
  75. { _data = data; }
  76. Model<Time> *get_model() const
  77. { return _model; }
  78. bool is_void() const
  79. { return _port_index == -2; }
  80. bool on_port(int port_index) const
  81. {
  82. assert(_port_index != -1);
  83. return _port_index == port_index;
  84. }
  85. ExternalEvent &operator=(const ExternalEvent &e)
  86. {
  87. _port_index = e._port_index;
  88. _model = e._model;
  89. _data = e._data;
  90. return *this;
  91. }
  92. int port_index() const
  93. {
  94. assert(_port_index != -1);
  95. return _port_index;
  96. }
  97. void set_model(Model<Time> *model)
  98. { _model = model; }
  99. std::string to_string() const
  100. {
  101. std::ostringstream ss;
  102. ss << "( ";
  103. if (_port_index != -1) {
  104. ss << _port_index << " , ";
  105. }
  106. ss << (_model ? _model->get_name() : "<>")
  107. << " , ";
  108. if (not _data.empty()) {
  109. ss << _data.to_string();
  110. } else {
  111. ss << "null";
  112. }
  113. ss << " )";
  114. return ss.str();
  115. }
  116. static ExternalEvent Void;
  117. private:
  118. friend class boost::serialization::access;
  119. template<class Archive>
  120. void serialize(Archive &ar, const unsigned int version)
  121. {
  122. (void) version;
  123. ar & _port_index;
  124. _model = 0;
  125. // ar & _model;
  126. ar & _data;
  127. // ar & _model->get_name();
  128. }
  129. int _port_index;
  130. Model<Time> *_model;
  131. Value _data;
  132. };
  133. template<class Time>
  134. ExternalEvent<Time> ExternalEvent<Time>::Void;
  135. }
  136. } // namespace artis common
  137. #endif