Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Serving Unique Ptr With Deps

From Leeroopedia
Knowledge Sources
Domains Memory Management, Utility
Last Updated 2026-02-13 00:00 GMT

Overview

A unique-pointer-like container that manages the lifecycle of a primary owned object along with its type-erased dependencies, destroying them in reverse order of addition.

Description

UniquePtrWithDeps<T> holds a pointer to a primary object of type T together with a vector of UniqueAnyPtr dependencies. Dependencies are added via AddDependency<X>(), which takes ownership of a unique_ptr<X>, stores it in the internal deleters vector, and returns a raw pointer for continued use. The primary owned object is itself stored as a dependency (via SetOwned()). On destruction, the deleters vector is popped in reverse order (last added first destroyed), ensuring proper destruction ordering where the primary object is destroyed before its prerequisites. This is particularly useful for returning ownership of an object to client code without exposing the dependencies, while guaranteeing that everything is cleaned up properly.

Usage

Use this when creating an object that depends on other objects whose lifetimes must be tied together, such as a servable that depends on a loaded model and its associated session or resources.

Code Reference

Source Location

  • Repository: Tensorflow_Serving
  • File: tensorflow_serving/util/unique_ptr_with_deps.h
  • Lines: 1-84

Signature

template <typename T>
class UniquePtrWithDeps {
 public:
  UniquePtrWithDeps();
  explicit UniquePtrWithDeps(std::unique_ptr<T> object);
  explicit UniquePtrWithDeps(T* owned_object);
  ~UniquePtrWithDeps();

  template <typename X>
  X* AddDependency(std::unique_ptr<X> dependency);

  void SetOwned(std::unique_ptr<T> object);

  T* get() const;
  const T& operator*() const;
  T* operator->() const;
};

Import

#include "tensorflow_serving/util/unique_ptr_with_deps.h"

I/O Contract

Inputs

Name Type Required Description
object std::unique_ptr<T> Yes The primary owned object
dependency std::unique_ptr<X> No Additional dependencies to be managed alongside the primary object

Outputs

Name Type Description
get() T* Raw pointer to the primary owned object
AddDependency() X* Raw pointer to the added dependency for continued use

Usage Examples

Object with Dependencies

UniquePtrWithDeps<MyServable> servable_with_deps;

// Add dependencies first
auto* session = servable_with_deps.AddDependency(
    std::make_unique<Session>(session_options));
auto* model = servable_with_deps.AddDependency(
    std::make_unique<Model>(model_path));

// Set the primary object last
servable_with_deps.SetOwned(
    std::make_unique<MyServable>(session, model));

// Use normally
servable_with_deps->Serve(request);

// On destruction: MyServable destroyed first, then Model, then Session

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment