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
23
24namespace bio {
25
27 :
28 m_stream(NULL),
29 m_typeName(""),
30 m_size(0),
31 m_holding(false)
32{
33}
34
36{
37 *this = other;
38}
39
41{
42 Release();
43}
44
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
64{
65 return !m_stream;
66}
67
68std::string ByteStream::GetTypeName() const
69{
70 return m_typeName;
71}
72
73std::size_t ByteStream::GetSize() const
74{
75 return m_size;
76}
77
79{
80 return m_stream;
81}
82
83void ByteStream::Set(const ByteStream& other)
84{
85 m_stream = std::malloc(other.m_size);
86 memcpy(
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
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
108bool 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
bool IsEmpty() const
Definition: ByteStream.cpp:63
std::string GetTypeName() const
Definition: ByteStream.cpp:68
std::size_t GetSize() const
Definition: ByteStream.cpp:73
std::string m_typeName
Definition: ByteStream.h:219
std::size_t m_size
Definition: ByteStream.h:220
void * IKnowWhatImDoing()
Definition: ByteStream.cpp:78
void Set(T in)
Definition: ByteStream.h:147
bool operator==(const ByteStream &other) const
Definition: ByteStream.cpp:108
void operator=(const ByteStream &other)
Definition: ByteStream.cpp:45
Definition: Cell.h:31