Structural deformable models
SpeciesDB.h
Go to the documentation of this file.
1 /* species database -*- C++ -*- */
2 #ifndef _SPECIESDB_H_
3 #define _SPECIESDB_H_
4 
5 #include <iostream>
6 #include <exception>
7 #include <map>
8 #include <list>
9 #include "common.h"
10 #include "ParseFile.h"
11 
12 class Species : public std::map<std::string, std::string> {
13 public:
14  static const char* SFIELDS[];
15  const static char s_SPFLAGS[];
16  enum SpFlags {
17  FLIPPED=0x01, QUALITY=0x02, SCALE=0x04, SEL1=0x08, DEL=0x10,
18  SEL2=0x20, SEL3=0x04, SEL4=0x80, SEL5=0x100
19  };
21  Species(dword _id=0, dword _flags=0) : id(_id), m_Flags(_flags) {}
22  Species &operator=(const Species &rhs) {
23  if(&rhs == this) return *this;
24  std::map<std::string, std::string>::operator=(rhs);
25  id = rhs.id;
26  m_Flags = rhs.m_Flags;
27  return *this;
28  }
29  void reset();
30  bool hasFlag(dword flag) const;
31  dword setFlag(dword flag);
32  dword unsetFlag(dword flag);
33  dword readFlagString(const std::string &flags);
34  std::string getFlagString() const;
35  bool select(const Species& sp,
36  enum SpCompare how=Species::CMP_CONTAINS) const;
37  dword getScale() const;
38  friend ParseFile& operator>>(ParseFile &is, Species &sp);
39  friend std::ostream& operator<<(std::ostream &os, const Species &sp);
40 
43 };
44 
45 class SpeciesDB : public std::map<dword, Species> {
46 public:
47  SpeciesDB() {}
48  SpeciesDB(const char* filename) {
49  load(filename);
50  }
51  bool load(const char* filename);
52 
53  friend ParseFile& operator>>(ParseFile &is, SpeciesDB &sp);
54  friend std::ostream& operator<<(std::ostream &os, const SpeciesDB &sp);
55  const std::string& getDirectory() const { return m_Directory; }
56  const std::string& getFilename() const { return m_Filename; }
57  dword select(const Species& rhs,
59  dword select(const std::list<dword>& idlist);
60  dword getSelectionID(int dir=0, bool wrap=true);
61  dword loadSelection(const std::string& fname)
62  { return loadSelection(fname, m_Selection); }
63  dword writeSelection(const std::string& fname) const
64  { return writeSelection(fname, m_Selection); }
65  static dword loadSelection(const std::string& fname,std::list<dword>& sel);
66  static dword writeSelection(const std::string& fname,
67  const std::list<dword>& sel);
68 protected:
69  std::string m_Directory;
70  std::string m_Filename;
71  std::list<dword> m_Selection;
72  std::list<dword>::iterator m_CSel;
73 };
74 
78 class DBSelector {
79 public:
80  DBSelector(SpeciesDB &db) : m_DB(db), m_CSel(m_Selectors.begin()) {};
81  DBSelector(SpeciesDB &db, const char* filename)
82  : m_DB(db), m_CSel(m_Selectors.begin())
83  { load(filename); }
86  bool load(const char* filename);
88  void update();
91  {
92  std::map<dword,Species>::iterator n = m_DB.find(
93  m_DB.getSelectionID());
94  if(n != m_DB.end()) return n->second;
95  else return m_CSpecies;
96  }
101  bool nextSelection(int dir=1, bool wrap=true);
106  bool nextSelector(int walkdir=1, bool wrap=true, dword mincount=1);
107 
108 public:
110  std::list<Species> m_Selectors;
111  std::list<Species>::iterator m_CSel;
113 };
114 
115 #ifdef _SP_DEFINE_
116 const char* Species::SFIELDS[] = {
117  "species_id",
118  "order",
119  "family",
120  "name",
121  "cname",
122  "type",
123  "image",
124  "flags",
125  "scale",
126  NULL
127 };
128 const char Species::s_SPFLAGS[] = "fqs1d2345\0x00";
129 #endif
130 
131 #endif
SpeciesDB()
Definition: SpeciesDB.h:47
#define NULL
Definition: simpletypes.h:9
DBSelector(SpeciesDB &db, const char *filename)
Definition: SpeciesDB.h:81
dword readFlagString(const std::string &flags)
Definition: SpeciesDB.cpp:189
DBSelector(SpeciesDB &db)
Definition: SpeciesDB.h:80
SpeciesDB & m_DB
Definition: SpeciesDB.h:109
dword id
Definition: SpeciesDB.h:41
const std::string & getDirectory() const
Definition: SpeciesDB.h:55
std::list< Species > m_Selectors
Definition: SpeciesDB.h:110
bool hasFlag(dword flag) const
Definition: SpeciesDB.cpp:169
dword writeSelection(const std::string &fname) const
Definition: SpeciesDB.h:63
SpeciesDB(const char *filename)
Definition: SpeciesDB.h:48
static const char * SFIELDS[]
Definition: SpeciesDB.h:14
dword m_Flags
Definition: SpeciesDB.h:42
dword setFlag(dword flag)
Definition: SpeciesDB.cpp:173
const std::string & getFilename() const
Definition: SpeciesDB.h:56
Species & operator=(const Species &rhs)
Definition: SpeciesDB.h:22
friend ParseFile & operator>>(ParseFile &is, Species &sp)
Definition: SpeciesDB.cpp:128
dword loadSelection(const std::string &fname)
Definition: SpeciesDB.h:61
std::list< dword > m_Selection
Definition: SpeciesDB.h:71
unsigned long dword
Definition: simpletypes.h:6
Species m_CSpecies
Definition: SpeciesDB.h:112
dword getScale() const
Definition: SpeciesDB.cpp:229
std::list< Species >::iterator m_CSel
Definition: SpeciesDB.h:111
static const char s_SPFLAGS[]
Definition: SpeciesDB.h:15
bool select(const Species &sp, enum SpCompare how=Species::CMP_CONTAINS) const
Definition: SpeciesDB.cpp:197
std::string getFlagString() const
Definition: SpeciesDB.cpp:181
std::string m_Directory
Definition: SpeciesDB.h:69
dword unsetFlag(dword flag)
Definition: SpeciesDB.cpp:177
Species(dword _id=0, dword _flags=0)
Definition: SpeciesDB.h:21
Species & getCurSpecies()
Returns a handle to the currently selected species in the data base.
Definition: SpeciesDB.h:90
std::list< dword >::iterator m_CSel
Definition: SpeciesDB.h:72
std::string m_Filename
Definition: SpeciesDB.h:70
friend std::ostream & operator<<(std::ostream &os, const Species &sp)
Definition: SpeciesDB.cpp:149
void reset()
Definition: SpeciesDB.cpp:163