Develop Biology
The language of life
ByteStream.h
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#pragma once
23
25#include "TypeName.h"
26#include <cstddef>
27#include <cstdlib>
28#include <cstring>
29
30namespace bio {
31
48{
49public:
53 ByteStream();
54
55 template < typename T >
57 {
58 Set(in);
59 }
60
64 ByteStream(const ByteStream& other);
65
70
74 void operator=(const ByteStream& other);
75
81 bool operator==(const ByteStream& other) const;
82
88 template < typename T >
89 T As()
90 {
91 BIO_ASSERT(Is< T >());
92 T* ret;
93 std::memcpy(
94 ret,
96 sizeof(T));
97 return *ret;
98 }
99
105 template < typename T >
106 const T As() const
107 {
108 BIO_ASSERT(Is< T >());
109 T* ret;
110 std::memcpy(
111 ret,
112 m_stream,
113 sizeof(T));
114 return *ret;
115 }
116
122 template < typename T >
123 operator T()
124 {
125 return As< T >();
126 }
127
133 template < typename T >
134 operator const T() const
135 {
136 return As< T >();
137 }
138
146 template < typename T >
147 void Set(T in)
148 {
149 m_stream = std::malloc(sizeof(T));
150 std::memcpy(
151 m_stream,
152 &in,
153 sizeof(T));
154 m_size = sizeof(T);
155 m_typeName = TypeName< T >();
156 }
157
161 void Set(const ByteStream& other);
162
169 void Release();
170
175 bool IsEmpty() const;
176
182 template < typename T >
183 bool Is() const
184 {
185 return sizeof(T) == m_size && TypeName< T >() == m_typeName;
186 }
187
194 template < typename T >
195 bool Is(const T& t) const
196 {
197 return Is< T >();
198 }
199
203 std::string GetTypeName() const;
204
208 std::size_t GetSize() const;
209
215 void* IKnowWhatImDoing();
216
217protected:
218 void* m_stream;
219 std::string m_typeName;
220 std::size_t m_size;
222};
223} //bio namespace
#define BIO_ASSERT(cond)
Definition: AssertMacros.h:29
const T As() const
Definition: ByteStream.h:106
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
ByteStream(T in)
Definition: ByteStream.h:56
std::size_t m_size
Definition: ByteStream.h:220
void * IKnowWhatImDoing()
Definition: ByteStream.cpp:78
bool Is() const
Definition: ByteStream.h:183
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
bool Is(const T &t) const
Definition: ByteStream.h:195
Definition: Cell.h:31