Debian Codegraph @

avogadro/qtgui/persistentatom.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_QTGUI_PERSISTENTATOM_H
7 #define AVOGADRO_QTGUI_PERSISTENTATOM_H
8
9 #include <avogadro/core/avogadrocore.h>
10
11 namespace Avogadro {
12 namespace QtGui {
13
14 /**
15 * @class PersistentAtom persistentatom.h <avogadro/qtcore/persistentatom.h>
16 * @brief The PersistentAtom object provides a container for a persistent atom
17 * reference that can be held onto. The atom() method gets the underlying atom
18 * using the unique ID mechanism of the molecule.
19 */
20 template <typename Molecule_T>
21 class PersistentAtom
22 {
23 public:
24 typedef Molecule_T MoleculeType;
25 typedef typename Molecule_T::AtomType AtomType;
26
27 /**
28 * @brief Create a persistent atom, with the specified unique id.
29 * @param m The molecule the persistent atom belongs to.
30 * @param uniqueId The unique identifier for the atom.
31 */
32 explicit PersistentAtom(MoleculeType* m = nullptr, Index uniqueId = MaxIndex)
33 : m_molecule(m), m_uniqueId(uniqueId)
34 {
35 }
36
37 /**
38 * @brief Create a persistent atom from a standard atom object.
39 * @param a The atom that a persistent reference should be created for.
40 */
41 explicit PersistentAtom(const AtomType& a);
42
43 /**
44 * @brief Set the molecule and unique ID for the persistent object.
45 * @param m The molecule that contains the atom.
46 * @param uniqueId The unique ID of the atom.
47 */
48 void set(MoleculeType* m, Index uniqueId);
49
50 /**
51 * @brief Set the persistent atom from a standard atom object.
52 * @param a The atom that a persistent reference should be created for.
53 */
54 void set(const AtomType& a);
55
56 /**
57 * @brief Reset the object to an invalid state.
58 */
59 void reset();
60
61 /**
62 * @return True if the persistent atom is valid.
63 */
64 bool isValid() const;
65
66 /**
67 * @return The molecule the atom is a part of.
68 */
69 MoleculeType* molecule() const { return m_molecule; }
70
71 /**
72 * @brief The persistent unique ID of the atom.
73 * @return The unique ID of the atom.
74 */
75 Index uniqueIdentifier() const { return m_uniqueId; }
76
77 /**
78 * @brief Obtain the atom being held by the persistent object.
79 * @return A reference to the atom held by the object.
80 */
81 AtomType atom() const;
82
83 private:
84 MoleculeType* m_molecule;
85 Index m_uniqueId;
86 };
87
88 template <typename Molecule_T>
89 PersistentAtom<Molecule_T>::PersistentAtom(const AtomType& a)
90 : m_molecule(dynamic_cast<MoleculeType*>(a.molecule()))
91 {
92 m_uniqueId = m_molecule ? m_molecule->atomUniqueId(a) : MaxIndex;
93 }
94
95 template <typename Molecule_T>
96 void PersistentAtom<Molecule_T>::set(MoleculeType* m, Index uniqueId)
97 {
98 m_molecule = m;
99 m_uniqueId = uniqueId;
100 }
101
102 template <typename Molecule_T>
103 void PersistentAtom<Molecule_T>::set(const AtomType& a)
104 {
105 m_molecule = dynamic_cast<MoleculeType*>(a.molecule());
106 m_uniqueId = m_molecule ? m_molecule->atomUniqueId(a) : MaxIndex;
107 }
108
109 template <typename Molecule_T>
110 void PersistentAtom<Molecule_T>::reset()
111 {
112 set(nullptr, MaxIndex);
113 }
114
115 template <typename Molecule_T>
116 bool PersistentAtom<Molecule_T>::isValid() const
117 {
118 return atom().isValid();
119 }
120
121 template <typename Molecule_T>
122 typename Molecule_T::AtomType PersistentAtom<Molecule_T>::atom() const
123 {
124 return m_molecule ? m_molecule->atomByUniqueId(m_uniqueId) : AtomType();
125 }
126
127 } // namespace QtGui
128 } // namespace Avogadro
129
130 #endif // AVOGADRO_QTGUI_PERSISTENTATOM_H