Implementation:Tensorflow Serving Class Registration
| Knowledge Sources | |
|---|---|
| Domains | Utility, Design Pattern |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
A macro-based class registration framework that maps protobuf config message types to factory methods for creating subclass instances of an abstract base class.
Description
The class registration system provides a compile-time mechanism for registering subclasses of an abstract base class, where each subclass is associated with a specific protobuf configuration message type. The system consists of three core components: AbstractClassRegistrationFactory (a pure virtual factory interface), ClassRegistrationFactory (a templated concrete factory that dispatches to a static Create method on the registered class), and ClassRegistry (a static map from protobuf message type names to factory instances). Registration is performed via the DEFINE_CLASS_REGISTRY and REGISTER_CLASS macros, which use __COUNTER__ to generate unique static variable names. The registry supports both direct config proto creation and creation from protobuf::Any messages. The factory signature can be extended with additional parameters beyond the config proto. The global factory map is protected by a mutex for thread-safe registration and lookup.
Usage
Use this when you need a plugin-like architecture where new implementations of a base class can be registered and instantiated based on protobuf configuration messages, such as registering different SourceAdapter, StoragePath, or Loader implementations in the serving system.
Code Reference
Source Location
- Repository: Tensorflow_Serving
- File:
tensorflow_serving/util/class_registration.h - Lines: 1-367
Signature
// Define a registry
DEFINE_CLASS_REGISTRY(RegistryName, BaseClass, ...AdditionalFactoryArgs);
// Register a class with the registry
REGISTER_CLASS(RegistryName, BaseClass, ClassCreator, ConfigProto, ...AdditionalFactoryArgs);
// Create from a config proto
static Status ClassRegistry::Create(const protobuf::Message& config,
AdditionalFactoryArgs... args,
std::unique_ptr<BaseClass>* result);
// Create from an Any proto
static Status ClassRegistry::CreateFromAny(const google::protobuf::Any& any_config,
AdditionalFactoryArgs... args,
std::unique_ptr<BaseClass>* result);
Import
#include "tensorflow_serving/util/class_registration.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | protobuf::Message& |
Yes | Configuration proto that determines which subclass to instantiate |
| any_config | google::protobuf::Any& |
No | An Any-wrapped config proto (alternative to direct config) |
| args | AdditionalFactoryArgs... |
No | Additional arguments forwarded to the Create method |
| result | std::unique_ptr<BaseClass>* |
Yes | Output pointer that receives the created instance |
Outputs
| Name | Type | Description |
|---|---|---|
| Status | tensorflow::Status |
OK on success, or InvalidArgument/Internal on failure |
| result | std::unique_ptr<BaseClass> |
The created subclass instance (via output parameter) |
Usage Examples
Basic Registry Definition and Usage
// Define the base class
class MyBaseClass {
public:
virtual ~MyBaseClass() = default;
};
// Define the registry
DEFINE_CLASS_REGISTRY(MyBaseClassRegistry, MyBaseClass);
// Define the registration macro
#define REGISTER_MY_BASE_CLASS(ClassCreator, ConfigProto) \
REGISTER_CLASS(MyBaseClassRegistry, MyBaseClass, ClassCreator, ConfigProto);
// Implement and register a subclass
class OneClass : public MyBaseClass {
public:
static Status Create(const OneConfigProto& config,
std::unique_ptr<MyBaseClass>* result) {
result->reset(new OneClass());
return Status::OK;
}
};
REGISTER_MY_BASE_CLASS(OneClass, OneConfigProto);
// Create an instance via the registry
OneConfigProto config = ...;
std::unique_ptr<MyBaseClass> instance;
TF_RETURN_IF_ERROR(MyBaseClassRegistry::Create(config, &instance));
Creation from Any Proto
google::protobuf::Any any_config;
any_config.PackFrom(one_config_proto);
std::unique_ptr<MyBaseClass> instance;
TF_RETURN_IF_ERROR(MyBaseClassRegistry::CreateFromAny(any_config, &instance));