wavelength 1.0.0
Loading...
Searching...
No Matches
factory.h
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4
5#include "os/binstream.h"
6
15template <typename Base>
16class Factory {
17public:
24 public:
31 virtual ~FactoryUnitBase() {
32 }
33
40 virtual Base *Create(BinStream &stream) = 0;
41 };
42
50 template <typename T>
52 public:
83 ~FactoryUnit() override = default;
84
115 Base *Create(BinStream &stream) override {
116 return new T(stream);
117 }
118 };
119
129 static std::map<int, FactoryUnitBase *> &GetUnits() {
130 static std::map<int, FactoryUnitBase *> units;
131 return units;
132 }
133
140 template <typename T>
141 static void Register(int nId) {
142 std::map<int, FactoryUnitBase *> &units = GetUnits();
143 FactoryUnitBase *pUnit = new FactoryUnit<T>;
144 units[nId] = pUnit;
145 }
146};
Stream of raw bytes, the base of the memory, file, and network streams.
Definition binstream.h:22
Builder of one class.
Definition factory.h:23
virtual ~FactoryUnitBase()
Release the builder.
Definition factory.h:31
virtual Base * Create(BinStream &stream)=0
Construct an object of the class from a stream.
Builder of the class T.
Definition factory.h:51
Base * Create(BinStream &stream) override
Construct a T from a stream.
Definition factory.h:115
~FactoryUnit() override=default
Release the builder.
Registry of the classes derived from a base that a stream can rebuild, by identifier.
Definition factory.h:16
static void Register(int nId)
Register a builder of the class T under an identifier.
Definition factory.h:141
static std::map< int, FactoryUnitBase * > & GetUnits()
Report the builders by identifier, constructing the registry on first use.
Definition factory.h:129