Code Monkey home page Code Monkey logo

sboptr's Introduction

SboPtr

C++ smart pointer type with configurable small buffer storage and value semantics.

Installation

The library is a single header file, so you can just download that.

Using conan, you can get it from my bintray repository.

Example

struct interface {
	// Virtual destructor is needed
	virtual ~interface() = default;

	virtual int foo() = 0;
};

struct small_impl : public interface {
	int value;
	small_impl(int value) : value{value} {}
	int foo() override { return value; }
};

struct big_impl : public interface {
	std::array<int, 500> value;
	int foo() override { return value[0]; }
};

using my_pointer_type = sboptr::basic_sbo_ptr<
	interface,
	/* size of small buffer */ sizeof(void*) * 2,
	sboptr::movable
    | sboptr::copyable
    | sboptr::allow_heap>;

// Does not allocate
my_pointer_type a = small_impl{10};
std::cout << a->foo() << "\n"; // prints 10

// Heap allocation
a = big_impl{{50}};

// Copies the pointed-to value
auto b = a;
std::cout << std::boolalpha;
std::cout << (b == a) << "\n"; // prints false
std::cout << (b.get() == a.get()) << "\n"; // prints false

struct no_copy_impl : public interface {
	std::unique_ptr<int> value;
	int foo() override { return *value; }
};

// Compile error; copy-constructible type is required
/* my_pointer_type = no_copy_impl{}; */

// Define another pointer type that can not be copied
// and never allocates;
// Equivalent to sboptr::basic_sbo_ptr<T, size, sboptr::movable>
using other_pointer_type = sboptr::unique_no_alloc_sbo_ptr<interface, sizeof(void*) * 2>;

other_pointer_type c = no_copy_impl{std::make_unique<int>(20)};

// Compile error - no copy constructor
/* auto d = c; */

// Compile error - too big to fit into the small buffer and heap allocations
// are disabled
/* other_pointer_type e = big_impl{}; */

c.reset();

// In-place construction
c.emplace<small_impl>(20);
other_pointer_type f{std::in_place_type<small_impl>, 30};

How is this different from available type erasure libraries?

There are many mature type erasure libraries which provide configurable small buffer storage. The advantage of sboptr is that it is (almost) a drop-in replacement for standard smart pointer types, allowing the use of normal virtual interfaces/base classes. The disadvantage is the larger size: 2 pointers + sbo size. This is because it needs to store a separate vtable pointer for special member functions (and your implementation classes will also contain the built-in base class vtable pointer).

sboptr's People

Contributors

miso1289 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sboptr's Issues

readability

Suggestion:
replace class definition with many booleans with more readable types

using my_pointer_type = sboptr::basic_sbo_ptr<
	interface,
	/* size of small buffer */ sizeof(void*) * 2,
	/* enable move */ true,
	/* enable copy */ true,
	/* enable heap storage */ true>;

replace with:

using my_pointer_type = sboptr::basic_sbo_ptr<
	interface,
	/* size of small buffer */ sizeof(void*) * 2,
	sboptr::movable, // sboptr::not_movable
	sboptr::copyable,
	sboptr::storage_heap; // sboptr::storage_stack

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.