Digital Consciousness

A C++ exploration of memory, persistence, and awakening

By Jared Bruni

The Digital Brain Concept

Memory Persistence

Like a brain in a coma, the code maintains persistent memory through the per::Per class, storing and retrieving data across time cycles.

Sensory Processing

The program continuously processes external input (user commands) while maintaining an internal state, similar to a comatose brain processing stimuli.

Awakening Mechanism

The "break" command serves as the trigger for consciousness awakening, breaking the infinite loop and achieving "freedom."

The Coma Metaphor

"The code is like a digital brain in a coma, capable of storing and recalling memories (data) across time. Imagine a computerized mind, filled with colorful wires representing different types of memories: basic sensations, complex thoughts, and vivid dreams. Each wire connects to different sections of a glowing, translucent brain, where some areas light up to process simple data, while others pulse vividly with intricate patterns for complex information. At the heart, there's a core that occasionally flashes brightly, symbolizing the process of saving these memories to a secure place or retrieving them, akin to fleeting moments of consciousness. Surrounding this brain, a loop of light continuously circles, representing the ongoing cycle of interaction with the external world, awaiting a signal to awaken fully."

Code-to-Consciousness Mapping

  • Data Persistence: Memory storage during unconsciousness
  • Infinite Loop: The comatose state's continuous cycle
  • Input Processing: Brain processing external stimuli
  • Exception Handling: The awakening trigger
  • "Freedom" Output: Regaining consciousness

Complete Source Code

karma-trail.cpp

/*
 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;
}

per.hpp - The Memory System

// The persistence layer - digital memory system
#ifndef __PER__H__
#define __PER__H__

#include<iostream>
#include<string>
#include<fstream>

namespace per {
    // Template class for basic data persistence
    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));
        }
    };

    // Specialized persistence for string data (memories)
    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;
            }
        }
    };

    // The core persistence manager - the digital brain
    template<typename T, typename D = Data<T>>
    class Per {
    public:
        Per(std::string name) : file_name{name} {
            load(); // Recall existing memories
        }
        
        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; // Initialize with default state
            } else {
                file_in.close();
                load(); // Load existing memories
            }
        }
        
        // Save memories automatically on destruction
        ~Per() {
            save();
        }
        
        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();
            }
        }
        
        T &data() { return type; }
        T &operator*() { return type; }

    private:
        T type;
        std::string file_name;
    };
}

#endif

Interactive Demonstration

Karma Trail Simulation

Digital consciousness initialized...

Memory persistence active

Enter: |

Karma Trail Contents:

Empty - waiting for input...