diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-11-21 08:59:17 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-11-21 08:59:17 +0100 |
commit | 4dedb85d2408c90dea1d3c36d8f3a985a4b26024 (patch) | |
tree | fbf8f2c2eab47267abb3737bb19318ccd29e38d9 /src | |
parent | 6c8ee8084a8fa8317be69f5578d9b1052aee3b70 (diff) |
working on ManagedContainers.hpp
Diffstat (limited to 'src')
-rw-r--r-- | src/core/ManagedContainers.hpp | 360 | ||||
-rw-r--r-- | src/core/dom/Typesystem.hpp | 87 |
2 files changed, 447 insertions, 0 deletions
diff --git a/src/core/ManagedContainers.hpp b/src/core/ManagedContainers.hpp new file mode 100644 index 0000000..c1787b4 --- /dev/null +++ b/src/core/ManagedContainers.hpp @@ -0,0 +1,360 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef _OUSIA_MANAGED_CONTAINERS_H_ +#define _OUSIA_MANAGED_CONTAINERS_H_ + +#include "Managed.hpp" + +namespace ousia { + +/** + * Template class which can be used to collect "Owned" refrences to a certain + * type of managed object. Do not use this class directly, use ManagedMap or + * ManagedVector instead. This class only provides functionality which is common + * to list and map containers (iterators and state). + * + * @param T is the type of the Managed object that should be managed. + * @param Collection should be a STL container of Owned<T> + */ +template <class T, class Collection> +class ManagedContainer { +public: + using collection_type = Collection; + using value_type = typename collection_type::value_type; + using reference = typename collection_type::reference; + using const_reference = typename collection_type::const_reference; + using iterator = typename collection_type::iterator; + using const_iterator = typename collection_type::const_iterator; + using size_type = typename collection_type::size_type; + +protected: + /** + * Handle containing a reference to the owner of the collection. + */ + Handle<Managed> owner; + + /** + * Underlying STL collection. + */ + collection_type c; + +protected: + /** + * Function which can be overridden by child classes to execute special code + * whenever a new element is added to the collection. + */ + virtual void addManaged(value_type h) {} + + /** + * Function which can be overriden by child classes to execute special code + * whenever an element is removed from the collection. + */ + virtual void deleteManaged(value_type h) {} + +public: + /** + * Constructor of the ManagedContainer class. + * + * @param owner is the managed object which owns the collection and all + * handles to other managed objects stored within. + */ + ManagedContainer(Handle<Managed> owner) : owner(owner){}; + + /* State functions */ + size_type size() const noexcept { return c.size(); } + bool empty() const noexcept { return c.empty(); } + + /* Iterators */ + iterator begin() { return c.begin(); } + iterator end() { return c.end(); } + + iterator rbegin() { return c.rbegin(); } + iterator rend() { return c.rend(); } + + const_iterator begin() const { return c.cbegin(); } + const_iterator end() const { return c.cend(); } + + const_iterator cbegin() const { return c.cbegin(); } + const_iterator cend() const { return c.cend(); } + + const_iterator rbegin() const { return c.crbegin(); } + const_iterator rend() const { return c.crend(); } + + const_iterator crbegin() const { return c.crbegin(); } + const_iterator crend() const { return c.crend(); } + + /* Clear function */ + void clear() noexcept + { + for (const_iterator it = cbegin(); it != cend(); it++) { + deleteManaged(*it); + } + c.clear(); + } +}; + +/** + * Template class which can be used to collect "Owned" refrences to a certain + * type of managed object. This class should be used in favour of other + * collections of handles, it takes care of acquiring an owned handle from the + * owner of this collection whenever a new element is added. + * + * @param T is the type of the Managed object that should be managed. + * @param Collection should be a STL list container of Owned<T> + */ +template <class T, class Collection> +class ManagedGenericList : public ManagedContainer() { +public: + using ManagedContainer<T, Collection>::ManagedContainer; + + /** + * Initialize with an iterator from another collection. + * + * @param owner is the managed object which owns the collection and all + * handles to other managed objects stored within. + * @param first is an iterator pointing at the first element to be copied + * from some other collection. + * @param last is an iterator pointing at the last element to be copied + * from some other collection. + */ + template <class InputIterator> + ManagedGenericList(Handle<Managed> owner, InputIterator first, InputIterator last) + : ManagedContainer<T, Collection>(owner) + { + insert(c.begin(), first, last); + } + + /** + * Initialize with another collection. + * + * @param owner is the managed object which owns the collection and all + * handles to other managed objects stored within. + * @param in is a reference at some other collection with content that + * should be copied. + */ + template <class InputCollection> + ManagedGenericList(Handle<Managed> owner, const InputCollection &in) + : ManagedContainer<T, Collection>(owner) + { + for (const auto &e : in) { + push_back(e); + } + } + + /* Front and back */ + reference front() { return c.front(); } + const_reference front() const { return c.front(); } + reference back() { return c.back(); } + const_reference back() const { return c.back(); } + + /* Insert and delete operations */ + + iterator insert(const_iterator position, Handle<T> h) + { + value_type v = owner->acquire(h); + addManaged(v); + return c.insert(position, owner->acquire(h)); + } + + template <class InputIterator> + iterator insert(const_iterator position, InputIterator first, + InputIterator last) + { + bool first = true; + const_iterator pos = position; + for (InputIterator it = first; it != last; it++) { + if (first) { + first = false; + } else { + pos++; + } + pos = insert(pos, *it); + } + return pos; + } + + iterator find(const Handle<T> h) + { + for (iterator it = begin(); it != end(); it++) { + if (*it == h) { + return it; + } + } + return end(); + } + + const_iterator find(const Handle<T> h) const + { + for (const_iterator it = cbegin(); it != cend(); it++) { + if (*it == h) { + return it; + } + } + return cend(); + } + + void push_back(Handle<T> h) + { + Rooted<T> rooted{h}; + addManaged(rooted); + c.push_back(owner->acquire(rooted)); + } + + void pop_back() + { + if (!empty()) { + deleteElement(c.back()); + } + c.pop_back(); + } + + iterator erase(iterator position) + { + deleteManaged(*position); + return c.erase(position); + } + + iterator erase(iterator first, iterator last) + { + for (const_iterator it = first; it != last; it++) { + deleteManaged(*it); + } + return c.erase(first, last); + } +}; + +/** + * Special type of ManagedContainer based on an STL map. + */ +template <class K, class T, class Collection> +class ManagedGenericMap : public ManagedCollection<T, Collection> { +private: + value_type acquirePair(std::pair<K, Handle<T>> val) + { + return std::pair<const K, T>{val->first, owner->acquire(val->second)}; + } + +public: + + /** + * Initialize with an iterator from another collection. + * + * @param owner is the managed object which owns the collection and all + * handles to other managed objects stored within. + * @param first is an iterator pointing at the first element to be copied + * from some other collection. + * @param last is an iterator pointing at the last element to be copied + * from some other collection. + */ + template <class InputIterator> + ManagedGenericMap(Handle<Managed> owner, InputIterator first, InputIterator last) + : ManagedContainer<T, Collection>(owner) + { + insert(first, last); + } + + /** + * Initialize with another collection. + * + * @param owner is the managed object which owns the collection and all + * handles to other managed objects stored within. + * @param in is a reference at some other collection with content that + * should be copied. + */ + template <class InputCollection> + ManagedGenericMap(Handle<Managed> owner, const InputCollection &in) + : ManagedContainer<T, Collection>(owner) + { + for (const auto &e : in) { + insert(*in); + } + } + + std::pair<iterator, bool> insert(std::pair<K, Handle<T>> val) + { + value_type v = acquirePair(val); + addManaged(v); + return c.insert(v); + } + + iterator insert(const_iterator position, std::pair<K, Handle<T>> val) + { + value_type v = acquirePair(val); + addManaged(v); + return c.insert(position, v); + } + + template <class InputIterator> + void insert(InputIterator first, InputIterator last) + { + for (auto it = first; it != last; it++) { + insert(acquirePair); + } + } + + iterator erase(const_iterator position) + { + deleteManaged(*position); + return c.erase(position); + } + + size_t erase(const key_type &k) + { + iterator pos = find(k); + if (pos != end()) { + erase(pos); + return 1; + } + return 0; + } + + iterator erase(const_iterator first, const_iterator last) + { + for (const_iterator it = first; it != last; it++) { + deleteManaged(*it); + } + return c.erase(first, last); + } + + iterator find(const key_type &k) { return c.find(k); } + const_iterator find(const key_type &k) const { return c.find(k); } +}; + +/** + * Special type of ManagedGenericList based on an STL vector. + */ +template <class T> +class ManagedVector : public ManagedGenericList<T, std::vector<Owned<T>>> { +public: + using ManagedContainer<T, std::vector<Owned<T>>>::ManagedContainer; +}; + +/** + * Special type of ManagedGenericMap based on an STL map. + */ +template <class K, class T> +class ManagedMap : public ManagedGenericMap<K, T, std::map<K, Owned<T>>> { +public: + using ManagedGenericMap<K, T, std::map<K, Owned<T>>>::ManagedMap; +}; + +} + +#endif /* _OUSIA_MANAGED_CONTAINERS_H_ */ + diff --git a/src/core/dom/Typesystem.hpp b/src/core/dom/Typesystem.hpp new file mode 100644 index 0000000..201aed2 --- /dev/null +++ b/src/core/dom/Typesystem.hpp @@ -0,0 +1,87 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef _OUSIA_DOM_TYPESYSTEM_HPP_ +#define _OUSIA_DOM_TYPESYSTEM_HPP_ + +#include <string> +#include <vector> + +#include <core/Managed.hpp> + +#include "Node.hpp" + +namespace ousia { +namespace dom { + +class Type; + +class TypeInstance : public Managed { +public: + const Owned<Type> type; + + TypeInstance(Manager &mgr, Handle<Type> type) + : Managed(mgr), type(acquire(type)) + { + } +}; + +class Type : public Node { +public: + using Node::Node; + + virtual bool isFinal() const { return true; } + + virtual bool isPrimitive() const { return true; } + + virtual Rooted<TypeInstance> create() = 0; + + virtual Rooted<TypeInstance> parse(const std::string &str) = 0; +}; + +class Typesystem : public Node { +private: + const std::vector<Owned<Type>> types; + const std::vector<Owned<TypeInstance>> constants; + +protected: + void doResolve(std::vector<Rooted<Node>> &res, + const std::vector<std::string> &path, Filter filter, + void *filterData, unsigned idx, + VisitorSet &visited) override; + +public: + using Node::Node; + + const &std::vector<Owned<Type>> getTypes() { return types; } + + const &std::vector<Owned<TypeInstance>> getConstants() { return constants; } + + void addType(Handle<Type> type) { + types.push_back(acquire(type)); + } + + void addConstant(Handle<TypeInstance> ) { + + } +}; +} +} + +#endif /* _OUSIA_DOM_TYPESYSTEM_HPP_ */ + |