libbio
Loading...
Searching...
No Matches
AtomAs.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) 2023 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
24// Out-of-line definitions of Atom::As< T >() / Atom::AsBonded< T >() /
25// Atom::operator T(). <br />
26//
27// This is NOT a standalone header: it requires bio::chemical::Atom to be a
28// COMPLETE type. It is #include'd at the very bottom of Atom.h, AFTER the Atom
29// class is closed, so that the bonded copy-on-write handle Cow< T > -- which
30// calls back into Atom -- is fully defined here; that ordering is what breaks
31// the Atom <-> Cow include cycle. Do not #include this directly; include
32// Atom.h, which pulls it in transparently (no call site needs an extra include).
33
35
36namespace bio {
37namespace chemical {
38
39template < typename T >
41{
42 BIO_STATIC_ASSERT(!type::IsReference< T >())
43 BIO_STATIC_ASSERT(!type::IsPointer< T >())
44
45 // Resolve a snapshot of the bonded under a shared lock (the same lookup the pre-COW
46 // AsBonded did), then hand out a DEFERRED handle: it shares the snapshot on read and
47 // copy-on-write detaches the source bond on write. The shared lock is released when
48 // this returns, so the handle's later Write takes a fresh exclusive (RFC §12).
49 SafelyAccessShared< Atom > guard(const_cast< Atom* >(this));
53 if (position && mBonds.IsAllocated(position))
54 {
55 snapshot = mBonds.OptimizedAccess(position)->GetBonded();
56 }
57 return Cow< T >(const_cast< Atom* >(this), bondId, snapshot);
58}
59
60template < typename T >
62 T,
64>::Type
65Atom::As() const
66{
67 // We store either a Wave-derived object or a Quantum<T> payload. T should be
68 // the bonded type itself (As<int>(), As<const Surface>()). Pointer spelling
69 // is still normalized here only so old template instantiations fail cleanly.
72 T,
74 >::As(*this);
75}
76
77template < typename T >
78Atom::operator T() const
79{
80 return As< T >();
81}
82
83} //chemical namespace
84} //bio namespace
#define BIO_STATIC_ASSERT(condition)
Definition AssertMacros.h:34
Definition Pointer.h:115
Definition Atom.h:154
Valence GetBondPositionLocked(AtomicNumber bondedId) const
Definition Atom.cpp:519
Cow< T > AsBonded() const
Definition AtomAs.h:40
atom_detail::AsResultType< T, type::IsWaveImplementation< typenametype::RemovePointer< T >::Type >::sValue >::Type As() const
Definition AtomAs.h:65
Bonds mBonds
Definition Atom.h:680
Index Valence
Definition chemical/common/Types.h:78
uint16_t AtomicNumber
Definition chemical/common/Types.h:85
BIO_CONSTEXPR bool IsPointer()
Definition IsPointer.h:54
Definition FinalCell.h:29
Definition IsWave.h:76