Coder Social home page Coder Social logo

Comments (3)

tarqd avatar tarqd commented on June 13, 2024

Could also use a scope guard, no need to hit the heap for that (plus there's one in this library!). Basically the equivalent of:

static void clone (data_type const& source, data_type& data) {
    struct scope_guard { 
        allocator_type& m_alloc; 
        typename allocator_traits::pointer m_ptr;
        bool dismissed = false; 
         void dismiss() { dismissed = true; }
        ~scope_guard() { if (!dismissed) allocator_traits::deallocate(m_alloc, m_ptr); }
    };
    allocator_type alloc { };
    auto const& value = *static_cast<Type* const>(source);
    auto pointer = allocator_traits::allocate(alloc, 1);
    scope_guard guard { alloc, pointer };
    allocator_traits::construct(alloc, pointer, value);
    guard.dismiss(); // if an exception throws, the memory will be deallocated in the guard dtor
    data = pointer;
  }

from core.

EricWF avatar EricWF commented on June 13, 2024

Seems like a very reasonable way to do it without having to add a new class. What do you by "no need to hit the heap"?

from core.

tarqd avatar tarqd commented on June 13, 2024

Whoops for some reason I thought unique_ptr would be making a heap allocation but obviously it doesn't. It's been a long day haha. It does fit the use-case for scope_guard very nicely though (basically unique_ptr is acting like one).

Essentially both of our examples are doing the same thing, but with the scope_guard in this library (after it adds dismissal support) it'd be

static void clone (data_type const& source, data_type& data) {
    allocator_type alloc { };
    auto const& value = *static_cast<Type* const>(source);
    auto pointer = allocator_traits::allocate(alloc, 1);
    auto guard = make_scope_guard([&] { allocator_traits::deallocate(alloc, pointer); });
    allocator_traits::construct(alloc, pointer, value);
    guard.dismiss(); // if an exception throws, the memory will be deallocated in the guard dtor
    data = pointer;
  }

from core.

Related Issues (20)

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.