Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
WeakPtrFactories.h
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3 *
4 * MoDELib is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef model_WeakPtrFactories_H_
9#define model_WeakPtrFactories_H_
10
11#include <map>
12#include <memory>
13#include <assert.h>
14
15#include <CRTP.h>
16
17namespace model
18{
19
20
21 template<typename Derived,typename ValueType,typename CompareType=std::less<typename ValueType::KeyType>>
22 struct WeakPtrFactory : public std::map<typename ValueType::KeyType,const std::weak_ptr<ValueType>>
23 {
24 typedef typename ValueType::KeyType KeyType;
25 typedef std::weak_ptr<ValueType> WeakPtrType;
26 typedef std::shared_ptr<ValueType> SharedPtrType;
27
28// using Base = std::map<typename ValueType::KeyType,const std::weak_ptr<ValueType>>;
29// using typename Base::const_iterator;
30// using typename Base::iterator;
31// using typename Base::key_type;
32// using typename Base::mapped_type;
33// using typename Base::size_type;
34// using typename Base::value_type;
35//
36// using Base::at;
37// using Base::Base;
38// using Base::begin;
39// using Base::cbegin;
40// using Base::cend;
41// using Base::clear;
42// using Base::emplace;
43// using Base::emplace_hint;
44// using Base::empty;
45// using Base::end;
46// using Base::erase;
47// using Base::find;
48// using Base::insert;
49// using Base::max_size;
50// using Base::swap;
51// using Base::operator[];
52// using Base::size;
53
54 template<typename...Args>
55 SharedPtrType create(const Args&...args)
56 {
57 SharedPtrType temp(new ValueType(static_cast<Derived*>(this),args...));
58
59 const auto iter(this->find(temp->key));
60 if(iter!=this->end())
61 {// key found
62 if(iter->second.expired())
63 {// shared_ptr was deleted elsewhere
64 this->erase(iter);
65 return this->emplace(temp->key,WeakPtrType(temp)).first->second.lock();
66 }
67 else
68 {// shared_ptr exists
69 assert(false && "key already existing");
70 return SharedPtrType(nullptr);
71 }
72 }
73 else
74 {// key not found found
75 return this->emplace(temp->key,WeakPtrType(temp)).first->second.lock();
76 }
77
78 }
79
80 /**************************************************************************/
82 {
83 const auto iter(this->find(key));
84 if(iter!=this->end())
85 {// key found
86 if(iter->second.expired())
87 {// shared_ptr was deleted elsewhere
88 return SharedPtrType(nullptr);
89 }
90 else
91 {// shared_ptr exists
92 return iter->second.lock();
93 }
94 }
95 else
96 {// key not found found
97 return SharedPtrType(nullptr);
98 }
99 }
100
101 const ValueType& getRef(const KeyType& key) const
102 {
104 if(ptr)
105 {
106 return *ptr;
107 }
108 else
109 {
110 throw std::runtime_error("WeakPtrFactory::getRef key not found");
111 return *ptr;
112 }
113 }
114
115 /**************************************************************************/
116 template<typename...Args>
117 SharedPtrType clone(const KeyType& key,const Args&... args)
118 {
119 const auto iter(this->find(key));
120 if(iter!=this->end())
121 {// key found
122 if(iter->second.expired())
123 {// shared_ptr was deleted elsewhere
124 return nullptr;
125 }
126 else
127 {// shared_ptr exists
128 const auto temp(iter->second.lock()->clone(args...));
129 return this->emplace(temp->key,WeakPtrType(temp)).first->second.lock();
130 }
131 }
132 else
133 {// key not found found
134 return nullptr;
135 }
136 }
137
138 };
139
140 template<typename Derived,typename ValueType,typename CompareType=std::less<typename ValueType::KeyType>>
141 struct KeyConstructableWeakPtrFactory : public std::map<typename ValueType::KeyType,
142 /* */ const std::weak_ptr<ValueType>,
143 /* */ CompareType>
144 {
145 typedef typename ValueType::KeyType KeyType;
146 typedef std::weak_ptr<ValueType> WeakPtrType;
147 typedef std::shared_ptr<ValueType> SharedPtrType;
148
149 /**************************************************************************/
151 {
152 const auto iter(this->find(key));
153 if(iter!=this->end())
154 {// key found
155 if(iter->second.expired())
156 {// shared_ptr was deleted elsewhere
157 this->erase(iter);
158 return this->emplace(key,SharedPtrType(new ValueType(static_cast<Derived*>(this),key))).first->second.lock();
159 }
160 else
161 {// shared_ptr exists
162 return iter->second.lock();
163 }
164 }
165 else
166 {// key not found found
167 // return this->emplace(key,new ValueType(this->derived(),key)).first->second;
168 return this->emplace(key,SharedPtrType(new ValueType(static_cast<Derived*>(this),key))).first->second.lock();
169 }
170
171 }
172
173// size_t keyID(const KeyType& key) const
174// {
175// return std::distance(this->find(key),this->begin());
176// }
177
178
179 // /**************************************************************************/
180 // template<typename...Args>
181 // SharedPtrType get(const Args&...args)
182 // {
183 // const KeyType key(ValueType::getKey(args...));
184 // const auto iter(this->find(key));
185 // if(iter!=this->end())
186 // {// key found
187 // if(iter->second.expired())
188 // {// shared_ptr was deleted elsewhere
189 // this->erase(iter);
190 // return this->emplace(key,SharedPtrType(new ValueType(static_cast<Derived*>(this),args...))).first->second.lock();
191 // }
192 // else
193 // {// shared_ptr exists
194 // return iter->second.lock();
195 // }
196 // }
197 // else
198 // {// key not found found
200 // return this->emplace(key,SharedPtrType(new ValueType(static_cast<Derived*>(this),args...))).first->second.lock();
201 // }
202 //
203 // }
204
205 };
206
207
208}
209#endif
210
211
212//
213// struct iterator : public BaseType::iterator
214// {
215//
216// typedef typename BaseType::iterator BaseIteratorType;
217//
218// const BaseType* const p_map;
219//
220//
221// iterator(const BaseType* const p_map_in, const BaseIteratorType& iter) :
222// /* init */ BaseType::iterator(iter)
223// /* init */,p_map(p_map_in)
224// {
225//
226// }
227//
228// iterator& operator++()
229// {
230// iterBase().operator++();
231// while(iterBase()!=p_map->end())
232// {
233// if(iterBase()->second.expired())
234// {
235// iterBase().operator++();
236// }
237// else
238// {
239// break;
240// }
241//
242// }
243// return *this;
244// }
245//
246// bool operator==(const iterator& other) const
247// {
248// return iterBase()==other.iterBase();
249// }
250//
251// bool operator!=(const iterator& other) const
252// {
253// return iterBase()!=other.iterBase();
254// }
255//
256// BaseIteratorType& iterBase()
257// {
258// return *this;
259// }
260//
261// const BaseIteratorType& iterBase() const
262// {
263// return *this;
264// }
265//
266// };
267//
268//
269// struct const_iterator : public BaseType::const_iterator
270// {
271//
272// typedef typename BaseType::const_iterator BaseIteratorType;
273//
274// const BaseType* const p_map;
275//
276//
277// const_iterator(const BaseType* const p_map_in, const BaseIteratorType& iter) :
278// /* init */ BaseType::const_iterator(iter)
279// /* init */,p_map(p_map_in)
280// {
281//
282// }
283//
284// const_iterator& operator++()
285// {
286// iterBase().operator++();
287// while(iterBase()!=p_map->end())
288// {
289// if(iterBase()->second.expired())
290// {
291// iterBase().operator++();
292// }
293// else
294// {
295// break;
296// }
297//
298// }
299// return *this;
300// }
301//
302// bool operator==(const const_iterator& other) const
303// {
304// return iterBase()==other.iterBase();
305// }
306//
307// bool operator!=(const const_iterator& other) const
308// {
309// return iterBase()!=other.iterBase();
310// }
311//
312// BaseIteratorType& iterBase()
313// {
314// return *this;
315// }
316//
317// const BaseIteratorType& iterBase() const
318// {
319// return *this;
320// }
321//
322// };
323//
324// iterator begin()
325// {
326// auto baseIter(base().begin());
327// while(baseIter!=base().end())
328// {
329// if(baseIter->second.expired())
330// {
331// baseIter++;
332// }
333// else
334// {
335// break;
336// }
337// }
338// return iterator(this,baseIter);
339// }
340//
341// const_iterator begin() const
342// {
343// auto baseIter(base().begin());
344// while(baseIter!=base().end())
345// {
346// if(baseIter->second.expired())
347// {
348// baseIter++;
349// }
350// else
351// {
352// break;
353// }
354// }
355// return const_iterator(this,baseIter);
356// }
357//
358// iterator end()
359// {
360// return iterator(this,base().end());
361// }
362//
363// const_iterator end() const
364// {
365// return const_iterator(this,base().end());
366// }
367//
368// BaseType& base()
369// {
370// return *this;
371// }
372//
373// const BaseType& base() const
374// {
375// return *this;
376// }
377
378
379// template<typename ValueType,typename CompareType>
380// struct WeakPtrFactoryBase : public std::map<typename ValueType::KeyType,
381// /* */ const std::weak_ptr<ValueType>,
382// /* */ CompareType>
383// {
384//
385// typedef typename ValueType::KeyType KeyType;
386// typedef std::weak_ptr<ValueType> WeakPtrType;
387// typedef std::shared_ptr<ValueType> SharedPtrType;
388// typedef std::map<KeyType, const WeakPtrType> BaseType;
389//
390//
391// class iterator : public BaseType::iterator
392// {
393//
394// typedef typename BaseType::iterator BaseIteratorType;
395//
396// const BaseType* const p_map;
397//
398// public:
399//
400// iterator(const BaseType* const p_map_in, const BaseIteratorType& iter) :
401// /* init */ BaseType::iterator(iter)
402// /* init */,p_map(p_map_in)
403// {
404//
405// }
406//
407// iterator& operator++()
408// {
409// iterBase().operator++();
410// while(iterBase()!=p_map->end())
411// {
412// if(iterBase()->second.expired())
413// {
414// iterBase().operator++();
415// }
416// else
417// {
418// break;
419// }
420//
421// }
422// return *this;
423// }
424//
425// bool operator==(const iterator& other) const
426// {
427// return iterBase()==other.iterBase();
428// }
429//
430// bool operator!=(const iterator& other) const
431// {
432// return iterBase()!=other.iterBase();
433// }
434//
435// BaseIteratorType& iterBase()
436// {
437// return *this;
438// }
439//
440// const BaseIteratorType& iterBase() const
441// {
442// return *this;
443// }
444//
445// };
446//
447// class const_iterator : public BaseType::const_iterator
448// {
449//
450// typedef typename BaseType::const_iterator BaseIteratorType;
451//
452// const BaseType* const p_map;
453//
454// public:
455//
456// const_iterator(const BaseType* const p_map_in, const BaseIteratorType& iter) :
457// /* init */ BaseType::const_iterator(iter)
458// /* init */,p_map(p_map_in)
459// {
460//
461// }
462//
463// const_iterator& operator++()
464// {
465// iterBase().operator++();
466// while(iterBase()!=p_map->end())
467// {
468// if(iterBase()->second.expired())
469// {
470// iterBase().operator++();
471// }
472// else
473// {
474// break;
475// }
476//
477// }
478// return *this;
479// }
480//
481// bool operator==(const const_iterator& other) const
482// {
483// return iterBase()==other.iterBase();
484// }
485//
486// bool operator!=(const const_iterator& other) const
487// {
488// return iterBase()!=other.iterBase();
489// }
490//
491// BaseIteratorType& iterBase()
492// {
493// return *this;
494// }
495//
496// const BaseIteratorType& iterBase() const
497// {
498// return *this;
499// }
500//
501// };
502//
503// iterator begin()
504// {
505// auto baseIter(base().begin());
506// while(baseIter!=base().end())
507// {
508// if(baseIter->second.expired())
509// {
510// baseIter++;
511// }
512// else
513// {
514// break;
515// }
516// }
517// return iterator(this,baseIter);
518// }
519//
520// const_iterator begin() const
521// {
522// auto baseIter(base().begin());
523// while(baseIter!=base().end())
524// {
525// if(baseIter->second.expired())
526// {
527// baseIter++;
528// }
529// else
530// {
531// break;
532// }
533// }
534// return const_iterator(this,baseIter);
535// }
536//
537// iterator end()
538// {
539// return iterator(this,base().end());
540// }
541//
542// const_iterator end() const
543// {
544// return const_iterator(this,base().end());
545// }
546//
547// BaseType& base()
548// {
549// return *this;
550// }
551//
552// const BaseType& base() const
553// {
554// return *this;
555// }
556//
557// size_t keyID(const KeyType& key) const
558// {
559// size_t temp(0);
560// for(const_iterator pair=begin();pair!=end();++pair)
561// {
562// if(pair->first==key)
563// {
564// return temp;
565// }
566// temp++;
567// }
568// assert(false && "key nof found");
569// return temp;
570// }
571// };
SharedPtrType getFromKey(const KeyType &key)
std::shared_ptr< LoopType > SharedPtrType
const ValueType & getRef(const KeyType &key) const
SharedPtrType create(const Args &...args)
SharedPtrType get(const KeyType &key) const
SharedPtrType clone(const KeyType &key, const Args &... args)
int end[2]
triangle ptr
newbad key
vertex temp