Belle II Software development
BufferedWriter Class Reference
Inheritance diagram for BufferedWriter:
Writer

Public Member Functions

 BufferedWriter (size_t size, unsigned char *memory=NULL)
 
 BufferedWriter (const BufferedWriter &w)
 
void seekTo (size_t pos)
 
unsigned char * ptr ()
 
size_t count () const
 
size_t size () const
 
size_t write (const void *, size_t) override
 
bool available () override
 
const BufferedWriteroperator= (const BufferedWriter &)
 
virtual void writeBool (bool)
 
virtual void writeChar (char)
 
virtual void writeShort (short)
 
virtual void writeInt (int)
 
virtual void writeLong (long long)
 
virtual void writeUChar (unsigned char)
 
virtual void writeUShort (unsigned short)
 
virtual void writeUInt (unsigned int)
 
virtual void writeULong (unsigned long long)
 
virtual void writeFloat (float)
 
virtual void writeDouble (double)
 
virtual void writeString (const std::string &)
 
virtual void writeObject (const Serializable &)
 

Protected Member Functions

void reverse (void *buf, size_t len)
 

Protected Attributes

unsigned char * m_memory
 
size_t m_size
 
size_t m_pos
 
bool m_allocated
 

Detailed Description

Definition at line 19 of file BufferedWriter.h.

Constructor & Destructor Documentation

◆ BufferedWriter() [1/3]

Definition at line 16 of file BufferedWriter.cc.

17 : m_memory(NULL), m_size(0), m_pos(0), m_allocated(false) {}

◆ BufferedWriter() [2/3]

BufferedWriter ( size_t  size,
unsigned char *  memory = NULL 
)

Definition at line 19 of file BufferedWriter.cc.

20 : m_memory(memory), m_size(size), m_pos(0), m_allocated(false)
21{
22 if (memory == NULL) {
23 m_memory = new unsigned char[size];
24 m_allocated = true;
25 }
26}

◆ BufferedWriter() [3/3]

Definition at line 28 of file BufferedWriter.cc.

29 : m_memory(NULL), m_size(writer.m_size),
30 m_pos(writer.m_pos), m_allocated(writer.m_allocated)
31{
32 if (m_allocated) {
33 m_memory = new unsigned char [writer.m_size];
34 ::memcpy(m_memory, writer.m_memory, writer.m_size);
35 } else {
36 m_memory = writer.m_memory;
37 }
38 m_size = writer.m_size;
39 m_pos = writer.m_pos;
40}

◆ ~BufferedWriter()

~BufferedWriter ( )
virtual

Definition at line 42 of file BufferedWriter.cc.

43{
44 if (m_allocated && m_memory != NULL) delete [] m_memory;
45}

Member Function Documentation

◆ available()

bool available ( )
inlineoverridevirtual

Implements Writer.

Definition at line 35 of file BufferedWriter.h.

35{ return true; }

◆ count()

size_t count ( ) const
inline

Definition at line 32 of file BufferedWriter.h.

32{ return m_pos; }

◆ operator=()

const BufferedWriter & operator= ( const BufferedWriter writer)

Definition at line 47 of file BufferedWriter.cc.

48{
49 if (m_allocated) {
50 delete [] m_memory;
51 }
52 m_allocated = writer.m_allocated;
53 if (m_allocated) {
54 m_memory = new unsigned char [writer.m_size];
55 ::memcpy(m_memory, writer.m_memory, writer.m_size);
56 } else {
57 m_memory = writer.m_memory;
58 }
59 m_size = writer.m_size;
60 m_pos = writer.m_pos;
61 return *this;
62}

◆ ptr()

unsigned char * ptr ( )
inline

Definition at line 31 of file BufferedWriter.h.

31{ return m_memory; }

◆ reverse()

void reverse ( void *  buf,
size_t  len 
)
protectedinherited

Definition at line 18 of file Writer.cc.

19{
20 for (int c = 0 ; c < (int)len / 2 ; c ++) {
21 char tmp = *((char*)buf + c);
22 *((char*)buf + c) = *((char*)buf + len - 1 - c);
23 *((char*)buf + len - 1 - c) = tmp;
24 }
25}

◆ seekTo()

void seekTo ( size_t  pos)
inline

Definition at line 30 of file BufferedWriter.h.

30{ m_pos = pos; }

◆ size()

size_t size ( ) const
inline

Definition at line 33 of file BufferedWriter.h.

33{ return m_size; }

◆ write()

size_t write ( const void *  buf,
size_t  count 
)
overridevirtual

Implements Writer.

Definition at line 64 of file BufferedWriter.cc.

65{
66 if (m_pos + count > m_size) {
67 throw (IOException("out of buffer range: %d+%d>%d",
68 (int)m_pos, (int)count, (int)m_size));
69 }
70 memcpy((m_memory + m_pos), buf, count);
71 m_pos += count;
72 return count;
73}

◆ writeBool()

void writeBool ( bool  v)
virtualinherited

Definition at line 27 of file Writer.cc.

28{
29 write(&v, sizeof(bool));
30}

◆ writeChar()

void writeChar ( char  v)
virtualinherited

Definition at line 32 of file Writer.cc.

33{
34 write(&v, sizeof(char));
35}

◆ writeDouble()

void writeDouble ( double  v)
virtualinherited

Definition at line 98 of file Writer.cc.

99{
100#if defined(__ENDIAN_L__)
101 reverse(&v, sizeof(double));
102#endif
103 write(&v, sizeof(double));
104}

◆ writeFloat()

void writeFloat ( float  v)
virtualinherited

Definition at line 90 of file Writer.cc.

91{
92#if defined(__ENDIAN_L__)
93 reverse(&v, sizeof(float));
94#endif
95 write(&v, sizeof(float));
96}

◆ writeInt()

void writeInt ( int  v)
virtualinherited

Definition at line 58 of file Writer.cc.

59{
60#if defined(__ENDIAN_L__)
61 reverse(&v, sizeof(int));
62#endif
63 write(&v, sizeof(int));
64}

◆ writeLong()

void writeLong ( long long  v)
virtualinherited

Definition at line 74 of file Writer.cc.

75{
76#if defined(__ENDIAN_L__)
77 reverse(&v, sizeof(long long));
78#endif
79 write(&v, sizeof(long long));
80}

◆ writeObject()

void writeObject ( const Serializable v)
virtualinherited

Definition at line 112 of file Writer.cc.

113{
114 return v.writeObject(*this);
115}

◆ writeShort()

void writeShort ( short  v)
virtualinherited

Definition at line 42 of file Writer.cc.

43{
44#if defined(__ENDIAN_L__)
45 reverse(&v, sizeof(short));
46#endif
47 write(&v, sizeof(short));
48}

◆ writeString()

void writeString ( const std::string &  v)
virtualinherited

Definition at line 106 of file Writer.cc.

107{
108 writeInt((int)v.size());
109 write(v.data(), v.size());
110}

◆ writeUChar()

void writeUChar ( unsigned char  v)
virtualinherited

Definition at line 37 of file Writer.cc.

38{
39 write(&v, sizeof(unsigned char));
40}

◆ writeUInt()

void writeUInt ( unsigned int  v)
virtualinherited

Definition at line 66 of file Writer.cc.

67{
68#if defined(__ENDIAN_L__)
69 reverse(&v, sizeof(unsigned int));
70#endif
71 write(&v, sizeof(unsigned int));
72}

◆ writeULong()

void writeULong ( unsigned long long  v)
virtualinherited

Definition at line 82 of file Writer.cc.

83{
84#if defined(__ENDIAN_L__)
85 reverse(&v, sizeof(unsigned long long));
86#endif
87 write(&v, sizeof(unsigned long long));
88}

◆ writeUShort()

void writeUShort ( unsigned short  v)
virtualinherited

Definition at line 50 of file Writer.cc.

51{
52#if defined(__ENDIAN_L__)
53 reverse(&v, sizeof(unsigned short));
54#endif
55 write(&v, sizeof(unsigned short));
56}

Member Data Documentation

◆ m_allocated

bool m_allocated
protected

Definition at line 46 of file BufferedWriter.h.

◆ m_memory

unsigned char* m_memory
protected

Definition at line 43 of file BufferedWriter.h.

◆ m_pos

size_t m_pos
protected

Definition at line 45 of file BufferedWriter.h.

◆ m_size

size_t m_size
protected

Definition at line 44 of file BufferedWriter.h.


The documentation for this class was generated from the following files: