Template Struct Factory

Struct Documentation

template<typename T, typename Allocator>
struct Factory

Allocates one contiguous batch and constructs each T separately.

Each returned handle destroys its object; the shared allocation owner calls allocator deallocate(original, count) after the final handle is gone.

Public Types

using AAloc = std::allocator_traits<Allocator>

Allocator traits used for allocation, per-object construction/destruction and final original-range deallocation.

Public Static Functions

template<typename ...Args>
static std::vector<std::shared_ptr<T>> create(size_t N, Args... args)

Allocate one contiguous batch and construct independent per-object handles.

Parameters:
  • N – Number of objects; zero returns an empty vector without allocation.

  • args – Stored arguments reused as lvalues for each object’s constructor.

Returns:

Separate object owners retaining a shared allocation owner; only the last one deallocates the original pointer/count through its copied allocator. A later construction/handle allocation failure destroys completed objects and releases the backing batch. Final deleters are noexcept: throwing destroy or deallocate operations terminate. Object access itself is unsynchronized.

template<typename ...Args>
static void construct(T *p, size_t N, Args... args)

Construct N objects in caller-owned contiguous storage, rolling back the completed prefix on failure.

Parameters:
  • p – Aligned writable storage for N unconstructed T objects, kept valid through rollback.

  • N – Number of objects; zero does nothing.

  • args – Stored arguments reused as lvalues for every constructor. Constructor exceptions propagate after reverse destruction of the completed prefix. This does not allocate, track slots or return storage; callers exclude overlapping access and supply nonthrowing rollback destruction.

static void destroy(T *p, size_t N = 1)

Destroy a contiguous live range without returning its backing allocation.

Parameters:
  • p – First valid live object, borrowed through the call.

  • N – Object count, one by default; zero does nothing. Uses a default allocator instance and destroys in increasing address order; a destructor failure propagates with earlier destructions already applied. Callers synchronize access and ensure every selected slot is live.