[80a6a52] | 1 | #pragma once
|
---|
| 2 |
|
---|
| 3 | #include <exception>
|
---|
| 4 |
|
---|
| 5 | #include "chemelem.h"
|
---|
| 6 | #include "mindbtypes.h"
|
---|
| 7 |
|
---|
| 8 | namespace geoworld
|
---|
| 9 | {
|
---|
| 10 | class MineralsDB : public IMineralsDatabase
|
---|
| 11 | {
|
---|
| 12 | private:
|
---|
| 13 | typedef std::map< std::string, mars::ptr< MineralClass > > ClassesMap;
|
---|
| 14 |
|
---|
| 15 | public:
|
---|
| 16 | typedef std::set< mars::ptr< MineralClass > > ClassesSet;
|
---|
| 17 |
|
---|
| 18 | class Ex : public std::exception
|
---|
| 19 | {
|
---|
| 20 | private:
|
---|
| 21 | const char * _message;
|
---|
| 22 |
|
---|
| 23 | public:
|
---|
| 24 | Ex (const char * szMsg) : _message(szMsg) {}
|
---|
| 25 |
|
---|
| 26 | virtual const char * what() const noexcept { return this->_message; }
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | template< typename MAP >
|
---|
| 30 | class MapIteratorWrapper : public std::iterator < std::input_iterator_tag, typename MAP::value_type >
|
---|
| 31 | {
|
---|
| 32 | private:
|
---|
| 33 | typename MAP::const_iterator _i;
|
---|
| 34 |
|
---|
| 35 | public:
|
---|
| 36 | MapIteratorWrapper (const typename MAP::const_iterator & i)
|
---|
| 37 | : _i (i) {}
|
---|
| 38 |
|
---|
| 39 | inline MapIteratorWrapper & operator ++ ()
|
---|
| 40 | {
|
---|
| 41 | ++_i;
|
---|
| 42 | return *this;
|
---|
| 43 | }
|
---|
| 44 | inline MapIteratorWrapper & operator ++ (int)
|
---|
| 45 | {
|
---|
| 46 | MapIteratorWrapper old = *this;
|
---|
| 47 |
|
---|
| 48 | _i++;
|
---|
| 49 | return old;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | inline const typename MAP::value_type & operator * () const { return *_i; }
|
---|
| 53 | inline const typename MAP::value_type & operator -> () const { return *_i; }
|
---|
| 54 | };
|
---|
| 55 | typedef MapIteratorWrapper< ClassesMap > classes_const_iterator;
|
---|
| 56 |
|
---|
| 57 | class GenesisMineralMapping
|
---|
| 58 | {
|
---|
| 59 | public:
|
---|
| 60 | MineralReference context;
|
---|
| 61 | mars::ptr< MineralDef > mineral;
|
---|
| 62 | MineralDef::GenesisList::const_iterator genesis;
|
---|
| 63 |
|
---|
| 64 | GenesisMineralMapping(const MineralReference & ctx, const mars::ptr< MineralDef > & pMin, const MineralDef::GenesisList::const_iterator & iGen)
|
---|
| 65 | : context(ctx), mineral(pMin), genesis(iGen) {}
|
---|
| 66 |
|
---|
| 67 | inline bool operator < (const GenesisMineralMapping & other) const
|
---|
| 68 | {
|
---|
| 69 | return
|
---|
| 70 | mineral < other.mineral ||
|
---|
| 71 | (mineral == other.mineral && context < other.context) || // TODO: Check use of MineralDef::GenesisList::const_iterator::_Ptr
|
---|
| 72 | (mineral == other.mineral && context == other.context && &genesis < &other.genesis);
|
---|
| 73 | }
|
---|
| 74 | };
|
---|
| 75 | typedef std::set< GenesisMineralMapping > PetrogenyMineralSet;
|
---|
| 76 |
|
---|
| 77 | class GenesisClassMapping
|
---|
| 78 | {
|
---|
| 79 | public:
|
---|
| 80 | mars::ptr< MineralDef > mineral;
|
---|
| 81 | MineralDef::GenesisList::const_iterator genesis;
|
---|
| 82 |
|
---|
| 83 | GenesisClassMapping(const mars::ptr< MineralDef > & mineral, const MineralDef::GenesisList::const_iterator iGen)
|
---|
| 84 | : mineral(mineral), genesis(iGen) {}
|
---|
| 85 |
|
---|
| 86 | inline bool operator < (const GenesisClassMapping & other) const
|
---|
| 87 | {
|
---|
| 88 | return mineral < other.mineral || (mineral == other.mineral && &genesis < &genesis);
|
---|
| 89 | }
|
---|
| 90 | };
|
---|
| 91 | typedef std::set< GenesisClassMapping > PetrogenyClassSet;
|
---|
| 92 |
|
---|
| 93 | class ClassesSetFacade
|
---|
| 94 | {
|
---|
| 95 | private:
|
---|
| 96 | const ClassesSet & _classset;
|
---|
| 97 |
|
---|
| 98 | public:
|
---|
| 99 | ClassesSetFacade (const ClassesSet & classes)
|
---|
| 100 | : _classset (classes) {}
|
---|
| 101 |
|
---|
| 102 | inline ClassesSet::const_iterator begin() const
|
---|
| 103 | { return _classset.begin(); }
|
---|
| 104 | inline ClassesSet::const_iterator end() const
|
---|
| 105 | { return _classset.end(); }
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 | class PetrogenyMineralSetFacade
|
---|
| 109 | {
|
---|
| 110 | private:
|
---|
| 111 | const PetrogenyMineralSet & _petroset;
|
---|
| 112 |
|
---|
| 113 | public:
|
---|
| 114 | PetrogenyMineralSetFacade (const PetrogenyMineralSet & petroset)
|
---|
| 115 | : _petroset(petroset) {}
|
---|
| 116 |
|
---|
| 117 | inline PetrogenyMineralSet::const_iterator begin() const
|
---|
| 118 | { return _petroset.begin(); }
|
---|
| 119 | inline PetrogenyMineralSet::const_iterator end() const
|
---|
| 120 | { return _petroset.end(); }
|
---|
| 121 | };
|
---|
| 122 |
|
---|
| 123 | class PetrogenyClassSetFacade
|
---|
| 124 | {
|
---|
| 125 | private:
|
---|
| 126 | const PetrogenyClassSet & _petroset;
|
---|
| 127 |
|
---|
| 128 | public:
|
---|
| 129 | PetrogenyClassSetFacade (const PetrogenyClassSet & petroset)
|
---|
| 130 | : _petroset(petroset) {}
|
---|
| 131 |
|
---|
| 132 | inline PetrogenyClassSet::const_iterator begin() const
|
---|
| 133 | { return _petroset.begin(); }
|
---|
| 134 | inline PetrogenyClassSet::const_iterator end() const
|
---|
| 135 | { return _petroset.end(); }
|
---|
| 136 | };
|
---|
| 137 |
|
---|
| 138 | mars::ptr< MineralDef > addMineral (const mars::ptr< MineralDef > & ptr);
|
---|
| 139 | mars::ptr< MineralClass > addClass (const mars::ptr< MineralClass > & ptr);
|
---|
| 140 | mars::ptr< MineralGroupDef > addGroup (const mars::ptr< MineralGroupDef > & ptr);
|
---|
| 141 |
|
---|
| 142 | void resolveReferences();
|
---|
| 143 | void createIndexes ();
|
---|
| 144 | inline void init ()
|
---|
| 145 | {
|
---|
| 146 | resolveReferences();
|
---|
| 147 | createIndexes();
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | const mars::ptr< MineralDef > getMinByName (const std::string & sName) const;
|
---|
| 151 | const mars::ptr< MineralClass > getClassByName (const std::string & sName) const;
|
---|
| 152 |
|
---|
| 153 | classes_const_iterator beginClasses (const MineralClass::Type enmcType) const;
|
---|
| 154 | classes_const_iterator endClasses (const MineralClass::Type enmcType) const;
|
---|
| 155 |
|
---|
| 156 | inline ClassesSetFacade getClassesBySilica (const Silica ensSilica) const
|
---|
| 157 | { return ClassesSetFacade (_classes.silica[ensSilica]); }
|
---|
| 158 |
|
---|
| 159 | PetrogenyMineralSetFacade getAssociatedMinerals (const std::string & sName) const;
|
---|
| 160 | PetrogenyMineralSetFacade getAssociatedMinerals (const MineralPtr & pMin) const;
|
---|
| 161 |
|
---|
| 162 | PetrogenyMineralSetFacade getIncubatedMinerals (const std::string & sName) const;
|
---|
| 163 | PetrogenyMineralSetFacade getIncubatedMinerals (const MineralPtr & pMin) const;
|
---|
| 164 |
|
---|
| 165 | PetrogenyMineralSetFacade getHostedMinerals (const std::string & sName) const;
|
---|
| 166 | PetrogenyMineralSetFacade getHostedMinerals (const MineralPtr & pMin) const;
|
---|
| 167 |
|
---|
| 168 | PetrogenyClassSetFacade getMineralsByEnvironment (const std::string & sName) const;
|
---|
| 169 | PetrogenyClassSetFacade getMineralsByEnvironment (const MinClassPtr & pClass) const;
|
---|
| 170 |
|
---|
| 171 | private:
|
---|
| 172 | typedef std::map< std::string, mars::ptr< MineralDef > > MineralMap;
|
---|
| 173 | typedef std::map< std::string, mars::ptr< MineralGroupDef > > MineralGroups;
|
---|
| 174 | typedef std::map< MineralPtr, PetrogenyMineralSet > PetrogenyMineralMultiMap;
|
---|
| 175 | typedef std::map< MinClassPtr, PetrogenyClassSet > PetrogenyClassMultiMap;
|
---|
| 176 |
|
---|
| 177 | typedef std::list< mars::ptr< MineralDef > > DirectMineralList;
|
---|
| 178 | typedef std::map< std::string, DirectMineralList > MineralGroupMap;
|
---|
| 179 |
|
---|
| 180 | MineralMap _minerals;
|
---|
| 181 | struct Groups
|
---|
| 182 | {
|
---|
| 183 | MineralGroups all;
|
---|
| 184 | MineralGroupMap classes;
|
---|
| 185 | } _groups;
|
---|
| 186 | PetrogenyMineralMultiMap _associations,
|
---|
| 187 | _protoliths,
|
---|
| 188 | _hosts;
|
---|
| 189 |
|
---|
| 190 | struct Classes
|
---|
| 191 | {
|
---|
| 192 | ClassesMap all, igneous, sedimentary;
|
---|
| 193 | ClassesSet silica[CountSilica];
|
---|
| 194 | } _classes;
|
---|
| 195 | PetrogenyClassMultiMap _environments;
|
---|
| 196 |
|
---|
| 197 | PetrogenyMineralSet _endMineralPetrogeny;
|
---|
| 198 | PetrogenyClassSet _endClassPetrogeny;
|
---|
| 199 |
|
---|
| 200 | void resolve( mars::WeakReference< MineralDef > & rWRef );
|
---|
| 201 | void resolve( mars::WeakReference< MineralClass > & rWRef );
|
---|
| 202 | void resolve( MineralReferenceList & list );
|
---|
| 203 | void resolve( MinClassReferenceList & list );
|
---|
| 204 | void resolveClassGroups ();
|
---|
| 205 |
|
---|
| 206 | static inline const std::string & getName (const MineralPtr & ptr)
|
---|
| 207 | {
|
---|
| 208 | if (ptr.getState() == MineralPtr::Weak)
|
---|
| 209 | return ptr.getName();
|
---|
| 210 | else
|
---|
| 211 | return ptr->name;
|
---|
| 212 | }
|
---|
| 213 | };
|
---|
| 214 | }
|
---|