libbio
Loading...
Searching...
No Matches
Cow.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) 2026 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
26#include "bio/common/Cast.h"
27
28namespace bio {
29
62template < typename T >
63class Cow :
65{
66public:
76
87 {
89 if (!bonded)
90 {
91 return NULL;
92 }
93 return ForceCast< physical::Class< T >* >(bonded.Get())->GetWaveObject();
94 }
95
96 // operator-> ALWAYS shares: it returns a Pointer<const T>, so cow->ConstMethod() is
97 // pointer-identical and zero-copy, while cow->NonConstMethod() is a compile error naming
98 // the fix ("...on a const T*"). Mutation is therefore explicit and can never silently
99 // bleed across clones -- take it via .Moo() (cow.Moo()->Mut()) or by extracting a
100 // mutable Pointer<T> (Pointer<T> p = cow, which detaches below). This is the only
101 // overload, so a non-const handle binds it too (a const member is callable on a
102 // non-const object) -- reads share regardless of the handle's const-ness.
104 {
105 return View();
106 }
107
108 // Extraction signals intent by target const-ness: a Pointer<const T> target SHARES (read);
109 // a Pointer<T> target copy-on-write DETACHES (write -- a private, uniquely-owned T).
110 operator Pointer< const T >() const
111 {
112 return View();
113 }
114
115 operator Pointer< T >()
116 {
117 return Moo();
118 }
119
120#if BIO_CPP_VERSION >= 11
121 // Per-method dispatch for a held handle that interleaves reads and writes:
122 // cow(&T::m, args...) reads m's const-ness off the member-pointer TYPE and routes
123 // share-vs-detach -- the one place a method's const-ness is visible at compile time
124 // (operator-> cannot see it; see the class TODO). A const m shares, a non-const m
125 // detaches via Moo(). Args bind to m's exact parameter types, so call-site conversions
126 // happen as usual. C++11+ only (variadic templates); cpp98 uses operator-> and Moo().
127 template < typename ReturnType, typename... Args >
128 ReturnType operator()(ReturnType (T::* method)(Args...) const, Args... args) const
129 {
130 return (View() ->* method)(args...);
131 }
132
133 template < typename ReturnType, typename... Args >
134 ReturnType operator()(ReturnType (T::* method)(Args...), Args... args)
135 {
136 return (Moo() ->* method)(args...);
137 }
138#endif
139
140private:
141 // The internal SHARE path behind operator->, the Pointer<const T> conversion, and the const
142 // dispatch overload. NOT public: reads already go through those (cow->ConstMethod(),
143 // Pointer<const X> p = cow), so an explicit View() would only re-expose -- and invite -- the
144 // share path the public surface is built to make implicit. Mutation stays explicit via
145 // the public Moo(); the asymmetry is the point (reads default, writes deliberate).
146 Pointer< const T > View() const
147 {
148 Pointer< const physical::Wave > bonded = ReadBonded();
149 if (!bonded)
150 {
151 return NULL;
152 }
153 return ForceCast< const physical::Class< T >* >(bonded.Get())->GetWaveObject();
154 }
155};
156
157} // namespace bio
Definition Cow.h:65
Pointer< T > Moo()
Definition Cow.h:86
Cow(Pointer< chemical::Atom > source, chemical::AtomicNumber bondId, Pointer< physical::Wave > bondedSnapshot)
Definition Cow.h:67
Pointer< const T > operator->() const
Definition Cow.h:103
Definition Pointer.h:115
T * Get() const
Definition Pointer.h:271
Definition AtomicCow.h:55
const physical::Wave * ReadBonded() const
Definition AtomicCow.h:88
AtomicCow(Pointer< Atom > source, AtomicNumber bondId, Pointer< physical::Wave > bondedSnapshot)
Definition AtomicCow.h:63
Pointer< physical::Wave > WriteBonded()
Definition AtomicCow.h:98
Definition FinalCell.h:29