snap7_buf.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "snap7_buf.h"
  2. #include <string>
  3. #include <string.h>
  4. Snap7_buf::Snap7_buf()
  5. {
  6. m_id = 0;
  7. m_start_index = 0;
  8. m_size = 0;
  9. mp_buf_obverse = nullptr;
  10. mp_buf_reverse = nullptr;
  11. m_communication_mode = NO_COMMUNICATION;
  12. }
  13. Snap7_buf::Snap7_buf(const Snap7_buf& other)
  14. {
  15. m_id = other.m_id;
  16. m_start_index = other.m_start_index;
  17. m_communication_mode = other.m_communication_mode;
  18. if ( other.m_size > 0 && other.mp_buf_obverse != nullptr && other.mp_buf_reverse != nullptr)
  19. {
  20. mp_buf_obverse = (void*)malloc(other.m_size);
  21. memcpy(mp_buf_obverse, other.mp_buf_obverse, other.m_size);
  22. mp_buf_reverse = (void*)malloc(other.m_size);
  23. memcpy(mp_buf_reverse, other.mp_buf_reverse, other.m_size);
  24. m_size = other.m_size;
  25. }
  26. }
  27. Snap7_buf& Snap7_buf::operator =(const Snap7_buf& other)
  28. {
  29. m_id = other.m_id;
  30. m_start_index = other.m_start_index;
  31. m_communication_mode = other.m_communication_mode;
  32. if ( other.m_size > 0 && other.mp_buf_obverse != nullptr && other.mp_buf_reverse != nullptr)
  33. {
  34. mp_buf_obverse = (void*)malloc(other.m_size);
  35. memcpy(mp_buf_obverse, other.mp_buf_obverse, other.m_size);
  36. mp_buf_reverse = (void*)malloc(other.m_size);
  37. memcpy(mp_buf_reverse, other.mp_buf_reverse, other.m_size);
  38. m_size = other.m_size;
  39. }
  40. }
  41. Snap7_buf::~Snap7_buf()
  42. {
  43. if ( mp_buf_obverse )
  44. {
  45. free(mp_buf_obverse);
  46. mp_buf_obverse = NULL;
  47. }
  48. if ( mp_buf_reverse )
  49. {
  50. free(mp_buf_reverse);
  51. mp_buf_reverse = NULL;
  52. }
  53. }
  54. Snap7_buf::Snap7_buf(int id, int start_index, int size, Communication_mode communication_mode)
  55. {
  56. m_id = id;
  57. m_start_index = start_index;
  58. m_communication_mode = communication_mode;
  59. if ( size > 0)
  60. {
  61. mp_buf_obverse = (void*)malloc(size);
  62. mp_buf_reverse = (void*)malloc(size);
  63. m_size = size;
  64. }
  65. }
  66. Snap7_buf::Snap7_buf(int id, int start_index, int size, void* p_buf_obverse, void* p_buf_reverse, Communication_mode communication_mode)
  67. {
  68. m_id = id;
  69. m_start_index = start_index;
  70. m_communication_mode = communication_mode;
  71. if ( size > 0 && p_buf_obverse != nullptr && p_buf_reverse != nullptr)
  72. {
  73. mp_buf_obverse = (void*)malloc(size);
  74. memcpy(mp_buf_obverse, p_buf_obverse, size);
  75. mp_buf_reverse = (void*)malloc(size);
  76. memcpy(mp_buf_reverse, p_buf_reverse, size);
  77. m_size = size;
  78. }
  79. }
  80. int Snap7_buf::get_id()
  81. {
  82. return m_id;
  83. }
  84. int Snap7_buf::get_start_index()
  85. {
  86. return m_start_index;
  87. }
  88. int Snap7_buf::get_size()
  89. {
  90. return m_size;
  91. }
  92. void* Snap7_buf::get_buf_obverse()
  93. {
  94. return mp_buf_obverse;
  95. }
  96. void* Snap7_buf::get_buf_reverse()
  97. {
  98. return mp_buf_reverse;
  99. }
  100. Snap7_buf::Communication_mode Snap7_buf::get_communication_mode()
  101. {
  102. return m_communication_mode;
  103. }
  104. void Snap7_buf::set_communication_mode(Communication_mode communication_mode)
  105. {
  106. m_communication_mode = communication_mode;
  107. }