Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
MemoryAccessor.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "MemoryAccessor.h"
14 
15 
16 using namespace kome::core;
17 
18 
19 #include <crtdbg.h>
20 #ifdef _DEBUG
21  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
22  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
23 #endif // _DEBUG
24 
25 
26 
27 // constructor
28 MemoryAccessor::MemoryAccessor( void* addr, const unsigned int size ) {
29  m_addr = (unsigned char*)addr;
30  m_size = size;
31 
32  m_pos = 0;
33 }
34 
35 // destructor
37 }
38 
39 // get data
41  return m_addr;
42 }
43 
44 // get data size
45 unsigned int MemoryAccessor::getSize() {
46  return m_size;
47 }
48 
49 // set reading position
50 void MemoryAccessor::setPosition( const unsigned int pos ) {
51  m_pos = std::min( pos, m_size );
52 }
53 
54 // write data
55 int MemoryAccessor::write( void* addr, int size ) {
56  // check parameters
57  if( addr == NULL || size <= 0 ) {
58  return 0;
59  }
60 
61  // actual size
62  int actualSize = std::min( size, (int)m_size - (int)m_pos );
63  if( actualSize <= 0 ) {
64  return 0;
65  }
66 
67  // write
68  memcpy( m_addr + m_pos, addr, actualSize );
69  m_pos += actualSize;
70 
71  return actualSize;
72 }
73 
74 // read data
75 int MemoryAccessor::read( void* addr, int size ) {
76  // check parameters
77  if( addr == NULL || size <= 0 ) {
78  return 0;
79  }
80 
81  // actual size
82  int actualSize = std::min( size, (int)m_size - (int)m_pos );
83  if( actualSize <= 0 ) {
84  return 0;
85  }
86 
87  // read
88  memcpy( addr, m_addr + m_pos, actualSize );
89  m_pos += actualSize;
90 
91  return actualSize;
92 }
void * getData()
gets memory access point
virtual int read(void *addr, int size)
reads data (override method)
interfarces of MemoryAccessor class
MemoryAccessor(void *addr, const unsigned int size)
constructor
#define NULL
Definition: CoreMacros.h:18
virtual ~MemoryAccessor()
destructor
void setPosition(const unsigned int pos)
sets reading postion
virtual int write(void *addr, int size)
writes data (override method)
unsigned int getSize()
gets data size