OpendTect-6_4  6.4
enums.h
Go to the documentation of this file.
1 #ifndef enums_h
2 #define enums_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: 4-2-1994
10  Contents: Enum <--> string conversion
11  RCS: $Id$
12 ________________________________________________________________________
13 
14 -*/
15 
16 #include "basicmod.h"
17 #include "iopar.h"
18 #include "uistring.h"
19 #include "bufstringset.h"
20 
21 #include "namedobj.h"
22 
45 mExpClass(Basic) EnumDef : public NamedObject
46 {
47 public:
48  EnumDef();
49  EnumDef(const char* nm,const char* s[],
50  short nrs=0);
51  bool isValidKey(const char*) const;
52  int indexOf(const char* s) const;
53  int indexOf(int enumval) const;
54  int getEnumValForIndex(int) const;
55  const char* getKeyForIndex(int i) const;
56  uiString getUiStringForIndex(int i) const;
57  void setUiStringForIndex(int,const uiString&);
58  const BufferStringSet& keys() const { return keys_; }
59  const uiStringSet& strings() const { return uistrings_; }
60 
61  const char* getIconFileForIndex(int idx) const;
62  void setIconFileForIndex(int idx,const char*);
63 
64  int size() const;
65 
66  //Expert use only!
67  void remove(const char* key);
68  void add(const char* key, const uiString&,
69  int enumval, const char* iconfile);
70 
71 protected:
72  void fillUiStrings();
77 public:
78  //Legacy
79  bool isValidName(const char* key) const
80  { return isValidKey(key); }
81  const char* convert(int idx) const { return getKeyForIndex(idx); }
82  int convert(const char* txt) const { return indexOf(txt); }
83 };
84 
85 template <class ENUM>
86 mClass(Basic) EnumDefImpl : public EnumDef
88 public:
89  EnumDefImpl(const char* nm,const char* s[],short nrs=0);
90  bool parse(const char* txt,ENUM& res) const;
91  bool parse(const IOPar& par,const char* key,ENUM& res) const;
92  ENUM parse(const char* txt) const;
93  ENUM getEnumForIndex(int idx) const;
94  const char* getKey(ENUM theenum) const;
95  uiString toUiString(ENUM theenum) const;
96 private:
97  void init();
98 public:
99  //Legacy
100  const char* toString(ENUM theenum) const { return getKey(theenum); }
101 
102 };
103 
104 
258 #define mDeclareEnumUtils(enm) \
259 public: \
260  static const EnumDefImpl<enm>& enm##Def(); \
261  static const char** enm##Names();\
262  static bool parseEnum##enm(const char*,enm&); /*legacy */ \
263  static bool parseEnum(const char*,enm&); \
264  static bool parseEnum(const IOPar&,const char*,enm&); \
265  static enm parseEnum##enm(const char*); \
266  static const char* toString(enm); \
267  static uiString toUiString(enm); \
268  static const char* get##enm##String(enm); /*legacy */ \
269 protected: \
270  static const char* enm##Keys_[]; \
271  static ConstPtrMan<EnumDefImpl<enm> > enm##Definition_; \
272 public:
273 
274 #define mDeclareNameSpaceEnumUtils(mod,enm) \
275  mExtern(mod) const EnumDefImpl<enm>& enm##Def(); \
276  mExtern(mod) const char** enm##Names();\
277  extern const char* enm##Keys_[]; \
278  mExtern(mod) bool parseEnum(const IOPar&,const char*,enm&); \
279  mExtern(mod) bool parseEnum(const char*,enm&); \
280  mExtern(mod) bool parseEnum##enm(const char*,enm&); /*legacy */ \
281  mExtern(mod) enm parseEnum##enm(const char*); \
282  mExtern(mod) const char* toString(enm); \
283  mExtern(mod) uiString toUiString(enm); \
284  mExtern(mod) const char* get##enm##String(enm); /*legacy */
285 
286 #define _DefineEnumNames(prefix,enm,deflen,prettynm) \
287 const EnumDefImpl<prefix::enm>& prefix::enm##Def() \
288 { \
289  if ( !enm##Definition_ ) \
290  { \
291  EnumDefImpl<prefix::enm>* newdef = \
292  new EnumDefImpl<prefix::enm>(prettynm,prefix::enm##Keys_,deflen); \
293  if ( !enm##Definition_.setIfNull( newdef ) ) \
294  delete newdef; \
295  } \
296  \
297  return *enm##Definition_; \
298 } \
299 const char** prefix::enm##Names() \
300 { return prefix::enm##Keys_; } \
301 bool prefix::parseEnum##enm(const char* txt, prefix::enm& res ) \
302 { \
303  const bool isok = prefix::parseEnum( txt, res ); \
304  if ( !isok ) res = (prefix::enm) 0; \
305  return isok; \
306 } \
307 bool prefix::parseEnum(const char* txt, prefix::enm& res ) \
308 { return prefix::enm##Def().parse( txt, res ); } \
309 bool prefix::parseEnum( const IOPar& par, const char* key, prefix::enm& res ) \
310 { return prefix::enm##Def().parse( par, key, res ); } \
311 prefix::enm prefix::parseEnum##enm(const char* txt) \
312 { return prefix::enm##Def().parse( txt ); } \
313 const char* prefix::get##enm##String( prefix::enm theenum ) \
314 { return prefix::toString( theenum ); } \
315 const char* prefix::toString( prefix::enm theenum ) \
316 { return enm##Def().getKey( theenum ); } \
317 uiString prefix::toUiString( prefix::enm theenum ) \
318 { return enm##Def().toUiString( theenum ); } \
319 const char* prefix::enm##Keys_[] =
320 
321 
322 #define DefineEnumNames(clss,enm,deflen,prettynm) \
323 ConstPtrMan<EnumDefImpl<clss::enm> > clss::enm##Definition_ = 0; \
324 _DefineEnumNames( clss, enm, deflen, prettynm )
325 
326 #define DefineNameSpaceEnumNames(nmspc,enm,deflen,prettynm) \
327 static ConstPtrMan<EnumDefImpl<nmspc::enm> > enm##Definition_ = 0; \
328 _DefineEnumNames( nmspc, enm, deflen, prettynm )
329 
330 //New Defs
331 #define mDefineEnumUtils(clss,enm,prettynm) \
332 DefineEnumNames(clss,enm,0,prettynm)
333 
334 #define mDefineNameSpaceEnumUtils(nmspc,enm,prettynm) \
335 DefineNameSpaceEnumNames(nmspc,enm,0,prettynm)
336 
337 //Legacy
338 #define DeclareEnumUtils(enm) mDeclareEnumUtils(enm)
339 #define DefineEnumUtils(clss,enm,prettynm) mDefineEnumUtils(clss,enm,prettynm)
340 #define DefineNameSpaceEnumUtils(nmspc,enm,prettynm) \
341 mDefineNameSpaceEnumUtils(nmspc,enm,prettynm)
342 #define DeclareNameSpaceEnumUtils(mod,enm) mDeclareNameSpaceEnumUtils(mod,enm)
343 
344 #define mEnumTr(str) tr(str)
345 
346 
347 template <class ENUM> inline
348 EnumDefImpl<ENUM>::EnumDefImpl( const char* nm, const char* nms[], short nrs )
349  : EnumDef( nm, nms, nrs )
350 {
351  init();
352  if ( !uistrings_.isEmpty() && uistrings_.size()!=size() )
353  {
354  pErrMsg( "Wrong number of uistrings" );
355  }
356 
357  for ( int idx=uistrings_.size(); idx<size(); idx++ )
358  uistrings_ += ::toUiString( keys_.get(idx) );
359 
360  if ( !iconfiles_.isEmpty() && iconfiles_.size()!=size() )
361  {
362  pErrMsg( "Wrong number of iconfiles" );
363  }
364 }
365 
366 template <class ENUM> inline
367 bool EnumDefImpl<ENUM>::parse(const char* txt, ENUM& res ) const
368 {
369  const int idx = isValidKey( txt )
370  ? indexOf( txt )
371  : -1;
372  if ( idx<0 )
373  return false;
374 
375  res = (ENUM) enums_[idx];
376  return true;
377 }
378 
379 
380 template <class ENUM> inline
381 bool EnumDefImpl<ENUM>::parse( const IOPar& par, const char* key,
382  ENUM& res ) const
383 {
384  const char* val = par.find( key );
385  return parse( val, res );
386 }
387 
388 
389 template <class ENUM> inline
391 {
392  return (ENUM) enums_[idx];
393 }
394 
395 
396 template <class ENUM> inline
397 ENUM EnumDefImpl<ENUM>::parse(const char* txt) const
398 {
399  return getEnumForIndex( indexOf( txt ) );
400 }
401 
402 
403 template <class ENUM> inline
404 const char* EnumDefImpl<ENUM>::getKey( ENUM theenum ) const
405 {
406  if ( !size() )
407  return 0;
408 
409  const int idx = enums_.indexOf( (int) theenum );
410  return keys_.get(idx>=0?idx:0).buf();
411 }
412 
413 
414 template <class ENUM> inline
416 {
417  const int idx = enums_.indexOf( (int) theenum );
418  return getUiStringForIndex( idx );
419 }
420 
421 
422 template <class ENUM> inline
424 {
425  fillUiStrings();
426 }
427 
428 
429 #endif
#define mExpClass(module)
Definition: commondefs.h:160
const char * convert(int idx) const
Definition: enums.h:81
#define mODTextTranslationClass(clss)
Definition: uistring.h:38
Export_Basic uiString toUiString(ViewStyle)
int size() const
void fillUiStrings()
BufferStringSet iconfiles_
Definition: enums.h:76
bool init()
int convert(const char *txt) const
Definition: enums.h:82
Definition: uistring.h:89
bool isEmpty() const
Definition: odset.h:45
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
int indexOf(const char *s) const
Definition: enums.h:86
const char * getKey(ENUM theenum) const
Definition: enums.h:404
const BufferStringSet & keys() const
Definition: enums.h:58
bool isValidKey(const char *) const
size_type size() const
Definition: objectset.h:50
Generalized set of parameters of the keyword-value type.
Definition: iopar.h:47
ENUM getEnumForIndex(int idx) const
Definition: enums.h:390
const char * toString(ENUM theenum) const
Definition: enums.h:100
const char * find(const char *) const
returns null if not found
const uiStringSet & strings() const
Definition: enums.h:59
uiString getUiStringForIndex(int i) const
EnumDefImpl(const char *nm, const char *s[], short nrs=0)
Definition: enums.h:348
uiStringSet uistrings_
Definition: enums.h:73
void init()
Definition: enums.h:423
const char * buf() const
Definition: odstring.h:47
size_type size() const
Definition: typeset.h:254
Object with a name.
Definition: namedobj.h:35
uiString toUiString(ENUM theenum) const
Definition: enums.h:415
TypeSet< int > enums_
Definition: enums.h:75
Definition: uistring.h:218
BufferString & get(int idx)
Definition: bufstringset.h:36
bool isValidName(const char *key) const
Definition: enums.h:79
BufferStringSet keys_
Definition: enums.h:74
bool parse(const char *txt, ENUM &res) const
Definition: enums.h:367
#define mClass(module)
Definition: commondefs.h:164
#define pErrMsg(msg)
Definition: errmsg.h:60
virtual size_type indexOf(T, bool forward=true, size_type start=-1) const
Definition: typeset.h:311
Holds data pertinent to a certain enum. It does not know the enum values themselves, but treat them as integers.
Definition: enums.h:45

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