ARB
ps_pg_specmap.hxx
Go to the documentation of this file.
1 #ifndef PS_PG_SPECMAP_HXX
2 #define PS_PG_SPECMAP_HXX
3 
4 // -----------------------------------------
5 // mapping shortname <-> SpeciesID
6 
9 static bool __MAPS_INITIALIZED = false;
10 
12 
13  GB_transaction ta(pb_main);
14 
16 
17  // look for existing mapping in pb-db:
18  GBDATA *pb_mapping = GB_entry(pb_main, "species_mapping");
19  if (!pb_mapping) { // error
20  GB_export_error("No species mapping");
21  }
22  else {
23  // retrieve mapping from string
24  const char *mapping = GB_read_char_pntr(pb_mapping);
25  if (!mapping) return GB_export_error("Can't read mapping");
26 
27  while (mapping[0]) {
28  const char *comma = strchr(mapping, ','); if (!comma) break;
29  const char *semicolon = strchr(comma, ';'); if (!semicolon) break;
30  string name(mapping, comma-mapping);
31  comma+=1;
32  string idnum(comma, semicolon-comma);
33  SpeciesID id = atoi(idnum.c_str());
34 
35  __NAME2ID_MAP[name] = id;
36  __ID2NAME_MAP[id] = name;
37 
38  mapping = semicolon+1;
39  }
40  }
41 
42  __MAPS_INITIALIZED = true;
43  return NULp;
44 }
45 
46 SpeciesID PG_SpeciesName2SpeciesID(const string& shortname) {
47  ps_assert(__MAPS_INITIALIZED); // you didn't call PG_initSpeciesMaps
48  return __NAME2ID_MAP[shortname];
49 }
50 
51 inline const string& PG_SpeciesID2SpeciesName(SpeciesID num) {
52  ps_assert(__MAPS_INITIALIZED); // you didn't call PG_initSpeciesMaps
53  return __ID2NAME_MAP[num];
54 }
55 
56 static int PG_NumberSpecies() {
57  return __ID2NAME_MAP.size();
58 }
59 
60 #else
61 #error ps_pg_specmap.hxx included twice
62 #endif // PS_PG_SPECMAP_HXX
const char * GB_ERROR
Definition: arb_core.h:25
const char * id
Definition: AliAdmin.cxx:17
static Name2IDMap __NAME2ID_MAP
int SpeciesID
Definition: ps_defs.hxx:17
static ID2NameMap __ID2NAME_MAP
GB_ERROR GB_export_error(const char *error)
Definition: arb_msg.cxx:259
static GB_ERROR PG_initSpeciesMaps(GBDATA *pb_main)
const string & PG_SpeciesID2SpeciesName(SpeciesID num)
SpeciesID PG_SpeciesName2SpeciesID(const string &shortname)
std::map< std::string, SpeciesID > Name2IDMap
Definition: ps_defs.hxx:22
#define NULp
Definition: cxxforward.h:97
#define ps_assert(cond)
Definition: ps_assert.hxx:18
GB_transaction ta(gb_var)
GB_CSTR GB_read_char_pntr(GBDATA *gbd)
Definition: arbdb.cxx:874
static int PG_NumberSpecies()
static bool __MAPS_INITIALIZED
std::map< SpeciesID, std::string > ID2NameMap
Definition: ps_defs.hxx:25
GBDATA * GB_entry(GBDATA *father, const char *key)
Definition: adquery.cxx:334