GithubHelp home page GithubHelp logo

resource's Introduction

Universal resource handling class template for C++

This is the unviersal class resource class. All init, cleanup routines can be expressed as policies, with good defaults already provided. Policies can also be used to choose storage and copy behaviour.

unique_ptr-like behaviour

using Res = resource<int>;
Res ptr1{new int{42}};
Res ptr2; // ptr2{nullptr}

// ptr2 = ptr1; // error
ptr2 = std::move(ptr1); // ok

// ptr2 gets deleted at the end of scope

with an array

using Res = resource<int[]>;
Res ptr1{new int[42]};
Res ptr2; // ptr2{nullptr}

// ptr2 = ptr1; // error
ptr2 = std::move(ptr1); // ok

// ptr2 gets delete[]d at the end of scope

with custom deleter

using deleter = cleanup::function_deleter<void(void*), &free>;
using Res = resource<void, deleter::impl>;
Res ptr1{malloc(1024)};
Res ptr2; // ptr2{nullptr}

// ptr2 = ptr1; // error
ptr2 = std::move(ptr1); // ok

// ptr2 gets free()d at the end of scope

With your file descriptor RAII class

unique_ptr cannot do this - at least not without pointless allocation.

using deleter = cleanup::function_deleter<int(int), &close>;

using Res = resource<
	traits::handle_is_type<int, traits::nullable::yes, int, -1>,
	deleter::impl
>;

Res r1{open("/dev/random", O_RDONLY)};
Res r2; // initialized with -1

assert(*r2 == -1)
// r2 = r1; // error, but the copy policy can be convinced to use dup(2)
r2 = std::move(r1);

// r2 gets close(2)d.

lock_guard-like behaviour

The cleanup policy can also define initialize method.

template<typename T, typename Resource>
struct MutexLocker
{
	void initialize()
	{
		auto& full_type = static_cast<Resource&>(*this);
		full_type->lock();
	}

	void cleanup()
	{
		auto& full_type = static_cast<Resource&>(*this);
		full_type->unlock();
	}
};

And the usage:

using Res = resource<traits::reference<std::mutex>,MutexLocker>;
// reference trait is not nullable

std::mutex m;
Res r{m}; // m.lock() called

// Res r2 = r; // error
// Res r2 = std::move(r); // error

// m.unlock() called

Why? My unique_ptr and lock_guard are better/good enough?

Most likely you're right. This was a programming exercise.

License

If you really want to use this, look here.

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.