Code Monkey home page Code Monkey logo

cow's Introduction

Cow version codecov.badge

Good old copy on write

Overview

Cow provides an easy way to seamlessly augment any copyable type with lazy copy-on-write semantics.

#include "cow.hpp"

#include <chrono>
#include <future>
#include <iostream>
#include <thread>

// consider a data type that is expensive to copy
struct expensive_to_copy {

    /* ... */

    expensive_to_copy() { /* ... */ };

    expensive_to_copy(expensive_to_copy const&) {

        /* ... */

        std::cout << "copied" << std::endl;

        /* ... */
    }

    void save_to_disk() const {

        /* ... */

        using namespace std::chrono_literals;

        std::cout << "saving..." << std::endl;
        std::this_thread::sleep_for(2s);
        std::cout << "saving complete" << std::endl;

        /* ... */
    }

    void mutate() {

        /* ... */

        std::cout << "mutated" << std::endl;

        /* ... */
    }

    /* ... */
};

// and a simple document type built around it
class document {

    cow::value<expensive_to_copy> data;

public:
    // save asynchronously
    std::future<void> save() const {

        // safely capture by value - no copy is performed
        return std::async([alias = data] {
            alias->save_to_disk();
        });
    }

    void update() {
        cow::unbox(data, [](expensive_to_copy& ref) {
            ref.mutate();
        });
    }
};

int main() {
    document doc;

    doc.update();           // no copy performed
    doc.save().get();       // no copy performed - blocks until finished
    doc.update();           // no copy performed
    auto fut = doc.save();  // no copy performed - runs asynchronously

    using namespace std::chrono_literals;
    std::this_thread::sleep_for(1s);

    doc.update(); // possible copy if the asynchronous operation hasn't completed yet
}

Probable output:

mutated
saving...
saving complete
mutated
saving...
copied
mutated
saving complete

Quick Start

  1. Download cow.hpp
  2. # include </path/to/cow.hpp>

License

This project is licensed under the MIT.

cow's People

Contributors

brunocodutra avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.