resource_ownership_set.hpp 3.34 KB
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#ifndef __RESOURCE_OWNERSHIP_SET_HPP__
#define __RESOURCE_OWNERSHIP_SET_HPP__
#include "resource_ownership.hpp"
#include "utils.hpp"
#include <vector>

using std::vector;

template <typename T>
class Resource_ownership_set
{
	private:
		vector<Resource_ownership<T> > _set;

	public:
		Resource_ownership_set(const vector<T>& resources, bool exclusive = false)
		{
			build_from(resources, exclusive);
		}

		Resource_ownership_set(){}

		Resource_ownership_set(const Resource_ownership_set& orig)
		{
			_set.assign(orig._set.begin(), orig._set.end());
		}

		~Resource_ownership_set(){}

		Resource_ownership_set& operator= (const Resource_ownership_set& orig)
		{
			if(this == &orig)
				return *this;
			_set.assign(orig._set.begin(), orig._set.end());
			return *this;
		}

		void build_from(const vector<T>& resources, bool exclusive = false)
		{
			_set.clear();
			for(int i=0; i<(int)resources.size(); i++)
				_set.push_back(Resource_ownership<int>(resources[i], exclusive));
		}

		int size() {return (int)_set.size();}

		void clear() {_set.clear();}

		Resource_ownership<T>& operator[](int index){return _set[index];}

		Resource_ownership<T>* find_resource_ownership(const T& res)
		{
			for(int i=0; i<(int)_set.size(); i++)
				if(_set[i].get_resource() == res)
					return &_set[i];
			return NULL;
		}

		void add(const Resource_ownership<T>& r)
		{
			add_object<Resource_ownership<T> >(_set, r);
		}

		void add(const T& r)
		{
			add_object<Resource_ownership<T> >(Resource_ownership<T>(r));
		}

		void remove(const Resource_ownership<T>& r)
		{
			remove_object<Resource_ownership<T> >(_set, r);
		}

		void remove(const T& r)
		{
			remove_object<Resource_ownership<T> >(_set, Resource_ownership<T>(r));
		}

		void reclaim(T res)
		{
			Resource_ownership<T>* ro = find_resource_ownership(res);
			THROW_IF(!ro, "Trying to reclaim a resource that does not exist");
			ro->decrement_owners();
		}

		void reclaim(vector<T> res)
		{
			for(int i=0; i<(int)res.size(); i++)
				reclaim(res[i]);
		}

		void grant(T res, bool is_exclusive)
		{
			Resource_ownership<T>* ro = find_resource_ownership(res);
			THROW_IF(!ro, "Trying to grant a resource that does not exist");
			ro->increment_owners();
			ro->set_exclusive(is_exclusive);
		}

		void grant(vector<T> res, bool is_exclusive)
		{
			for(int i=0; i<(int)res.size(); i++)
				grant(res[i], is_exclusive);
		}

		void transfer_to(const T &res, 
				Resource_ownership_set<T> &destination)
		{
			Resource_ownership<T> *_ro = find_resource_ownership(res);
			assert(_ro);
			THROW_IF(!_ro, "Resource does not exist");

			//Dirty ugly hack. Will be fixed later on with move assignment/constructor
			Resource_ownership<T> ro(*_ro); //must happen before the remove
			remove(ro);
			ro.reset();
			destination.add(ro);
		}

		void transfer_to(const vector<T> &res, 
				Resource_ownership_set<T> &destination)
		{
			for(int i=0; i<(int)res.size(); i++)
				transfer_to(res[i], destination);
		}

		bool are_unused(const vector<T> &res)
		{
			Resource_ownership<T> *ro;
			for(int i=0; i<(int)res.size(); i++)
			{
				ro = find_resource_ownership(res[i]);
				assert(ro);
				THROW_IF(!ro, "Resource does not exist");
				if(ro->is_used())
					return false;
			}
			return true;
		}

		void get_resources(vector<T> & res)
		{
			res.clear();
			for(int i=0; i<(int)_set.size(); i++)
				res.push_back(_set[i].get_resource());
		}
};
#endif