aboutsummaryrefslogtreecommitdiffstats
path: root/interface/type_depository.hpp
blob: 6abaa1ba3ca69392dbdf9b8c7bda33592138889f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/**
   \file type_depository.hpp
   \brief depository and draw objects
   \author Junhua Gu
 */



#ifndef TYPE_DEPOSITORY_HPP
#define TYPE_DEPOSITORY_HPP
#include <string>
#include <utility>
#include <core/optimizer.hpp>
#include <core/fitter.hpp>
#include <map>
#include <iostream>
namespace opt_utilities
{

  enum fetch_direction
    {
      in,out
    };

  class type_unregistered
    :public opt_exception
  {
    const char* what()const throw()
    {
      return "type not registred";
    }
  };

  template <typename T>
  class holder
  {
  private:
    T* ptr;
  public:
    holder()
      :ptr(0)
    {}
    
    holder(T* p)
      :ptr(p)
    {}

    holder(const holder& rhs)
      :ptr(rhs.ptr)
    {
      const_cast<holder&>(rhs).ptr=0;
    }

    ~holder()
    {
      destroy();
    }

    holder& operator=(const holder& rhs)
    {
      if(this==&rhs)
	{
	  return *this;
	}
      destroy();
      ptr=rhs.ptr;
      const_cast<holder&>(rhs).ptr=0;
      return *this;
    }

  public:
    T* release()
    {
      T* p=ptr;
      ptr=0;
      return p;
    }

    void destroy()
    {
      if(ptr)
	{
	  ptr->destroy();
	}
    }

    T* get()const
    {
      return ptr;
    }

    void reset(T* p)
    {
      destroy();
      ptr=p;
    }

    operator T*()
    {
      return ptr;
    }

    operator const T*()const
    {
      return ptr;
    }
    
  public:
    T& operator*()
    {
      return *ptr;
    }

  public:
    T* operator->()
    {
      return ptr;
    }
  };

  
  template <typename Ty,typename Tx>
  void delete_clone(const func_obj<Ty,Tx>* pfo)
  {
    const_cast<func_obj<Ty,Tx>* >(pfo)->destroy();
  }

  template <typename Ty,typename Tx>
  void fetch_func_obj(const func_obj<Ty,Tx>* &fo,std::string cname,fetch_direction dir=in)
  {
    static std::map<std::string,holder<func_obj<Ty,Tx> > >pm;
    typename std::map<std::string,
      holder<func_obj<Ty,Tx> > >::iterator it=pm.find(cname);
    
    if(dir==out)
      {
	if(it==pm.end())
	  {
	    std::cerr<<cname<<std::endl;
	    throw type_unregistered();;
	  }
	else
	  {
	    func_obj<Ty,Tx>* result=it->second;
	    fo=result;
	  }
      }
    else if(dir==in)
      {
	//pm.insert(make_pair(cname,holder<func_obj<Ty,Tx> >(fo->clone())));
	
	pm[cname]=holder<func_obj<Ty,Tx> >(fo->clone());
      }
  }

  template <typename Ty,typename Tx>
  void register_func_obj(const func_obj<Ty,Tx>& fo)
  {
    const func_obj<Ty,Tx>* pfo=&fo;
    fetch_func_obj(pfo,fo.get_type_name(),in);
  }

  template <typename Ty,typename Tx>
  const func_obj<Ty,Tx>* get_func_obj(std::string cname)
  {
    const func_obj<Ty,Tx>* pom;
    fetch_func_obj(pom,cname,out);
    return pom;
  }


  template <typename Ty,typename Tx>
  void delete_clone(const opt_method<Ty,Tx>* pfo)
  {
    const_cast<opt_method<Ty,Tx>* >(pfo)->destroy();
  }

  template <typename Ty,typename Tx>
  void fetch_opt_method(const opt_method<Ty,Tx>* &fo,std::string cname,fetch_direction dir)
  {
    static std::map<std::string,holder<opt_method<Ty,Tx> > > pm;
    typename std::map<std::string,
      holder<opt_method<Ty,Tx> > >::iterator it=pm.find(cname);
    
    if(dir==out)
      {
	if(it==pm.end())
	  {
	    std::cerr<<cname<<std::endl;
	    throw type_unregistered();;
	  }
	else
	  {
	    opt_method<Ty,Tx>* result=it->second;
	    fo=result;
	  }
      }
    else if(dir==in)
      {
	//pm.insert(cname,fo->clone());
	pm[cname]=holder<opt_method<Ty,Tx> >(fo->clone());
      }
  }

  template <typename Ty,typename Tx>
  void register_opt_method(const opt_method<Ty,Tx>& fo)
  {
    const opt_method<Ty,Tx>* pfo=&fo;
    fetch_opt_method(pfo,fo.get_type_name(),in);
  }

  template <typename Ty,typename Tx>
  const opt_method<Ty,Tx>* get_opt_method(std::string cname)
  {
    const opt_method<Ty,Tx>* pom;
    fetch_opt_method(pom,cname,out);
    return pom;
  }

  template<typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr>
  void delete_clone(const statistic<Ty,Tx,Tp,Ts,Tstr>* pfo)
  {
    const_cast<statistic<Ty,Tx,Tp,Ts,Tstr>* >(pfo)->destroy();
  }


  template<typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr>
  void fetch_statistic(const statistic<Ty,Tx,Tp,Ts,Tstr>* &fo,std::string cname,fetch_direction dir)
  {
    static std::map<std::string,holder<statistic<Ty,Tx,Tp,Ts,Tstr> > > pm;
    typename std::map<std::string,
      holder<statistic<Ty,Tx,Tp,Ts,Tstr> > >::iterator it=pm.find(cname);
    
    if(dir==out)
      {
	if(it==pm.end())
	  {
	    std::cerr<<cname<<std::endl;
	    throw type_unregistered();;
	  }
	else
	  {
	    statistic<Ty,Tx,Tp,Ts,Tstr>* result=it->second;
	    fo=result;
	  }
      }
    else if(dir==in)
      {
	//pm.insert(cname,fo->clone());
	pm[cname]=holder<statistic<Ty,Tx,Tp,Ts,Tstr> >(fo->clone());
      }
  }

  template<typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr>
  void register_statistic(const statistic<Ty,Tx,Tp,Ts,Tstr>& fo)
  {
    const statistic<Ty,Tx,Tp,Ts,Tstr>* pfo=&fo;
    fetch_statistic(pfo,fo.get_type_name(),in);
  }

  template<typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr>
  const statistic<Ty,Tx,Tp,Ts,Tstr>* get_statistic(std::string cname)
  {
    const statistic<Ty,Tx,Tp,Ts,Tstr>* pst;
    fetch_statistic(pst,cname,out);
    return pst;
  }

  template <typename Ty,typename Tx,typename Tp,typename Tstr>
  void delete_clone(const param_modifier<Ty,Tx,Tp,Tstr>* pfo)
  {
    const_cast<param_modifier<Ty,Tx,Tp,Tstr>* >(pfo)->destroy();
  }

  template <typename Ty,typename Tx,typename Tp,typename Tstr>
  void fetch_param_modifier(const param_modifier<Ty,Tx,Tp,Tstr>* &fo,std::string cname,fetch_direction dir)
  {
    static std::map<std::string,holder<param_modifier<Ty,Tx,Tp,Tstr> > > pm;
    typename std::map<std::string,
      holder<param_modifier<Ty,Tx,Tp,Tstr> > >::iterator it=pm.find(cname);
    
    if(dir==out)
      {
	if(it==pm.end())
	  {
	    std::cerr<<cname<<std::endl;
	    throw type_unregistered();
	  }
	else
	  {
	    param_modifier<Ty,Tx,Tp,Tstr>* result=it->second;
	    fo=result;
	  }
      }
    else if(dir==in)
      {
	//pm.insert(cname,fo->clone());
	pm[cname]=holder<param_modifier<Ty,Tx,Tp,Tstr> >(fo->clone());
      }
  }

  template<typename Ty,typename Tx,typename Tp,typename Tstr>
  void register_param_modifier(const param_modifier<Ty,Tx,Tp,Tstr>& fo)
  {
    const param_modifier<Ty,Tx,Tp,Tstr>* pfo=&fo;
    fetch_param_modifier(pfo,fo.get_type_name(),in);
  }

  template<typename Ty,typename Tx,typename Tp,typename Tstr>
  const param_modifier<Ty,Tx,Tp,Tstr>* get_param_modifier(std::string cname)
  {
    const param_modifier<Ty,Tx,Tp,Tstr>* ppm;
    fetch_param_modifier(ppm,cname,out);
    return ppm;
  }
  
  
  template <typename Ty,typename Tx>
  void delete_clone(const data_set<Ty,Tx>* pfo)
  {
    const_cast<data_set<Ty,Tx>* >(pfo)->destroy();
  }

  template <typename Ty,typename Tx>
  void fetch_data_set(const data_set<Ty,Tx>* &fo,std::string cname,fetch_direction dir)
  {
    static std::map<std::string,holder<data_set<Ty,Tx> > > pm;
    typename std::map<std::string,
      holder<data_set<Ty,Tx> > >::iterator it=pm.find(cname);
    
    if(dir==out)
      {
	if(it==pm.end())
	  {
	    std::cerr<<cname<<std::endl;
	    throw type_unregistered();;
	  }
	else
	  {
	    data_set<Ty,Tx>* result=it->second;
	    fo=result;
	  }
      }
    else if(dir==in)
      {
	//pm.insert(cname,fo->clone());
	pm[cname]=holder<data_set<Ty,Tx> >(fo->clone());
      }
  }

  template <typename Ty,typename Tx>
  void register_data_set(const data_set<Ty,Tx>& fo)
  {
    const data_set<Ty,Tx>* pfo=&fo;
    fetch_data_set(pfo,fo.get_type_name(),in);
  }

  template<typename Ty,typename Tx>
  const data_set<Ty,Tx>* get_data_set(std::string cname)
  {
    const data_set<Ty,Tx>* pds;
    fetch_data_set(pds,cname,out);
    return pds;
  }
  
  

  ////////////////////
  template <typename Ty,typename Tx,typename Tp,typename Tstr>
  void delete_clone(const model<Ty,Tx,Tp,Tstr>* pfo)
  {
    const_cast<model<Ty,Tx,Tp,Tstr>* >(pfo)->destroy();
  }


  template <typename Ty,typename Tx,typename Tp,typename Tstr>
  void fetch_model(const model<Ty,Tx,Tp,Tstr>* &fo,std::string cname,fetch_direction dir)
  {
    static std::map<std::string,holder<model<Ty,Tx,Tp,Tstr> > > pm;
    typename std::map<std::string,
      holder<model<Ty,Tx,Tp,Tstr> > >::iterator it=pm.find(cname);
    
    if(dir==out)
      {
	if(it==pm.end())
	  {
	    std::cerr<<cname<<std::endl;
	    throw type_unregistered();
	  }
	else
	  {
	    model<Ty,Tx,Tp,Tstr>* result=it->second;
	    fo=result;
	  }
      }
    else if(dir==in)
      {
	pm[cname]= holder<model<Ty,Tx,Tp,Tstr> >(fo->clone());
      }
  }

  template <typename Ty,typename Tx,typename Tp,typename Tstr>
  void register_model(const model<Ty,Tx,Tp,Tstr>& fo)
  {
    const model<Ty,Tx,Tp,Tstr>* pfo=&fo;
    fetch_model(pfo,fo.get_type_name(),in);
  }

  template <typename Ty,typename Tx,typename Tp,typename Tstr>
  const model<Ty,Tx,Tp,Tstr>* get_model(std::string cname)
  {
    const model<Ty,Tx,Tp,Tstr>* pds;
    fetch_model(pds,cname,out);
    return pds;
  }
  
}


#endif