Develop Biology
The language of life
Atom.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/chemical/Atom.h"
25
26namespace bio {
27namespace chemical {
28
30 :
31 physical::Class< Atom >(this),
32 m_bonds(4)
33{
34}
35
36Atom::Atom(const Atom& other)
37 :
38 physical::Class< Atom >(this),
39 m_bonds(other.m_bonds.GetCapacity())
40{
41
42}
43
45{
46
47}
48
50{
51 BIO_SANITIZE(other, ,
52 return code::BadArgument1())
53
54 const physical::Wave* demodulated = other->Demodulate();
55 Code ret = code::Success();
56
57 Bond* bondBuffer;
58 for (
60 !bnd.IsAtBeginning();
61 --bnd
62 )
63 {
64 bondBuffer = bnd;
65 if (bondBuffer->IsEmpty())
66 {
67 continue;
68 }
70 bondBuffer->GetBonded(),
71 other
72 ).size())
73 {
74 if (bondBuffer->GetBonded()->Attenuate(demodulated) != code::Success())
75 {
76 ret = code::UnknownError(); //user can debug from logs for now.
77 }
78 }
79 }
80 return ret;
81}
82
84{
85 BIO_SANITIZE(other, ,
86 return code::BadArgument1())
87
88 const physical::Wave* demodulated = other->Demodulate();
89 Code ret = code::Success();
90
91 Bond* bondBuffer;
92 for (
94 !bnd.IsAtBeginning();
95 --bnd
96 )
97 {
98 bondBuffer = bnd;
99 if (bondBuffer->IsEmpty())
100 {
101 continue;
102 }
104 bondBuffer->GetBonded(),
105 other
106 ).size())
107 {
108 if (bondBuffer->GetBonded()->Disattenuate(demodulated) != code::Success())
109 {
110 ret = code::UnknownError(); //user can debug from logs for now.
111 }
112 }
113 }
114 return ret;
115}
116
118 Wave* toBond,
119 AtomicNumber id,
120 BondType type
121)
122{
123 BIO_SANITIZE(!toBond || !id, ,
124 return false);
125
126 Valence position = GetBondPosition(id);
127 Bond* bondBuffer;
128 if (m_bonds.IsAllocated(position))
129 {
130 bondBuffer = m_bonds.OptimizedAccess(position);
131 BIO_SANITIZE(!bondBuffer->IsEmpty(), ,
132 return false)
133 return bondBuffer->Form(
134 id,
135 toBond,
136 type
137 );
138 }
139 //implicitly cast the addition index to a bool.
140 return m_bonds.Add(
141 Bond(
142 id,
143 toBond,
144 type
145 ));
146}
147
149 Wave* toBreak,
150 AtomicNumber id,
151 BondType type
152)
153{
154 Valence position = GetBondPosition(id);
155
156 BIO_SANITIZE(id && position, ,
157 return false);
158 BIO_SANITIZE(m_bonds.IsAllocated(position), ,
159 return false);
160
161 m_bonds.OptimizedAccess(position)->Break();
162 //Let dtor cleanup.
163
164 return true;
165}
166
167
169{
170 BIO_SANITIZE(bondedId, ,
171 return 0);
172 for (
173 SmartIterator bnd = m_bonds.End();
174 !bnd.IsAtBeginning();
175 --bnd
176 )
177 {
178 if ((*bnd).template As< Bond >() == bondedId)
179 {
180 return bnd.GetIndex();
181 }
182 }
183 return 0;
184}
185
187{
188 return GetBondPosition(PeriodicTable::Instance().GetIdWithoutCreation(typeName));
189}
190
191BondType Atom::GetBondType(Valence position) const
192{
193 BIO_SANITIZE(m_bonds.IsAllocated(position), ,
194 return BondTypePerspective::InvalidId());
195 return m_bonds.OptimizedAccess(position)->GetId();
196}
197
199{
200 //TODO...
201 return Wave::Spin();
202}
203
205{
206 //TODO...
207 return Wave::Reify(symmetry);
208}
209
211{
212 BIO_SANITIZE(m_bonds.IsAllocated(position), ,
213 return NULL)
214 return m_bonds.OptimizedAccess(position)->GetBonded();
215}
216
218{
219 BIO_SANITIZE(m_bonds.IsAllocated(position), ,
220 return NULL)
221 return m_bonds.OptimizedAccess(position)->GetBonded();
222}
223
225{
226 return &m_bonds;
227}
228
230{
231 return &m_bonds;
232}
233
234
235} //chemical namespace
236} //bio namespace
#define BIO_SANITIZE(test, success, failure)
virtual SmartIterator End() const
Definition: Container.cpp:302
virtual bool IsAllocated(const Index index) const
Definition: Container.cpp:107
bool IsAtBeginning() const
virtual physical::Symmetry * Spin() const
Definition: Atom.cpp:198
virtual Code Disattenuate(const Wave *other)
Definition: Atom.cpp:83
Wave * GetBonded(Valence position)
Definition: Atom.cpp:210
virtual bool BreakBondImplementation(Wave *toDisassociate, AtomicNumber id, BondType type)
Definition: Atom.cpp:148
Atom(const Atom &other)
Definition: Atom.cpp:29
Bonds * GetAllBonds()
Definition: Atom.cpp:224
virtual bool FormBondImplementation(Wave *toBond, AtomicNumber id, BondType type)
Definition: Atom.cpp:117
virtual ~Atom()
Definition: Atom.cpp:44
virtual Code Reify(physical::Symmetry *symmetry)
Definition: Atom.cpp:204
BondType GetBondType() const
Definition: Atom.h:352
virtual Code Attenuate(const Wave *other)
Definition: Atom.cpp:49
Valence GetBondPosition() const
Definition: Atom.h:326
bool IsEmpty() const
Definition: Bond.cpp:89
bool Form(AtomicNumber id, physical::Wave *bonded, BondType type=bond_type::Unknown())
Definition: Bond.cpp:55
physical::Wave * GetBonded()
Definition: Bond.cpp:74
Index Add(const ByteStream content)
Definition: Arrangement.h:60
virtual TYPE OptimizedAccess(Index index)
Definition: Arrangement.h:128
virtual Code Attenuate(const Wave *other)
Definition: Wave.cpp:81
virtual Code Disattenuate(const Wave *other)
Definition: Wave.cpp:86
virtual Wave * Demodulate()
Definition: Wave.cpp:99
static Properties GetResonanceBetween(ConstWaves waves)
Definition: Wave.cpp:209
Index Valence
Definition: Types.h:65
uint16_t AtomicNumber
Definition: Types.h:72
Code UnknownError()
Code BadArgument1()
Code Success()
Definition: Cell.h:31
const char * Name
Definition: Types.h:46