HoverRace  2.0
DllObjectFactory.h
Go to the documentation of this file.
1 
2 // DllObjectFactory.h
3 //
4 // Copyright (c) 1995-1998 - Richard Langlois and Grokksoft Inc.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 //
22 
23 // This file constais fonctions for creating objects from
24 // two integer numbers. One of this number identify the
25 // dll containing the object to create and the other contain
26 // factory class
27 
28 #pragma once
29 
30 #include "../Parcel/ObjStream.h"
31 
32 #include "MR_Types.h"
33 
34 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
35 # ifdef MR_ENGINE
36 # define MR_DllDeclare __declspec( dllexport )
37 # else
38 # define MR_DllDeclare __declspec( dllimport )
39 # endif
40 #else
41 # define MR_DllDeclare
42 #endif
43 
44 namespace HoverRace {
45  namespace ObjFacTools {
46  class ResourceLib;
47  }
48 }
49 
50 namespace HoverRace {
51 namespace Util {
52 
55 {
58 
59  void Serialize(Parcel::ObjStream &pArchive);
60 
61  constexpr bool operator==(const ObjectFromFactoryId &pId) const noexcept
62  {
63  return
64  mDllId == pId.mDllId &&
65  mClassId == pId.mClassId;
66  }
67 };
68 
69 class ObjectFromFactory;
70 
71 namespace DllObjectFactory {
72 
74 MR_DllDeclare void Init() noexcept;
75 
77 MR_DllDeclare void Clean() noexcept;
78 
80 MR_DllDeclare std::shared_ptr<ObjectFromFactory> CreateObject(
81  const ObjectFromFactoryId &pId);
82 
83 } // namespace DllObjectFactory
84 
87 {
88 private:
90 
91 public:
92  ObjectFromFactory(const ObjectFromFactoryId &pId) noexcept : mId(pId) { }
93  virtual ~ObjectFromFactory() { }
94 
95  const ObjectFromFactoryId &GetTypeId() const noexcept { return mId; }
96 
97 private:
98  static void ThrowUnexpected(const ObjectFromFactoryId &oid);
99 
100 public:
101  // Serialisation functions
102  //
103  // Warning this module do not support multiple references to objects
104  // or looped structures
105 
119  template<class T>
120  static void SerializeShared(Parcel::ObjStream &archive,
121  typename std::enable_if<
122  std::is_base_of<ObjectFromFactory, T>::value,
123  std::shared_ptr<T>>::type &obj)
124  {
125  ObjectFromFactoryId oid = { 0, 0 };
126 
127  if (archive.IsWriting()) {
128  if (obj) {
129  oid = obj->mId;
130  }
131  oid.Serialize(archive);
132  if (obj) {
133  obj->Serialize(archive);
134  }
135  }
136  else {
137  oid.Serialize(archive);
138 
139  if (oid.mDllId == 0) {
140  obj.reset();
141  }
142  else {
143  auto dynObj = std::dynamic_pointer_cast<T>(
145  if (!dynObj) {
146  obj.reset();
147  // The ID is probably corrupt or unsupported, so we can't
148  // rely on the rest of the stream (we don't know where the
149  // next object starts!).
150  ThrowUnexpected(oid);
151  }
152  obj = std::move(dynObj);
153  obj->Serialize(archive);
154  }
155  }
156  }
157 
158  virtual void Serialize(Parcel::ObjStream &archive) = 0;
159 };
160 
161 } // namespace Util
162 } // namespace HoverRace
163 
164 #undef MR_DllDeclare
Unique identifier for a Factory Object.
Definition: DllObjectFactory.h:54
virtual ~ObjectFromFactory()
Definition: DllObjectFactory.h:93
constexpr bool operator==(const ObjectFromFactoryId &pId) const noexcept
Definition: DllObjectFactory.h:61
ObjectFromFactoryId mId
Definition: DllObjectFactory.h:89
uint16_t MR_UInt16
Definition: MR_Types.h:42
MR_UInt16 mDllId
Definition: DllObjectFactory.h:56
MR_DllDeclare void Clean() noexcept
Must be called at the end of the program.
Definition: DllObjectFactory.cpp:63
bool IsWriting() const
Definition: ObjStream.h:71
MR_DllDeclare std::shared_ptr< ObjectFromFactory > CreateObject(const ObjectFromFactoryId &pId)
Fast Object Creation function.
Definition: DllObjectFactory.cpp:68
const ObjectFromFactoryId & GetTypeId() const noexcept
Definition: DllObjectFactory.h:95
MR_DllDeclare void Init() noexcept
Must be called at the beginning of the program.
Definition: DllObjectFactory.cpp:58
Base class for parcel serializers.
Definition: ObjStream.h:58
MR_UInt16 mClassId
Definition: DllObjectFactory.h:57
void Serialize(Parcel::ObjStream &pArchive)
Definition: DllObjectFactory.cpp:83
static void SerializeShared(Parcel::ObjStream &archive, typename std::enable_if< std::is_base_of< ObjectFromFactory, T >::value, std::shared_ptr< T >>::type &obj)
Serialize a shared pointer to a serializable object.
Definition: DllObjectFactory.h:120
#define MR_DllDeclare
Definition: DllObjectFactory.h:41
Base class for object created with a Dll Factory.
Definition: DllObjectFactory.h:86
ObjectFromFactory(const ObjectFromFactoryId &pId) noexcept
Definition: DllObjectFactory.h:92
Definition: Announcement.h:24