Karma Trail - Persistent Data Metaphor
This code implements a C++ persistent data container that serves as both
a technical experiment and a personal metaphor about continuity, cycles,
consciousness, experience, and emerging from a coma.
Core Concept: The Karma Trail
"Karma Trail
Simple Concept
Coma
When you break the infinite while loop
you branch out to the greater reality"
- Jared Bruni
The name Per, short for persistent,
and the concept of repeatedly writing data to storage became part of
a larger metaphor.
-
Persistence:
State, history, and experience remain rather than simply disappearing.
-
The Karma Trail:
Actions and experiences accumulate into a continuing record.
-
The Infinite Loop:
A closed state that appears to continue indefinitely.
-
Breaking the Loop:
An interruption of the existing state and its normal control flow.
-
The Greater Reality:
Becoming aware of something larger than the state that previously
seemed complete.
Technical Implementation
The code implements a persistent data container using C++ templates
and binary file I/O.
#ifndef __PER__H__
#define __PER__H__
#include<iostream>
#include<string>
#include<fstream>
namespace per {
template<typename T>
class Data {
public:
Data() = default;
static void write(std::fstream &file, const T &type) {
if(file.is_open())
file.write(reinterpret_cast<const char *>(&type), sizeof(type));
}
static void read(std::fstream &file, T &type) {
if(file.is_open())
file.read(reinterpret_cast<char *>(&type), sizeof(type));
}
};
class StringData {
public:
StringData() = default;
static void write(std::fstream &file, const std::string &type) {
if(file.is_open()) {
int len = type.length();
file.write(reinterpret_cast<const char *>(&len), sizeof(len));
file.write(reinterpret_cast<const char *>(type.c_str()), len);
}
}
static void read(std::fstream &file, std::string &type) {
if(file.is_open()) {
int len;
file.read(reinterpret_cast<char *>(&len), sizeof(len));
char *buf = new char[len + 1];
file.read(buf, len);
buf[len] = 0;
type = buf;
delete [] buf;
}
}
};
template<typename C>
class StringContainerData {
public:
StringContainerData() = default;
static void write(std::fstream &file, const C &type) {
if(file.is_open()) {
for(auto it = type.begin(); it != type.end(); ++it) {
int len = it->length();
file.write(reinterpret_cast<const char *>(&len), sizeof(len));
file.write(reinterpret_cast<const char *>(it->c_str()), len);
}
}
}
static void read(std::fstream &file, C &type) {
if(file.is_open()) {
while(!file.eof()) {
int len;
file.read(reinterpret_cast<char *>(&len), sizeof(len));
if(file) {
char *buf = new char[len + 1];
file.read(buf, len);
buf[len] = 0;
std::string t{buf};
delete [] buf;
type.push_back(t);
}
}
}
}
};
template<typename C, typename T>
class ContainerData {
public:
ContainerData() = default;
static void write(std::fstream &file, const C &type) {
if(file.is_open()) {
for(auto i = type.begin(); i != type.end(); ++i) {
file.write(reinterpret_cast<const char *>(&*i), sizeof(*i));
}
}
}
static void read(std::fstream &file, C &type) {
if(file.is_open()) {
while(!file.eof()) {
T t;
file.read(reinterpret_cast<char *>(&t), sizeof(t));
if(file)
type.push_back(t);
else
break;
}
}
}
};
template<typename T, typename D = Data<T>>
class Per {
public:
Per(std::string name) : file_name{name} {
load();
}
Per(std::string name, const T &i) : file_name{name} {
std::fstream file_in;
file_in.open(file_name, std::ios::in | std::ios::binary);
if(!file_in.is_open()) {
type = i;
} else {
file_in.close();
load();
}
}
Per(const Per<T,D> &p, const std::string &file_n)
: file_name{file_n}, type{p.type} {}
Per(const Per<T,D> &&p, const std::string &file_n)
: file_name{file_n}, type{std::move(p.type)} {}
Per<T,D> &operator=(const Per<T,D> &p) = delete;
Per<T,D> &operator=(const Per<T,D> &&p) = delete;
Per(const Per<T,D> &) = delete;
Per(const Per<T,D> &&) = delete;
Per<T,D> &operator=(const T &t) {
type = t;
return *this;
}
Per<T,D> &operator=(const T &&t) {
type = std::move(t);
return *this;
}
void setFileName(const std::string &fn) {
file_name = fn;
}
std::string getFileName() const {
return file_name;
}
void save() {
std::fstream file_in;
file_in.open(file_name, std::ios::out | std::ios::binary | std::ios::trunc);
if(!file_in.is_open()) {
std::cerr << "Error could not open: "
<< file_name
<< " for output...\n";
exit(EXIT_FAILURE);
}
D::write(file_in, type);
file_in.close();
}
void load() {
std::fstream file_in;
file_in.open(file_name, std::ios::in | std::ios::binary);
if(file_in.is_open()) {
D::read(file_in, type);
file_in.close();
}
}
void set(const T &t) {
type = t;
}
void reset() {
type = T();
}
void clear() {
reset();
save();
}
~Per() {
save();
}
T &data() {
return type;
}
T &operator*() {
return type;
}
private:
T type;
std::string file_name;
};
}
#endif
/*
Karma Trail
Simple Concept
Coma
When you break the infinite while loop
you branch out to the greater reality
- Jared Bruni
*/
#include<iostream>
#include<string>
#include<algorithm>
#include "per.hpp"
class Escape {};
std::string give() {
std::string data;
std::cout << "Enter: ";
std::getline(std::cin, data);
return data;
}
std::string get(std::string data) {
if(data == "love")
std::cout << "love sent...\n";
else if(data == "fear")
std::cout << "fear sent...\n";
else if(data == "break")
throw Escape();
return data;
}
int main() {
try {
per::Per<std::string, per::StringData> trail("karma_trail.txt", "");
while(1) {
*trail += get(give()) + "\n";
std::cout << "karma trail: " << *trail << "\n";
}
}
catch(Escape &e) {
std::cout << "freedom..\n";
}
return 0;
}
Metaphorical Interpretation
Symbolic Meaning Behind Key Elements
1. Per<T,D> Class
The Per<T,D> class is essentially a wrapper around
a value that persists on disk:
per::Per<std::string, per::StringData> trail("karma_trail.txt", "");
The persistence mechanism gives the metaphor an important additional meaning.
The program can stop and later begin again without necessarily beginning
from nothing. The previous state can still exist and be loaded again.
Symbolically, this can represent continuity. A change in conscious state
does not necessarily erase everything that came before it. History,
memory, consequence, and identity can continue forward.
2. Infinite Loop & Escape Mechanism
while(1) {
*trail += get(give()) + "\n";
}
The infinite loop represents a state that continues without an ordinary endpoint.
- The program repeatedly receives input.
- Each new input becomes part of the persistent trail.
- The process continues indefinitely unless something changes the control flow.
-
Typing "break"
throws an
Escape exception.
The use of an exception makes the metaphor stronger than a normal
loop termination. The program does not simply proceed through the ordinary
execution path. Instead, the existing flow is interrupted and execution
jumps outside the entire cycle.
else if(data == "break")
throw Escape();
The exception is caught outside the loop:
catch(Escape &e) {
std::cout << "freedom..\n";
}
Symbolically, this represents an abrupt transition from one state into another.
3. File Persistence
The save() and load() functions preserve
the state of the object across program executions:
void save() { ... }
void load() { ... }
The trail does not simply vanish when the program stops. It can survive
the termination of one execution and become part of the next.
In the metaphor, this persistence represents the continuation of history
and identity. The system changes state, but what happened before remains
part of what comes afterward.
The Coma Metaphor
Metaphorical Framework
Control Flow as Metaphor
Final Interpretation
This code can be read as a small program about persistence,
consciousness, continuity, and transition.
A system exists inside a repeating state.
Its experiences accumulate and become part of a persistent trail.
The state appears endless while the system remains inside it.
Then the normal control flow is interrupted.
The loop ends.
What existed before is not necessarily erased.
The system continues into a greater reality.
Written after a coma, the program uses the technical language of C++
to express an experience that ordinary language can have difficulty representing.
Loops describe repetition. Persistence describes continuity.
State represents the condition of the system.
The Escape exception represents a sudden discontinuity
in normal execution. Branching outside the loop represents becoming
aware of something larger than the state that came before it.
In that sense, the code is not simply an illustration of a philosophical idea.
It is a personal metaphor expressed through the concepts of computer programming.