Develop Biology
The language of life
bio::ByteStream Class Reference

#include <ByteStream.h>

Public Member Functions

 ByteStream ()
 
 ByteStream (const ByteStream &other)
 
template<typename T >
 ByteStream (T in)
 
 ~ByteStream ()
 
template<typename T >
As ()
 
template<typename T >
const T As () const
 
std::size_t GetSize () const
 
std::string GetTypeName () const
 
void * IKnowWhatImDoing ()
 
template<typename T >
bool Is () const
 
template<typename T >
bool Is (const T &t) const
 
bool IsEmpty () const
 
template<typename T >
 operator const T () const
 
template<typename T >
 operator T ()
 
void operator= (const ByteStream &other)
 
bool operator== (const ByteStream &other) const
 
void Release ()
 
void Set (const ByteStream &other)
 
template<typename T >
void Set (T in)
 

Protected Attributes

bool m_holding
 
std::size_t m_size
 
void * m_stream
 
std::string m_typeName
 

Detailed Description

Generic byte stream class. Kinda like a void* that you can save and cast later. Work around for c++98 auto keyword and other wonky problems.

               DO NOT USE THIS IMPROPERLY!!

If you don't understand what this does and how it CAN GO HORRIBLY WRONG, DO NOT USE THIS CLASS!

This is used by BIO_SANITIZE_WITH_CACHE and Containers.

NOTE: ByteStreams are not virtual to save what space we can. This may change in a future release if we decide we somehow need more hacky, abstract storage.

Definition at line 47 of file ByteStream.h.

Constructor & Destructor Documentation

◆ ByteStream() [1/3]

bio::ByteStream::ByteStream ( )

DON'T USE THIS UNLESS YOU KNOW WHAT YOU'RE DOING

Definition at line 26 of file ByteStream.cpp.

27 :
28 m_stream(NULL),
29 m_typeName(""),
30 m_size(0),
31 m_holding(false)
32{
33}
std::string m_typeName
Definition: ByteStream.h:219
std::size_t m_size
Definition: ByteStream.h:220

◆ ByteStream() [2/3]

template<typename T >
bio::ByteStream::ByteStream ( in)
inline

Definition at line 56 of file ByteStream.h.

57 {
58 Set(in);
59 }
void Set(T in)
Definition: ByteStream.h:147

References Set().

◆ ByteStream() [3/3]

bio::ByteStream::ByteStream ( const ByteStream other)
Parameters
other

Definition at line 35 of file ByteStream.cpp.

36{
37 *this = other;
38}

◆ ~ByteStream()

bio::ByteStream::~ByteStream ( )

Definition at line 40 of file ByteStream.cpp.

41{
42 Release();
43}

References Release().

Member Function Documentation

◆ As() [1/2]

template<typename T >
T bio::ByteStream::As ( )
inline

Casts stored data to T.

Template Parameters
T
Returns
stored bytes as T.

Definition at line 89 of file ByteStream.h.

90 {
91 BIO_ASSERT(Is< T >());
92 T* ret;
93 std::memcpy(
94 ret,
96 sizeof(T));
97 return *ret;
98 }
#define BIO_ASSERT(cond)
Definition: AssertMacros.h:29

References BIO_ASSERT, and m_stream.

◆ As() [2/2]

template<typename T >
const T bio::ByteStream::As ( ) const
inline

Casts stored data to T.

Template Parameters
T
Returns
stored bytes as T.

Definition at line 106 of file ByteStream.h.

107 {
108 BIO_ASSERT(Is< T >());
109 T* ret;
110 std::memcpy(
111 ret,
112 m_stream,
113 sizeof(T));
114 return *ret;
115 }

References BIO_ASSERT, and m_stream.

◆ GetSize()

std::size_t bio::ByteStream::GetSize ( ) const
Returns
the number of bytes *this points to.

Definition at line 73 of file ByteStream.cpp.

74{
75 return m_size;
76}

References m_size.

◆ GetTypeName()

std::string bio::ByteStream::GetTypeName ( ) const
Returns
the type stored in *this as a string.

Definition at line 68 of file ByteStream.cpp.

69{
70 return m_typeName;
71}

References m_typeName.

◆ IKnowWhatImDoing()

void * bio::ByteStream::IKnowWhatImDoing ( )

Assume the caller knows something we don't. Please don't use this.

Returns
the data in *this

Definition at line 78 of file ByteStream.cpp.

79{
80 return m_stream;
81}

References m_stream.

Referenced by bio::genetic::Insertion::Seek(), and bio::genetic::Localization::Seek().

◆ Is() [1/2]

template<typename T >
bool bio::ByteStream::Is ( ) const
inline

Check if Set was called with T.

Template Parameters
T
Returns
whether or not *this should be pointing to data of type T.

Definition at line 183 of file ByteStream.h.

184 {
185 return sizeof(T) == m_size && TypeName< T >() == m_typeName;
186 }

References m_size, and m_typeName.

Referenced by bio::physical::Arrangement< TYPE >::Add(), bio::physical::Arrangement< TYPE >::AreEqual(), and bio::physical::Line::AreEqual().

◆ Is() [2/2]

template<typename T >
bool bio::ByteStream::Is ( const T &  t) const
inline

Auto template determining version of Is<T>().

Template Parameters
T
Parameters
tonly used for automatically determining T.
Returns
whether or not *this should be pointing to data of type T.

Definition at line 195 of file ByteStream.h.

196 {
197 return Is< T >();
198 }

◆ IsEmpty()

bool bio::ByteStream::IsEmpty ( ) const

Check if *this has been Set.

Returns
Whether or not *this points to any possibly valid data.

Definition at line 63 of file ByteStream.cpp.

64{
65 return !m_stream;
66}

References m_stream.

◆ operator const T()

template<typename T >
bio::ByteStream::operator const T ( ) const
inline

Casts stored data to T.

Template Parameters
T
Returns
stored bytes as T.

Definition at line 134 of file ByteStream.h.

135 {
136 return As< T >();
137 }

◆ operator T()

template<typename T >
bio::ByteStream::operator T ( )
inline

Casts stored data to T.

Template Parameters
T
Returns
stored bytes as T.

Definition at line 123 of file ByteStream.h.

124 {
125 return As< T >();
126 }

◆ operator=()

void bio::ByteStream::operator= ( const ByteStream other)
Parameters
other

Definition at line 45 of file ByteStream.cpp.

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}

References m_holding, m_size, m_stream, m_typeName, Release(), and Set().

◆ operator==()

bool bio::ByteStream::operator== ( const ByteStream other) const

Compares the memory contained in both *this and other.

Parameters
other
Returns
whether or not other holds the same bits as *this.

Definition at line 108 of file ByteStream.cpp.

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}

References m_size, m_stream, and m_typeName.

◆ Release()

void bio::ByteStream::Release ( )

Frees the memory this was Holding. Nop if *this was not holding anything. NOTE: This does not call any destructors. You must do that yourself. (i.e. there is no typename -> new type -> union -> delete; delete)

Definition at line 96 of file ByteStream.cpp.

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}

References m_holding, m_size, m_stream, and m_typeName.

Referenced by ~ByteStream(), and operator=().

◆ Set() [1/2]

void bio::ByteStream::Set ( const ByteStream other)

Copies the data from an other into *this and Holds it.

Definition at line 83 of file ByteStream.cpp.

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}

References m_holding, m_size, m_stream, and m_typeName.

◆ Set() [2/2]

template<typename T >
void bio::ByteStream::Set ( in)
inline

Copies the data given to a new memory location. This should be used if the provided "in" is expected to go out of scope but the value still be valid. Make sure you Release *this to delete the stored content.

Template Parameters
T
Parameters
indata to store

Definition at line 147 of file ByteStream.h.

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 }

References m_size, m_stream, and m_typeName.

Referenced by ByteStream(), bio::chemical::ExcitationWithoutArgument< WAVE, RETURN >::CallDown(), bio::chemical::ExcitationWithArgument< WAVE, RETURN, ARGUMENT >::CallDown(), bio::chemical::ExcitationWithTwoArguments< WAVE, RETURN, ARGUMENT1, ARGUMENT2 >::CallDown(), operator=(), bio::physical::Filterable::Spin(), bio::physical::Periodic::Spin(), and bio::physical::Quantum< T >::Spin().

Member Data Documentation

◆ m_holding

bool bio::ByteStream::m_holding
protected

Definition at line 221 of file ByteStream.h.

Referenced by operator=(), Release(), and Set().

◆ m_size

std::size_t bio::ByteStream::m_size
protected

Definition at line 220 of file ByteStream.h.

Referenced by GetSize(), Is(), operator=(), operator==(), Release(), and Set().

◆ m_stream

void* bio::ByteStream::m_stream
protected

Definition at line 218 of file ByteStream.h.

Referenced by As(), IKnowWhatImDoing(), IsEmpty(), operator=(), operator==(), Release(), and Set().

◆ m_typeName

std::string bio::ByteStream::m_typeName
protected

Definition at line 219 of file ByteStream.h.

Referenced by GetTypeName(), Is(), operator=(), operator==(), Release(), and Set().


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