Develop Biology
The language of life
ByteStream.cpp
Go to the documentation of this file.
1
/*
2
* This file is a part of the Biology project by eons LLC.
3
* Biology (aka Develop Biology) is a framework for approaching software
4
* development from a natural sciences perspective.
5
*
6
* Copyright (C) 2021 Séon O'Shannon & eons LLC
7
*
8
* This program is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU Affero General Public License as
10
* published by the Free Software Foundation, either version 3 of the
11
* License, or (at your option) any later version.
12
*
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU Affero General Public License for more details.
17
*
18
* You should have received a copy of the GNU Affero General Public License
19
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20
*/
21
22
#include "
bio/common/ByteStream.h
"
23
24
namespace
bio
{
25
26
ByteStream::ByteStream
()
27
:
28
m_stream(NULL),
29
m_typeName(
""
),
30
m_size(0),
31
m_holding(false)
32
{
33
}
34
35
ByteStream::ByteStream
(
const
ByteStream
& other)
36
{
37
*
this
= other;
38
}
39
40
ByteStream::~ByteStream
()
41
{
42
Release
();
43
}
44
45
void
ByteStream::operator=
(
const
ByteStream
& other)
46
{
47
Release
();
//wipe old state.
48
49
if
(other.
m_holding
)
50
{
51
//We can't free the same memory twice, so we have to allocate a new block for ourselves.
52
Set
(other);
53
}
54
else
55
{
56
m_stream
= other.
m_stream
;
57
m_typeName
= other.
m_typeName
;
58
m_size
= other.
m_size
;
59
m_holding
=
false
;
60
}
61
}
62
63
bool
ByteStream::IsEmpty
()
const
64
{
65
return
!
m_stream
;
66
}
67
68
std::string
ByteStream::GetTypeName
()
const
69
{
70
return
m_typeName
;
71
}
72
73
std::size_t
ByteStream::GetSize
()
const
74
{
75
return
m_size
;
76
}
77
78
void
*
ByteStream::IKnowWhatImDoing
()
79
{
80
return
m_stream
;
81
}
82
83
void
ByteStream::Set
(
const
ByteStream
& other)
84
{
85
m_stream
= std::malloc(other.
m_size
);
86
memcpy(
87
m_stream
,
88
other.
m_stream
,
89
other.
m_size
90
);
91
m_size
= other.
m_size
;
92
m_typeName
= other.
m_typeName
;
93
m_holding
=
true
;
94
}
95
96
void
ByteStream::Release
()
97
{
98
if
(!
m_holding
)
99
{
100
return
;
101
}
102
std::free(
m_stream
);
103
m_size
= 0;
104
m_typeName
.clear();
105
m_holding
=
false
;
106
}
107
108
bool
ByteStream::operator==
(
const
ByteStream
& other)
const
109
{
110
if
(
m_size
!= other.
m_size
||
m_typeName
!= other.
m_typeName
)
111
{
112
return
false
;
113
}
114
return
memcmp(
115
m_stream
,
116
other.
m_stream
,
117
m_size
118
) == 0;
119
}
120
121
}
//bio namespace
ByteStream.h
bio::ByteStream
Definition:
ByteStream.h:48
bio::ByteStream::~ByteStream
~ByteStream()
Definition:
ByteStream.cpp:40
bio::ByteStream::IsEmpty
bool IsEmpty() const
Definition:
ByteStream.cpp:63
bio::ByteStream::GetTypeName
std::string GetTypeName() const
Definition:
ByteStream.cpp:68
bio::ByteStream::GetSize
std::size_t GetSize() const
Definition:
ByteStream.cpp:73
bio::ByteStream::m_typeName
std::string m_typeName
Definition:
ByteStream.h:219
bio::ByteStream::m_stream
void * m_stream
Definition:
ByteStream.h:218
bio::ByteStream::m_size
std::size_t m_size
Definition:
ByteStream.h:220
bio::ByteStream::IKnowWhatImDoing
void * IKnowWhatImDoing()
Definition:
ByteStream.cpp:78
bio::ByteStream::Release
void Release()
Definition:
ByteStream.cpp:96
bio::ByteStream::ByteStream
ByteStream()
Definition:
ByteStream.cpp:26
bio::ByteStream::Set
void Set(T in)
Definition:
ByteStream.h:147
bio::ByteStream::operator==
bool operator==(const ByteStream &other) const
Definition:
ByteStream.cpp:108
bio::ByteStream::operator=
void operator=(const ByteStream &other)
Definition:
ByteStream.cpp:45
bio::ByteStream::m_holding
bool m_holding
Definition:
ByteStream.h:221
bio
Definition:
Cell.h:31
src
common
ByteStream.cpp
Generated by
1.9.4