The MeowBe class implements a monadic type that simulates Schrödinger's Cat in a box, represented as either meowable (alive) or not meowable (dead). This template class is designed to manage the lifecycle of objects safely, handling both copy and move semantics efficiently.
- Encapsulation of "nullable" or "optional" objects.
- Supports copy and move semantics for seamless usage in different contexts.
template <typename Cat>
class MeowBeCat: The type of the cat in the MeowBe box. Could be any cat from a Persian cat to a Danger kitty (can be any C++ type).
bool isMeowable: Indicates whether the cat (or object) is "meowable" (i.e., meowing and well).Cat kuro: The cat itself being managed. (Kuro here is a Sri-lankan gangmember of the clan.)
MeowBe();- Creates a box with no meow in it (non-meowable).
MeowBe(const Cat &yourCat);- Creates a box with a meowable cat (
yourcat).
MeowBe(MeowBe &randomBox);- Copies the contents of another
MeowBeinstance.
MeowBe(MeowBe&& randomBox) noexcept;- Transfers ownership of the cat from
randomBoxto the new instance, leavingrandomBoxempty.
~MeowBe();- Destroys the managed box and releasing any held cats from the memory.
MeowBe& operator=(const MeowBe& otherBox) noexcept;- Copies the cat of another
MeowBebox in a new box while avoiding the new cat going in the old box.
MeowBe& operator=(MeowBe&& otherBox) noexcept;- Moves the contents of another
MeowBebox, transferring cat and its ownership to a new box and destroying the original empty box.
bool isMeowing() const;- Returns
trueif the cat is preset and meowing.
bool isNotMeowing() const;- Returns
trueif the box is empty.
static MeowBe<RandomKitty> pure(const RandomKitty& neko);- takes a random kitty and puts it in a
MeowBebox and returns the static box
MeowBe<UpgradedCat> bind(std::function<MeowBe<UpgradedCat>(Cat)> f);Basically takes a cat out of the box, pets/feeds/doesWhateverYouWantWithTheCat and expects that you are responsible for putting the cat in the box and return.
- Applies a petting/feeding function
fto the cat (if present), returning a newMeowBebox containing the upgraded cat.
MeowBe<UpgradedCat> map(std::function<UpgradedCat(Cat)> f);Basically takes a cat out of the box, pets/feeds/doesWhateverYouWantWithTheCat and puts the new cat in the box and returns.
- Applies a petting/feeding function
fto the cat (if present), returning a newMeowBebox containing the upgraded cat.
Cat fromMeowBe(Cat someRandomCat);- Returns the contained cat if present, otherwise returns a provided fallback someRandomCat.
MeowBe<RandomCat> meow(const RandomCat& randomKuro);- Simply a constructor like function to give a randomCat a box. Can be used like just().
MeowBe<RandomCat> noMeow();- Simply a constructor like function to give an empty box with a specific type. Can be used like nothing().
#include "meowBe.hpp"
#include <iostream>
int main() {
auto value = meow<int>(42);
auto value2 = noMeow<int>();
std::cout << "Value: " << value.fromMeowBe(0) << std::endl;
std::cout << "Value2: " << value2.fromMeowBe(0) << std::endl;
return 0;
}Value: 42
Value2: 0
- C++17 or higher (for
std::functionand move semantics).
- The class uses manual placement new and destructor calls for flexibility.
- It implements the Monad pattern for functional programming style transformations.