WSF
USMTF_Factory.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2019 Infoscitex, a DCS Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11
12#ifndef USMTFFACTORY_HPP
13#define USMTFFACTORY_HPP
14
15#include "usmtf_export.h"
16
17#include <functional>
18#include <memory>
19#include <unordered_map>
20#include <vector>
21
22#include "Message.hpp"
23#include "UtMemory.hpp"
24
25namespace usmtf
26{
36
38template<class T, class U>
39class USMTF_Factory
40{
41public:
43 using ConstructorCb = std::function<std::unique_ptr<T>(const std::string& aType, U aContents)>;
44
46 static USMTF_Factory<T, U>* GetFactory();
47
51 template<class V>
52 const V* CastIfRegistered(const T& aEntity) noexcept;
53
56 template<class V>
57 static std::unique_ptr<V> Construct(const std::string& aType, U&& aContents);
58
61 void RegisterEntity(const std::string& aType, ConstructorCb aCb) noexcept;
62
64 void UnregisterEntity(const std::string& aType) noexcept;
65
67 std::unique_ptr<T> CreateInstance(const std::string& aType, U aContents);
68
69private:
70 USMTF_Factory<T, U>() = default;
71 using CallbackMap = std::unordered_map<std::string, ConstructorCb>;
72
73 // Map type to constructor function.
74 CallbackMap mRegisteredEntities;
75 static std::unique_ptr<USMTF_Factory<T, U>> mFactoryInstance;
76};
77
82
84#if defined(SWDEV_ALL_USE_DLL)
86template class USMTF_EXPORT USMTF_Factory<Set, std::vector<Field>>;
87
88#endif
89
90template<class T, class U>
91USMTF_Factory<T, U>* USMTF_Factory<T, U>::GetFactory()
92{
93 if (!mFactoryInstance)
94 {
95 mFactoryInstance = std::unique_ptr<USMTF_Factory<T, U>>(new USMTF_Factory<T, U>);
96 }
97
98 return mFactoryInstance.get();
99}
100
101template<class T, class U>
102template<class V>
103const V* USMTF_Factory<T, U>::CastIfRegistered(const T& aEntity) noexcept
104{
105 typename CallbackMap::iterator it = mRegisteredEntities.find(aEntity.GetType());
106 if (it != mRegisteredEntities.end())
107 {
108 return dynamic_cast<const V*>(&aEntity);
109 }
110 return nullptr; // cast not valid, type not registered so a concrete class could not have been instantiated for the type.
111}
112
113template<class T, class U>
114template<class V>
115std::unique_ptr<V> USMTF_Factory<T, U>::Construct(const std::string& aType, U&& aContents)
116{
117 return ut::make_unique<V>(aType, std::move(aContents));
118}
119
120template<class T, class U>
121void USMTF_Factory<T, U>::RegisterEntity(const std::string& aType,
122 typename USMTF_Factory<T, U>::ConstructorCb aConstructorCb) noexcept
123{
124 mRegisteredEntities[aType] = aConstructorCb;
125}
126
127template<class T, class U>
128void USMTF_Factory<T, U>::UnregisterEntity(const std::string& aType) noexcept
129{
130 mRegisteredEntities.erase(aType);
131}
132
133template<class T, class U>
134std::unique_ptr<T> USMTF_Factory<T, U>::CreateInstance(const std::string& aType, U aContents)
135{
136 typename CallbackMap::iterator it = mRegisteredEntities.find(aType);
137 if (it != mRegisteredEntities.end())
138 {
139 return (it->second)(aType, std::move(aContents));
140 }
141 return ut::make_unique<T>(aType, std::move(aContents));
142}
143
144template<class T, class U>
145std::unique_ptr<USMTF_Factory<T, U>> USMTF_Factory<T, U>::mFactoryInstance;
146} // namespace usmtf
147#endif
Uses of this is highly documented in pretty much every tests Suite.
Definition USMTF_Factory.hpp:40
static std::unique_ptr< V > Construct(const std::string &aType, U &&aContents)
Definition USMTF_Factory.hpp:115
std::function< std::unique_ptr< T >(const std::string &aType, U aContents)> ConstructorCb
Constructor Callback specifies the expected format of the function needed to instantiate objects.
Definition USMTF_Factory.hpp:43
std::unique_ptr< T > CreateInstance(const std::string &aType, U aContents)
Instantiate an instance of the Set or Message. Used Only by parser.
Definition USMTF_Factory.hpp:134
static USMTF_Factory< T, U > * GetFactory()
Return the singleton instance of the factory.
Definition USMTF_Factory.hpp:91
void RegisterEntity(const std::string &aType, ConstructorCb aCb) noexcept
Definition USMTF_Factory.hpp:121
const V * CastIfRegistered(const T &aEntity) noexcept
Definition USMTF_Factory.hpp:103
void UnregisterEntity(const std::string &aType) noexcept
Unregister a previously registers derived type.
Definition USMTF_Factory.hpp:128
Definition Acmid.cpp:17
USMTF_Factory< Message, std::vector< std::unique_ptr< Set > > > MessageFactory
Definition USMTF_Factory.hpp:80
USMTF_Factory< Set, std::vector< Field > > SetFactory
Definition USMTF_Factory.hpp:81
Copyrights Multiple, All Rights Reserved