TensorFlow Serving C++ API Documentation
Public Member Functions | List of all members
tensorflow::serving::AnyPtr Class Reference

#include <any_ptr.h>

Public Member Functions

 AnyPtr ()
 AnyPtr is void and null by default.
 
 AnyPtr (std::nullptr_t)
 Implicit construction from nullptr.
 
template<typename T >
 AnyPtr (T *ptr)
 Construct from a pointer to any type.
 
template<typename T >
T * get () const
 Accessor for the underlying pointer if it is of type T, otherwise null.
 

Detailed Description

A (sort of) type-safe void*. Appears as null if a caller attempts to use it as the wrong type.

Example use:

// A function that returns an AnyPtr: AnyPtr StringOrInt() { if (use_string) { return AnyPtr(&some_string); } else { return AnyPtr(&some_int); } }

// Use an AnyPtr at the correct type: AnyPtr ptr = StringOrInt(); if (ptr.get<int>() != nullptr) { DoSomethingWithInt(*ptr.get<int>()); } else if (ptr.get<string>() != nullptr) { DoSomethingWithString(*ptr.get<string>()); } else { // Handle error. }

Typical best practice for this class is to use it when two disjoint pieces of code must agree on type, but intermediate code is type agnostic. Large chains of conditionals that handle a multitude of types is discouraged as an anti-pattern.

Note that this will appear null even if T is somewhere on the underlying type's inheritance hierarchy, if you must use the object at some other type you must do so explicitly when constructing an AnyPtr, like so:

SomeObject object; AnyPtr any_ptr(static_cast<SomeInterface*>(&object)); SomeInterface* interface = any_ptr.get<SomeInterface>();

This class is a value type; It can be copied or assigned. It performs no internal allocations and should be relatively cheap to copy or return by value.

Definition at line 65 of file any_ptr.h.


The documentation for this class was generated from the following file: