Debian Codegraph @

avogadro/core/coordinateset.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_CORE_COORDINATESET_H
7 #define AVOGADRO_CORE_COORDINATESET_H
8
9 #include "avogadrocore.h"
10
11 #include <typeinfo>
12 #include <vector>
13
14 namespace Avogadro::Core {
15
16 /**
17 * @class ArraySet coordinateset.h <avogadro/core/coordinateset.h>
18 * @brief Base class for array type containers.
19 *
20 * This base class gives us the non-templated base class that stores all arrays
21 * of data. You should use the base class as the container, and figure out its
22 * type when using it.
23 */
24 class ArraySet
25 {
26 public:
27 ArraySet() : m_content(nullptr), m_data(nullptr) {}
28 virtual ~ArraySet() { delete m_content; }
29
30 /** @return true if the type of the array matches the input type. */
31 template <typename T>
32 bool isType(const T&) const
33 {
34 return m_content ? typeid(T) == m_content->type() : false;
35 }
36 /*
37 template<typename T>
38 std::vector<T> data()
39 {
40 if (m_data && isType(T))
41 return static_cast<std::vector<T> &>(*m_data);
42 else
43 return std::vector<T>();
44 }
45 */
46 protected:
47 class PlaceHolder
48 {
49 public:
50 virtual ~PlaceHolder() {}
51
52 virtual const std::type_info& type() const = 0;
53
54 virtual PlaceHolder* clone() const = 0;
55 };
56
57 template <typename ValueType>
58 class Holder : public PlaceHolder
59 {
60 public:
61 Holder(const ValueType& value) : m_content(value) {}
62
63 const std::type_info& type() const override { return typeid(ValueType); }
64
65 PlaceHolder* clone() const override { return new Holder(m_content); }
66
67 ValueType m_content;
68 };
69
70 PlaceHolder* m_content;
71 void* m_data;
72 };
73
74 /**
75 * @class CoordinateSet coordinateset.h <avogadro/core/coordinateset.h>
76 * @brief Templated class for array type containers.
77 *
78 * This class gives us the derived templated class that stores arrays of data.
79 * of data. This class should be used to store concrete arrays, and can be
80 * cast to ArraySet when stored in generic containers.
81 */
82 template <typename T>
83 class CoordinateSet : public ArraySet
84 {
85 public:
86 CoordinateSet() { m_content = new Holder<T>(T()); }
87
88 ~CoordinateSet() override = default;
89
90 // Properties
91 void resize(Index _size) { m_coordinates.resize(_size); }
92 Index size() const { return m_coordinates.size(); }
93 std::vector<T>& coordinates() { return m_coordinates; }
94 const std::vector<T>& coordinates() const { return m_coordinates; }
95
96 // Inline operator methods.
97 /** Returns the element at \index _index. */
98 T operator()(Index _index) const { return m_coordinates.at(_index); }
99 T& operator[](Index _index) { return m_coordinates[_index]; }
100 const T& operator[](Index _index) const { return m_coordinates[_index]; }
101
102 private:
103 std::vector<T> m_coordinates;
104 };
105
106 } // namespace Avogadro::Core
107
108 #endif // AVOGADRO_CORE_COORDINATESET_H