snap7_buf.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. memset(mp_buf_obverse, 0, size);
  63. mp_buf_reverse = (void*)malloc(size);
  64. memset(mp_buf_reverse, 0, size);
  65. m_size = size;
  66. }
  67. }
  68. Snap7_buf::Snap7_buf(int id, int start_index, int size, void* p_buf_obverse, void* p_buf_reverse, Communication_mode communication_mode)
  69. {
  70. m_id = id;
  71. m_start_index = start_index;
  72. m_communication_mode = communication_mode;
  73. if ( size > 0 && p_buf_obverse != nullptr && p_buf_reverse != nullptr)
  74. {
  75. mp_buf_obverse = (void*)malloc(size);
  76. memcpy(mp_buf_obverse, p_buf_obverse, size);
  77. mp_buf_reverse = (void*)malloc(size);
  78. memcpy(mp_buf_reverse, p_buf_reverse, size);
  79. m_size = size;
  80. }
  81. }
  82. int Snap7_buf::get_id()
  83. {
  84. return m_id;
  85. }
  86. int Snap7_buf::get_start_index()
  87. {
  88. return m_start_index;
  89. }
  90. int Snap7_buf::get_size()
  91. {
  92. return m_size;
  93. }
  94. void* Snap7_buf::get_buf_obverse()
  95. {
  96. return mp_buf_obverse;
  97. }
  98. void* Snap7_buf::get_buf_reverse()
  99. {
  100. return mp_buf_reverse;
  101. }
  102. Snap7_buf::Communication_mode Snap7_buf::get_communication_mode()
  103. {
  104. return m_communication_mode;
  105. }
  106. void Snap7_buf::set_communication_mode(Communication_mode communication_mode)
  107. {
  108. m_communication_mode = communication_mode;
  109. }