Git Product home page Git Product logo

state_ptr's Introduction

state_ptr

Linux & macOS Windows
travisCI appveyor

A C++ pointer type with storage space for information filling.

Because of data alignment pointers that are used to iterate over arrays of data have states that cannot be reached. For example for arrays of element type uint64_t the three least significant bits of the pointer are always the same (zero).

This class provides a convenient way to use this otherwise wasted space to store information.

Release Notes

0.3.0

  • Fixed critical compile-time bug in operator* implementation. (Thanks goes to fkutzner)
  • Overloads for operator* and operator-> respect const now
  • Internal code clean-up (thanks clang-tidy!)
  • Added some new unit tests for state_ptr.

0.2.0

  • Added comprehensive doxygen-ish documentation to all public parts of the library.
  • Improved the internal representation of the pointer and the state that no longer is based on bitfields.
  • It is now possible to use a user-defined type as state representable.
  • Users can (and should) set a state value limit on state_ptr instances.
  • Added constructors and comparisons for nullptr_t.
  • Added bool conversion overload.
  • Moved state_ptr.hpp from root directory to include/putl/state_ptr.hpp for more convenience.
  • Lots of code refactoring and clean-up.
  • Devel
    • Made the project a proper CMake project.
    • Added test suite for state_ptr and log2.

0.1.0

  • Initial release.

Example

using namespace putl;

struct Foo{ int baz; };
enum class Bar{ A, B, C };

Foo foo{42};
state_ptr<Foo, Bar> p{&foo, Bar::A};

// Stats for an exemplary 64-bit architecture
// ------------------------------------------
static_assert(state_ptr<Foo, Bar>::ptr_bits   == 61); // 61 bits for the pointer
static_assert(state_ptr<Foo, Bar>::state_bits == 3 ); // 3 bits for the state
static_assert(state_ptr<Foo, Bar>::threshold  == 7 ); // this is the greatest number that can be stored in the pointer's state

assert(p.get_state() == Bar::A);
assert(p.get_ptr()   == std::address_of(foo));

// Set state of `p` to `Bar::B`
// ----------------------------

p.set_state(Bar::B);
assert(p.get_state(), Bar::B);

// Use the pointed-to value
// ------------------------

foo->baz = 5; // sets foo's baz to `5`
assert(*foo == 5);

// Try setting the state of `p` to a value that is out-of-bounds results in a panic
// --------------------------------------------------------------------------------

p.set_state(static_cast<Bar>(42)); /* !! panic !! */

I hope this small pointer utility library is useful to you.

state_ptr's People

Contributors

fkutzner avatar robbepop avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

fkutzner

state_ptr's Issues

Dereferencing state_ptr seems to be broken

As of commit 894d894, dereferencing state_ptr instances seems to be broken.

Example:

int testData = 1;
putl::state_ptr<int> underTest{&testData, 0};
int target = *underTest;

When compiling with Apple clang-802.0.42, I get the following error:

[...]/state_ptr/include/putl/state_ptr.hpp:263:10: error: reinterpret_cast from rvalue to reference type
      'reference_type' (aka 'int &')
                return reinterpret_cast<reference_type>(get_ptr());
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[...]/state_ptr/testsrc/state_ptr_tests.cpp:119:18: note: in instantiation of member function
      'putl::state_ptr<int, unsigned long, 2>::operator*' requested here
    int target = *underTest;

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.