acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
_impl_factory.h
1//
2// Created by camilo on 23/12/2022 02:00 <3ThomasBorregaardSorensen!!
3//
4#pragma once
5
6
7namespace factory
8{
9
10
11 template < typename TYPE, typename ORIGIN_TYPE >
13 {
14
15 {
16
17 critical_section_lock lock(&m_criticalsection);
18
19 if (m_pfree)
20 {
21
22 auto pNew = m_pfree;
23
24 m_pfree = pNew->m_pnext;
25
26 pNew->reuse();
27
28 return pNew;
29
30 }
31
32 }
33
34 return factory_item < TYPE, ORIGIN_TYPE >::__callallocateø();
35
36 }
37
38
39 template < typename TYPE, typename ORIGIN_TYPE >
40 inline void reusable_factory_item < TYPE, ORIGIN_TYPE >::return_back(ORIGIN_TYPE * p)
41 {
42
43 critical_section_lock lock(&m_criticalsection);
44
45 p->m_pnext = m_pfree;
46
47 m_pfree = p;
48
49 }
50
51
52 template < typename ORIGIN_TYPE >
53 inline ::pointer<ORIGIN_TYPE>factory::__call__create(::particle * pparticle)
54 {
55
56 auto pfactoryinterface = get_factory_item < ORIGIN_TYPE >();
57
58 if (!pfactoryinterface)
59 {
60
61 throw_exception(error_no_factory);
62
63 }
64
65 auto p = pfactoryinterface->__call__create_particle();
66
67 p->initialize(pparticle);
68
69 return p;
70
71 }
72
73
74 template < typename TYPE, typename ORIGIN_TYPE>
75 inline pointer< ::factory::factory_item_base < ORIGIN_TYPE > > factory::add_factory_item()
76 {
77
78 critical_section_lock lock(&m_criticalsection);
79
80 auto pfactory = allocateø ::factory::factory_item< TYPE, ORIGIN_TYPE > ();
81
82 auto typeindex = ::type_index < ORIGIN_TYPE >();
83
84 m_mapByTypeIndex[typeindex] = pfactory;
85
86 auto strTypeName = ::type_name < ORIGIN_TYPE >();
87
88 m_mapByTypeName[strTypeName] = pfactory;
89
90 return pfactory;
91
92 }
93
94
95 template < typename ORIGIN_TYPE >
96 inline void factory::__call__construct(::particle * pparticleInitializer, ::pointer < ORIGIN_TYPE > & p)
97 {
98
99 __call__raw_construct(p);
100
101 p->initialize(pparticleInitializer);
102
103 }
104
105
106 template < typename ORIGIN_TYPE >
107 inline void factory::__call__raw_construct(::pointer<ORIGIN_TYPE> & p)
108 {
109
110//#if REFERENCING_DEBUGGING
111//
112// ::allocator::add_referer(REFERENCING_DEBUGGING_ARGUMENTS);
113//
114//#endif
115//
116 auto pfactoryitem = get_factory_item < ORIGIN_TYPE >();
117
118 if (!pfactoryitem)
119 {
120
121 ::string strMessage;
122
123 strMessage = "Factory hasn't creator for type \"" + ::platform::type(::type<ORIGIN_TYPE>()).name() + "\"";
124
125 warningf(strMessage);
126
127 throw_exception(error_no_factory, strMessage);
128
129 }
130
131 auto pparticle = ::transfer(pfactoryitem->__call__create_particle());
132
133 if (!pparticle)
134 {
135
136 ::string strMessage;
137
138 strMessage = "Couldn't __call__create_particle for type \"" + ::platform::type(::type<ORIGIN_TYPE>()).name() + "\"";
139
140 warningf(strMessage);
141
142 throw_exception(error_no_memory, strMessage);
143
144 }
145
146 p = pparticle;
147
148 if (!p)
149 {
150
151 ::string strMessage;
152
153 strMessage = "Created element is not of the base type \"" + ::platform::type(::type<ORIGIN_TYPE>()).name() + "\"";
154
155 warningf(strMessage);
156
157 throw_exception(error_wrong_type, strMessage);
158
159 }
160
161 }
162
163
164
165
166 template < typename ORIGIN_TYPE >
167 inline ::factory::factory_item_interface * factory::get_factory_item() const
168 {
169
170 auto typeindex = ::std::type_index(typeid(ORIGIN_TYPE));
171
172 return _find_factory_item_by_type_index(typeindex);
173
174 }
175
176
177 //template < typename TYPE >
178 //inline stream & load_object(stream & stream, ::pointer<TYPE>& p)
179 //{
180
181 // string strText;
182
183 // stream >> strText;
184
185 // if (strText.is_empty() || strText.case_insensitive_begins_eat("factoryless://"))
186 // {
187
188 // if (p && ::platform::type(p).name()) == strText
189 // {
190
191 // informationf("loading into existing matter of same class type (1)");
192
193 // }
194 // else
195 // {
196
197 // p.defer_create_new();
198
199 // if (!p)
200 // {
201
202 // informationf("defer_new failed (1.1)");
203
204 // stream.set_fail_bit();
205
206 // }
207 // else if (::platform::type(p).name()) != strText
208 // {
209
210 // informationf("allocated matter type is different from streamed matter type (1.2)");
211
212 // stream.set_fail_bit();
213
214 // }
215
216 // }
217
218 // }
219 // else
220 // {
221
222 // auto atom = stream.text_to_factory_id(strText);
223
224 // if (p && atom == ::platform::type(p).name())
225 // {
226
227 // informationf("loading into existing matter of same class type (2)");
228
229 // }
230 // else
231 // {
232
233 // p = stream.create_object_from_text(strText);
234
235 // if (!p)
236 // {
237
238 // informationf("stream::alloc_object_from_text failed (2.1)");
239
240 // }
241 // else if (::platform::type(p).name()) != atom.to_string()
242 // {
243
244 // informationf("allocated matter type is different from streamed matter type (2.2)");
245
246 // stream.set_fail_bit();
247
248 // }
249
250 // }
251
252 // }
253
254 // if (!stream.fail())
255 // {
256
257 // if (p->has(e_object_factory))
258 // {
259
260 // p->read(stream);
261
262 // }
263 // else
264 // {
265
266 // stream >> *p;
267
268 // }
269
270 // }
271
272 // return stream;
273
274 //}
275
276
277} // namespace factory
278
279
280//#include "_factory.h"
281
282
283namespace factory
284{
285
286
287 template < typename BASE_TYPE >
288 inline bool factory::__call__defer_construct(::particle * pparticle, ::pointer<BASE_TYPE> & ptype)
289 {
290
291 if (ptype)
292 {
293
294#if REFERENCING_DEBUGGING
295
296 ::allocator::defer_erase_referer();
297
298#endif
299
300 return false;
301
302 }
303
304 pparticle->__call__construct(ptype, this);
305
306 return true;
307
308 }
309
310
311} // namespace factory
312
313
314template < typename TYPE >
315inline void particle__call__raw_construct2( ::pointer<TYPE> & p, ::factory::factory * pfactory COMMA_REFERENCING_DEBUGGING_PARAMETERS_DECLARATION)
316{
317
318#if REFERENCING_DEBUGGING
319
320 ::allocator::push_referer(REFERENCING_DEBUGGING_ARGUMENTS);
321
322#endif
323
324 __call_raw_construct(p, pfactory);
325
326}
327
328
329template < typename TYPE >
330inline void particle::__call__raw_construct(::pointer<TYPE>&p, ::factory::factory * pfactory COMMA_REFERENCING_DEBUGGING_PARAMETERS_DEFINITION)
331{
332
333
334 if (::is_null(pfactory))
335 {
336
337 pfactory = this->factory();
338
339 }
340
341 auto pfactoryitem = pfactory->get_factory_item< TYPE >();
342
343 if (!pfactoryitem)
344 {
345
346 string strMessage;
347
348 strMessage.formatf("matter::constructø has failed to find factory_item for type \"%s\"", ::platform::type(::type<TYPE>()).c_str());
349
350 throw_exception(::error_not_implemented, strMessage);
351
352 }
353
354 auto pparticleNew = pfactoryitem->__call__create_particle();
355
356 if (!pparticleNew)
357 {
358
359 string strMessage;
360
361 strMessage.formatf("matter::constructø no memory to allocate implementation of type \"%s\"", ::platform::type(::type<TYPE>()).c_str());
362
363 throw_exception(::error_no_memory, strMessage);
364
365 }
366
367 p = ::transfer(pparticleNew);
368
369 if (!p)
370 {
371
372 string strMessage;
373
374 strMessage.formatf("matter::constructø object(%s) is not of type \"%s\"", ::platform::type(typeid(*pparticleNew)).c_str(), ::platform::type(::type<TYPE>()).c_str());
375
376 throw_exception(::error_wrong_type, strMessage);
377
378 }
379
380}
381
382
383// template < typename T, typename ...Args >
384// inline void particle::__call__forward_construct(::pointer < T > &p, Args &&... args)
385// {
386//
387//
388// // if (::is_null(pfactory))
389// // {
390//
391// auto pfactory = this->factory();
392//
393// //}
394//
395// auto pfactoryitem = pfactory->get_factory_item< T >();
396//
397// if (!pfactoryitem)
398// {
399//
400// string strMessage;
401//
402// strMessage.formatf("matter::constructø has failed to find factory_item for type \"%s\"", ::platform::type(::type<T>()).c_str());
403//
404// throw_exception(::error_not_implemented, strMessage);
405//
406// }
407//
408// auto pparticleNew = pfactoryitem->__call__create_particle();
409//
410// if (!pparticleNew)
411// {
412//
413// string strMessage;
414//
415// strMessage.formatf("matter::constructø no memory to allocate implementation of type \"%s\"", ::platform::type(::type<TYPE>()).c_str());
416//
417// throw_exception(::error_no_memory, strMessage);
418//
419// }
420//
421// p = ::transfer(pparticleNew);
422//
423// if (!p)
424// {
425//
426// string strMessage;
427//
428// strMessage.formatf("matter::constructø object(%s) is not of type \"%s\"", ::platform::type(typeid(*pparticleNew)).c_str(), ::platform::type(::type<TYPE>()).c_str());
429//
430// throw_exception(::error_wrong_type, strMessage);
431//
432// }
433//
434// }
435
436
437
438template < typename BASE_TYPE >
439inline ::pointer<BASE_TYPE> particle::__raw_create(::factory::factory* pfactory)
440{
441
442 ::pointer<BASE_TYPE> p;
443
444 øraw_construct(p, pfactory);
445
446 return ::transfer(p);
447
448}
449
450
451
452
453//template < typename TYPE >
454//inline void constructø(::particle* pparticle, ::pointer<TYPE>& p, ::factory::factory* pfactory)
455//{
456//
457// øraw_construct(p, pfactory);
458//
459// p->initialize(pparticle);
460//
461//}
462//
463//
464//template < typename BASE_TYPE >
465//inline ::pointer < BASE_TYPE > createø(::particle* pparticle, ::factory::factory* pfactory)
466//{
467//
468// ::pointer < BASE_TYPE > p;
469//
470// constructø(pparticle, p, pfactory);
471//
472// return p;
473//
474//}
475
476
477template < typename TYPE >
478inline bool particle::__call__defer_construct(::pointer<TYPE>& p, ::factory::factory* pfactory)
479{
480
481 if (p)
482 {
483
484#if REFERENCING_DEBUGGING
485
486 ::allocator::defer_erase_referer();
487#endif
488
489 return false;
490
491 }
492
493 __call__construct(p, pfactory);
494
495 return true;
496
497}
498
499
500//template < typename TYPE >
501//inline bool particle::defer_constructø(::pointer<TYPE>& p, ::factory::factory* pfactory)
502//{
503//
504// if (!p)
505// {
506//
507// constructø(p, pfactory);
508//
509// return true;
510//
511// }
512//
513// return false;
514//
515//}
516
517
518template < typename TYPE >
519inline void particle::__call__construct_by_type(::pointer<TYPE> &p, const ::platform::type & type, ::factory::factory *pfactory)
520{
521
522 if (::is_null(pfactory))
523 {
524
525 pfactory = this->factory();
526
527 }
528
529 auto pfactoryitem = pfactory->_find_factory_item(type);
530
531 if (::is_null(pfactoryitem))
532 {
533
534 ::string strError;
535
536 strError.format("Factory item not found for type \"{}\"", type.as_string());
537
538 throw ::exception(error_not_found, strError);
539
540 }
541
542 auto pparticleNew = pfactoryitem->__call__create_particle();
543
544 //if (!pparticleNew)
545 //{
546
547 // return error_no_memory;
548
549 //}
550
551 p = pparticleNew;
552
553 if (!p)
554 {
555
556 throw_exception(error_wrong_type);
557
558 }
559
560 p->set_flag(e_flag_factory);
561
562 //auto estatus =
563
564 p->initialize(this);
565
566 //if (!estatus)
567 //{
568
569 // return estatus;
570
571 //}
572
573 //return estatus;
574
575}
576
577
578
579// template < typename TYPE >
580// inline void particle::__call__construct_by_type_index(::pointer<TYPE>& p, const ::std::type_index & typeindex, ::factory::factory* pfactory)
581// {
582//
583// if (::is_null(pfactory))
584// {
585//
586// pfactory = this->factory();
587//
588// }
589//
590// auto pfactoryitem = pfactory->_get_factory_item_by_type_index(typeindex);
591//
592// auto pparticleNew = pfactoryitem->__call__create_particle();
593//
594// //if (!pparticleNew)
595// //{
596//
597// // return error_no_memory;
598//
599// //}
600//
601// p = pparticleNew;
602//
603// if (!p)
604// {
605//
606// throw_exception(error_wrong_type);
607//
608// }
609//
610// p->set_flag(e_flag_factory);
611//
612// //auto estatus =
613//
614// p->initialize(this);
615//
616// //if (!estatus)
617// //{
618//
619// // return estatus;
620//
621// //}
622//
623// //return estatus;
624//
625// }
626//
627
628
629//template < class T >
630//template < typename PARTICLE >
631//inline pointer < T >& pointer < T >::create(PARTICLE* pparticle, ::factory::factory* pfactory)
632//{
633//
634// auto p = pparticle->createø < T >(pfactory);
635//
636// return operator =(p);
637//
638//}
639
640
641
Definition critical_section.h:29
Definition factory.h:201
::pointer< ORIGIN_TYPE > __call__create() override
consumes a referer
Definition _impl_factory.h:12
Definition particle.h:95