StructPort.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //-----------------------------------------------------------------------------
  2. // (c) 2009 by Basler Vision Technologies
  3. // Section: Vision Components
  4. // Project: GenApi
  5. // Author: Fritz Dierks
  6. // $Header$
  7. //
  8. // License: This file is published under the license of the EMVA GenICam Standard Group.
  9. // A text file describing the legal terms is included in your installation as 'GenICam_license.pdf'.
  10. // If for some reason you are missing this file please contact the EMVA or visit the website
  11. // (http://www.genicam.org) for a full copy.
  12. //
  13. // THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
  14. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD GROUP
  17. // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  20. // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. // POSSIBILITY OF SUCH DAMAGE.
  24. //-----------------------------------------------------------------------------
  25. /*!
  26. \file
  27. */
  28. #ifndef __STRUCTPORT_H
  29. #define __STRUCTPORT_H
  30. #include <cstring>
  31. #include <string.h>
  32. #include <GenApi/PortImpl.h>
  33. namespace GENAPI_NAMESPACE
  34. {
  35. //! Implements a register spaces based on a C++ struct
  36. template <class CDataStruct>
  37. class CTestPortStruct : public CDataStruct, public GENAPI_NAMESPACE::CPortImpl
  38. {
  39. public:
  40. CTestPortStruct(int64_t BaseAddress = 0)
  41. : m_BaseAddress(BaseAddress)
  42. {
  43. MemSet(0);
  44. ResetStatistics();
  45. }
  46. //-------------------------------------------------------------
  47. // IBase implementation
  48. //-------------------------------------------------------------
  49. //! Get the access mode of the node
  50. virtual GENAPI_NAMESPACE::EAccessMode GetAccessMode() const
  51. {
  52. return RW;
  53. }
  54. //! Get the type of the main interface of a node
  55. virtual GENAPI_NAMESPACE::EInterfaceType GetPrincipalInterfaceType() const
  56. {
  57. return intfIPort;
  58. }
  59. //---------------------------------------------------------------
  60. // IPort implementation
  61. //---------------------------------------------------------------
  62. //! Reads a chunk of bytes from the port
  63. virtual void Read(void *pBuffer, int64_t Address, int64_t Length)
  64. {
  65. int64_t InternalAddress = Address - m_BaseAddress;
  66. CDataStruct *pDataStruct = static_cast<CDataStruct*>(this);
  67. if( InternalAddress < 0 || Length < 0 || InternalAddress + Length > sizeof(CDataStruct) )
  68. throw RUNTIME_EXCEPTION("CTestPortStruct::Read - Invalid address and/or length");
  69. memcpy( pBuffer, (uint8_t*)pDataStruct + InternalAddress, (size_t)Length );
  70. m_NumReads++;
  71. }
  72. //! Writes a chunk of bytes to the port
  73. virtual void Write(const void *pBuffer, int64_t Address, int64_t Length)
  74. {
  75. int64_t InternalAddress = Address - m_BaseAddress;
  76. CDataStruct *pDataStruct = static_cast<CDataStruct*>(this);
  77. if( InternalAddress < 0 || Length < 0 || InternalAddress + Length > sizeof(CDataStruct) )
  78. throw RUNTIME_EXCEPTION("CTestPortStruct::Write - Invalid address and/or length");
  79. memcpy( (uint8_t*)pDataStruct + InternalAddress, pBuffer, (size_t)Length );
  80. m_NumWrites++;
  81. }
  82. //-------------------------------------------------------------
  83. // Utility functions
  84. //-------------------------------------------------------------
  85. void MemSet( const char FillValue )
  86. {
  87. memset( static_cast<CDataStruct*>(this), FillValue, sizeof(CDataStruct) );
  88. }
  89. //-------------------------------------------------------------
  90. // Statistics functions
  91. //-------------------------------------------------------------
  92. //! Resets the read/write statistics
  93. void ResetStatistics()
  94. {
  95. m_NumReads = 0;
  96. m_NumWrites = 0;
  97. }
  98. //! Returns the number of reads since lastReset Statistics
  99. int64_t GetNumReads()
  100. {
  101. return m_NumReads;
  102. }
  103. //! Returns the number of writes since lastReset Statistics
  104. int64_t GetNumWrites()
  105. {
  106. return m_NumWrites;
  107. }
  108. protected:
  109. //! Number of reads since last reset
  110. int64_t m_NumReads;
  111. //! Number of writes since last reset
  112. int64_t m_NumWrites;
  113. //! the base address used for the struct
  114. int64_t m_BaseAddress;
  115. };
  116. }
  117. #endif // __STRUCTPORT_H