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 Any Ptr

From Leeroopedia
Revision as of 13:52, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tensorflow_Serving_Any_Ptr.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Utility, Type System
Last Updated 2026-02-13 00:00 GMT

Overview

A lightweight, type-safe void pointer wrapper that returns nullptr when accessed as the wrong type, along with an owning variant (UniqueAnyPtr) that manages object lifetime.

Description

AnyPtr is a value type that wraps a void pointer along with a type identifier. It uses a static-variable-based FastTypeId<T>() function to generate unique per-type identifiers at zero runtime cost (each type gets a unique address from a static int variable). When constructed from a T*, the type id is recorded; when accessed via get<T>(), the stored type id is compared with FastTypeId<T>(), and nullptr is returned on mismatch. This provides type safety without RTTI. The class handles const-correctness by treating const T and T as different types. UniqueAnyPtr extends this concept with ownership semantics, storing a type-erased deleter (a raw function pointer for minimal overhead) alongside the AnyPtr. It supports move semantics but not copying, and calls the correct typed delete upon destruction.

Usage

Use AnyPtr when two disjoint pieces of code must agree on a type but intermediate code is type-agnostic, such as storing heterogeneous dependencies in UniquePtrWithDeps. Avoid using it for large chains of type conditionals.

Code Reference

Source Location

Signature

class AnyPtr {
 public:
  AnyPtr();
  AnyPtr(std::nullptr_t);
  template <typename T> AnyPtr(T* ptr);
  template <typename T> T* get() const;
};

class UniqueAnyPtr {
 public:
  UniqueAnyPtr();
  UniqueAnyPtr(std::nullptr_t);
  template <typename T> explicit UniqueAnyPtr(std::unique_ptr<T> ptr);
  ~UniqueAnyPtr();
  template <typename T> T* get() const;
  const AnyPtr& as_any_ptr() const;
};

Import

#include "tensorflow_serving/util/any_ptr.h"

I/O Contract

Inputs

Name Type Required Description
ptr T* No A pointer to any type; nullptr creates a null AnyPtr

Outputs

Name Type Description
get<T>() T* The underlying pointer if it matches type T, otherwise nullptr

Usage Examples

Type-Safe Polymorphic Access

AnyPtr StringOrInt(bool use_string) {
  static string some_string = "hello";
  static int some_int = 42;
  if (use_string) {
    return AnyPtr(&some_string);
  } else {
    return AnyPtr(&some_int);
  }
}

AnyPtr ptr = StringOrInt(true);
if (ptr.get<string>() != nullptr) {
  DoSomethingWithString(*ptr.get<string>());
} else if (ptr.get<int>() != nullptr) {
  DoSomethingWithInt(*ptr.get<int>());
}

Owning UniqueAnyPtr

UniqueAnyPtr owned(std::make_unique<MyType>(args));
MyType* raw = owned.get<MyType>();  // non-null if type matches
// Automatically deleted when owned goes out of scope

Related Pages

Page Connections

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