OpendTect  6.3
factory.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: A.H.Bril
8  Date: Sep 1994, Aug 2006
9 ________________________________________________________________________
10 
11 -*/
12 
13 #include "basicmod.h"
14 #include "bufstringset.h"
15 #include "perthreadrepos.h"
16 #include "ptrman.h"
17 #include "typeset.h"
18 #include "uistring.h"
19 
26 {
27 public:
28  virtual ~FactoryBase();
29 
30  int size() const;
31  bool isEmpty() const;
32 
33  bool hasName(const char* n) {return indexOf(n)>=0;}
34  const BufferStringSet& getNames() const;
35  const uiStringSet& getUserNames() const;
36  void setDefaultName(int idx);
39  const char* getDefaultName() const;
40  static char cSeparator() { return ','; }
41 
42  uiString errMsg() const;
45 
46  const char* currentName() const;
54 protected:
55 
56  int indexOf(const char*) const;
57  void addNames(const char*,const uiString&);
58  void setNames(int,const char*,const uiString&);
59 
62 
63 private:
64 
69 };
70 
71 
115 template <class T>
116 mClass(Basic) Factory : public FactoryBase
118 public:
119  typedef T* (*Creator)();
120  inline void addCreator(Creator,const char* nm,
121  const uiString& username =
130  inline T* create(const char* nm) const;
132 protected:
133 
135 };
136 
137 
186 template <class T, class P>
187 mClass(Basic) Factory1Param : public FactoryBase
189 public:
190  typedef T* (*Creator)(P);
191  inline void addCreator(Creator,const char* nm=0,
192  const uiString& usernm =
200  inline T* create(const char* nm, P, bool chknm=true)const;
203 protected:
204 
206 };
207 
208 
213 template <class T, class P0, class P1>
214 mClass(Basic) Factory2Param : public FactoryBase
215 {
216 public:
217  typedef T* (*Creator)(P0,P1);
218  inline void addCreator(Creator,const char* nm=0,
219  const uiString& usernm=uiString::emptyString());
226  inline T* create(const char* nm, P0, P1,
227  bool chknm=true)const;
230 protected:
231 
233 };
234 
235 
240 template <class T, class P0, class P1, class P2>
241 mClass(Basic) Factory3Param : public FactoryBase
242 {
243 public:
244  typedef T* (*Creator)(P0,P1,P2);
245  inline void addCreator(Creator,const char* nm=0,
246  const uiString& usernm =uiString::emptyString());
253  inline T* create(const char* nm, P0, P1, P2,
254  bool chknm=true)const;
257 protected:
258 
260 };
261 
262 
263 #define mDefaultFactoryStringImpl \
264  const char* factoryKeyword() const { return sFactoryKeyword(); } \
265  uiString factoryDisplayName() const \
266  { return sFactoryDisplayName(); }
267 
268 #define mDefaultStaticFactoryStringDeclaration \
269  static const char* sFactoryKeyword(); \
270  static uiString sFactoryDisplayName()
271 
272 #define mDefaultFactoryInitClassImpl( baseclss, createfunc ) \
273 { \
274  baseclss::factory().addCreator(createfunc,sFactoryKeyword(), \
275  sFactoryDisplayName()); \
276 }
277 
278 #define mDefaultFactoryInstanciationBase( keywrd, usernm ) \
279  mDefaultFactoryStringImpl \
280  static const char* sFactoryKeyword() { return keywrd; } \
281  static uiString sFactoryDisplayName() { return usernm; } \
282  static void initClass()
283 
284 #define mDefaultFactoryCreatorImpl( baseclss, clss ) \
285 static baseclss* createInstance() { return new clss; } \
286 
287 #define mDefaultFactoryInstantiation( baseclss, clss, keywrd, usernm ) \
288  mDefaultFactoryCreatorImpl( baseclss, clss ); \
289  mDefaultFactoryInstanciationBase( keywrd, usernm ) \
290  mDefaultFactoryInitClassImpl( baseclss, createInstance )
291 
292 
293 #define mDefaultFactoryCreatorImpl1Param( baseclss, clss, parclss ) \
294 static baseclss* createInstance( parclss p1 ) \
295  { return new clss( p1 ); }
296 
297 #define mDefaultFactoryInstantiation1Param( baseclss, clss, parclss,\
298  keywrd, usernm ) \
299  mDefaultFactoryCreatorImpl1Param( baseclss, clss, parclss ) \
300  mDefaultFactoryInstanciationBase( keywrd, usernm ) \
301  mDefaultFactoryInitClassImpl( baseclss, createInstance )
302 
303 #define mDefaultFactoryCreatorImpl2Param( baseclss, clss, parclss1, parclss2 ) \
304 static baseclss* createInstance( parclss1 p1, parclss2 p2 ) \
305  { return new clss( p1, p2 ); }
306 
307 #define mDefaultFactoryInstantiation2Param( baseclss, clss, parclss1,\
308  parclss2, keywrd, usernm ) \
309  mDefaultFactoryCreatorImpl2Param( baseclss, clss, parclss1, parclss2 ) \
310  mDefaultFactoryInstanciationBase( keywrd, usernm ) \
311  mDefaultFactoryInitClassImpl( baseclss, createInstance )
312 
313 
314 #define mCreateImpl( donames, createfunc ) \
315  currentname_.getObject() = name; \
316  if ( donames ) \
317  { \
318  const int idx = indexOf( name ); \
319  if ( idx==-1 ) \
320  { \
321  errmsgs_.getObject() = "Name "; \
322  errmsgs_.getObject().add( name ).add(" not found.\n" ) \
323  .add( "Perhaps all plugins are not loaded\n" );\
324  return 0; \
325  } \
326  return createfunc; \
327  } \
328  \
329  for ( int idx=0; idx<creators_.size(); idx++ ) \
330  { \
331  T* res = createfunc; \
332  if ( res ) return res; \
333  } \
334  \
335  return 0
336 
337 
338 #define mAddCreator \
339  const int idx = indexOf( name );\
340 \
341  if ( idx==-1 )\
342  { \
343  addNames( name, username ); \
344  creators_ += cr; \
345  } \
346  else\
347  {\
348  setNames( idx, name, username ); \
349  creators_[idx] = cr;\
350  }
351 
352 template <class T> inline
353 void Factory<T>::addCreator( Creator cr, const char* name,
354  const uiString& username )
355 {
356  if ( !name ) return;\
357  mAddCreator;
358 }
359 
360 
361 template <class T> inline
362 T* Factory<T>::create( const char* name ) const
363 {
364  mCreateImpl( true, creators_[idx]() );
365 }
366 
367 
368 template <class T, class P> inline
369 void Factory1Param<T,P>::addCreator( Creator cr, const char* name,
370  const uiString& username )
371 {
372  mAddCreator;
373 }
374 
375 
376 template <class T, class P> inline
377 T* Factory1Param<T,P>::create( const char* name, P data, bool chk ) const
378 {
379  mCreateImpl( chk, creators_[idx]( data ) );
380 }
381 
382 
383 template <class T, class P0, class P1> inline
384 void Factory2Param<T,P0,P1>::addCreator( Creator cr, const char* name,
385  const uiString& username )
386 {
387  mAddCreator;
388 }
389 
390 
391 template <class T, class P0, class P1> inline
392 T* Factory2Param<T,P0,P1>::create( const char* name, P0 p0, P1 p1,
393  bool chk ) const
394 {
395  mCreateImpl( chk, creators_[idx]( p0, p1 ) );
396 }
397 
398 
399 template <class T, class P0, class P1, class P2> inline
400 void Factory3Param<T,P0,P1,P2>::addCreator( Creator cr, const char* name,
401  const uiString& username )
402 {
403  mAddCreator;
404 }
405 
406 
407 template <class T, class P0, class P1, class P2> inline
408 T* Factory3Param<T,P0,P1,P2>::create( const char* name, P0 p0, P1 p1, P2 p2,
409  bool chk ) const
410 {
411  mCreateImpl( chk, creators_[idx]( p0, p1, p2 ) );
412 }
413 
414 
415 #define mDefineFactory( mod, T, funcname ) \
416 mGlobal(mod) ::Factory<T>& funcname()
417 
418 
419 #define mDefineFactoryInClasswKW( T, funcname, kw ) \
420 static ::Factory<T>& funcname(); \
421 virtual uiString factoryDisplayName() const \
422 { return ::toUiString( factoryKeyword() ); } \
423 virtual const char* factoryKeyword() const { return kw; }
424 #define mDefineFactoryInClass( T, funcname ) \
425  mDefineFactoryInClasswKW( T, funcname, 0 )
426 
427 
428 #define mImplFactory( T, funcname ) \
429 ::Factory<T>& funcname() \
430 { \
431  mDefineStaticLocalObject(PtrMan< ::Factory<T> >,inst,(new ::Factory<T>)); \
432  return *inst; \
433 }
434 
435 
436 #define mDefineFactory1Param( mod, T, P, funcname ) \
437 mGlobal(mod) ::Factory1Param<T,P>& funcname()
438 
439 
440 #define mDefineFactory1ParamInClasswKW( T, P, funcname, kw ) \
441 static ::Factory1Param<T,P>& funcname(); \
442 virtual uiString factoryDisplayName() const \
443 { return ::toUiString(factoryKeyword()); } \
444 virtual const char* factoryKeyword() const { return kw; }
445 #define mDefineFactory1ParamInClass( T, P, funcname ) \
446  mDefineFactory1ParamInClasswKW( T, P, funcname, 0 )
447 
448 #define mImplFactory1Param( T, P, funcname ) \
449 ::Factory1Param<T,P>& funcname() \
450 { \
451  mLockStaticInitLock( static_inst_lck__ ); \
452  static PtrMan< ::Factory1Param<T,P> > \
453  inst(new ::Factory1Param<T,P>); \
454  mUnlockStaticInitLock( static_inst_lck__ ); \
455  \
456  return *inst; \
457 }
458 
459 
460 #define mDefineFactory2Param( mod, T, P0, P1, funcname ) \
461 mGlobal(mod) ::Factory2Param<T,P0,P1>& funcname()
462 
463 
464 #define mDefineFactory2ParamInClasswKW( T, P0, P1, funcname, kw ) \
465 static ::Factory2Param<T,P0,P1>& funcname(); \
466 virtual uiString factoryDisplayName() const \
467 { return ::toUiString( factoryKeyword() ); } \
468 virtual const char* factoryKeyword() const { return kw; }
469 #define mDefineFactory2ParamInClass( T, P0, P1, funcname ) \
470  mDefineFactory2ParamInClasswKW( T, P0, P1, funcname, 0 )
471 
472 
473 #define mImplFactory2Param( T, P0, P1, funcname ) \
474 ::Factory2Param<T,P0,P1>& funcname() \
475 { \
476  mLockStaticInitLock( static_inst_lck__ ); \
477  static PtrMan< ::Factory2Param<T,P0,P1> > \
478  inst(new ::Factory2Param<T,P0,P1>); \
479  mUnlockStaticInitLock( static_inst_lck__ ); \
480  \
481  return *inst; \
482 }
483 
484 
485 #define mDefineFactory3Param( mod, T, P0, P1, P2, funcname ) \
486 mGlobal(mod) ::Factory3Param<T,P0,P1,P2>& funcname()
487 
488 
489 #define mDefineFactory3ParamInClasswKW( T, P0, P1, P2, funcname, kw ) \
490 static ::Factory3Param<T,P0,P1,P2>& funcname(); \
491 virtual uiString factoryDisplayName() const \
492 { return ::toUiString( factoryKeyword() ); } \
493 virtual const char* factoryKeyword() const { return kw; }
494 #define mDefineFactory3ParamInClass( T, P0, P1, P2, funcname ) \
495  mDefineFactory3ParamInClasswKW( T, P0, P1, P2, funcname, 0 )
496 
497 
498 #define mImplFactory3Param( T, P0, P1, P2,funcname ) \
499 ::Factory3Param<T,P0,P1,P2>& funcname() \
500 { \
501  mLockStaticInitLock( static_inst_lck__ ); \
502  static PtrMan< ::Factory3Param<T,P0,P1,P2> > \
503  inst(new ::Factory3Param<T,P0,P1,P2>); \
504  mUnlockStaticInitLock( static_inst_lck__ ); \
505 \
506  return *inst; \
507 }
508 
509 #undef mCreateImpl
510 #undef mAddCreator
#define mExpClass(module)
Definition: commondefs.h:157
Subclass of FactoryBase.
Definition: factory.h:214
#define mODTextTranslationClass(clss)
Definition: uistring.h:37
TypeSet< Creator > creators_
Definition: factory.h:134
void addCreator(Creator, const char *nm=0, const uiString &usernm=uiString::emptyString())
Definition: factory.h:369
Generalized static factory that can deliver instances of T, when no variable is needed in the creatio...
Definition: factory.h:116
BufferString defaultname_
Definition: factory.h:68
T * create(const char *nm, P0, P1, P2, bool chknm=true) const
Definition: factory.h:408
#define mAddCreator
Definition: factory.h:338
static const uiString & emptyString()
Definition: uistring.h:107
BufferStringSet names_
Definition: factory.h:65
Definition: uistring.h:88
Set of BufferString objects.
Definition: bufstringset.h:25
ObjectSet< T >::size_type indexOf(const ObjectSet< T > &os, const S &val)
Locate object in set.
Definition: objectset.h:173
T * create(const char *nm, P0, P1, bool chknm=true) const
Definition: factory.h:392
static char cSeparator()
Definition: factory.h:40
TypeSet< Creator > creators_
Definition: factory.h:205
BufferStringSet aliases_
Definition: factory.h:67
Definition: seistype.h:59
TypeSet< Creator > creators_
Definition: factory.h:259
void addCreator(Creator, const char *nm, const uiString &username=uiString::emptyString())
Definition: factory.h:353
uiStringSet usernames_
Definition: factory.h:66
Generalized static factory that can deliver instances of T, when a variable is needed in the creation...
Definition: factory.h:187
bool hasName(const char *n)
Definition: factory.h:33
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size...
Definition: bufstring.h:38
Base class for Factories ( Factory, Factory1Param, Factory2Param and Factory3Param. )
Definition: factory.h:25
T * create(const char *nm, P, bool chknm=true) const
Definition: factory.h:377
bool isEmpty(const char *)
StaticStringManager currentname_
Definition: factory.h:61
static uiHor3DInterpol * create(uiParent *)
#define mCreateImpl(donames, createfunc)
Definition: factory.h:314
Definition: uistring.h:235
StaticStringManager errmsgs_
Definition: factory.h:60
void addCreator(Creator, const char *nm=0, const uiString &usernm=uiString::emptyString())
Definition: factory.h:400
const char * errMsg() const
Definition: horizontracker.h:59
void addNames(const ODSET &inp, WITHADD &withadd)
Adds all names from a set to another set with an add() function (typically a BufferStringSet) ...
Definition: odset.h:65
T * create(const char *nm) const
Name may be not be null.
Definition: factory.h:362
#define mClass(module)
Definition: commondefs.h:161
Subclass of FactoryBase.
Definition: factory.h:241
TypeSet< Creator > creators_
Definition: factory.h:232
void addCreator(Creator, const char *nm=0, const uiString &usernm=uiString::emptyString())
Definition: factory.h:384

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2017