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