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

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