VCluster_object_array.hpp 2.88 KB
Newer Older
incardon's avatar
incardon 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
/*
 * Vcluster_object_array.hpp
 *
 *  Created on: Feb 4, 2015
 *      Author: Pietro Incardona
 */

#ifndef VCLUSTER_OBJECT_ARRAY_HPP_
#define VCLUSTER_OBJECT_ARRAY_HPP_

#include <vector>
#include "VObject.hpp"

/*! \brief Encapsulate any object created by the Virtual cluster machine
 *
 * \tparam original object
 *
 */

template<typename T>
class Vcluster_object_array : public VObject
{
	std::vector<T> objects;

public:

	/*! \brief Constructor of object array
	 *
	 */
	Vcluster_object_array()
	{

	}

	/*! \brief Return the size of the objects array
	 *
	 * \return the size of the array
	 *
	 */
	size_t size()
	{
		return objects.size();
	}

	/*! \brief Return the element i
	 *
	 * \return a reference to the object i
	 *
	 */

	T & get(unsigned int i)
	{
		return objects[i];
	}

	/*! \brief Check if this Object is an array
	 *
	 * \return true, it is an array
	 *
	 */
	bool isArray()
	{
		return true;
	}

	/*! \brief Destroy the object
	 *
	 */
	virtual void destroy()
	{
		// Destroy the objects
		objects.clear();
	}

	/*! \brief Get the size of the memory needed to pack the object
	 *
	 * \return the size of the message to pack the object
	 *
	 */
	size_t packObjectSize()
	{
		size_t message = 0;

		// Destroy each objects
		for (size_t i = 0 ; i < objects.size() ; i++)
		{
			message += objects[i].packObjectSize();
		}

		return message;
	}


	/*! \brief Get the size of the memory needed to pack the object
	 *
	 * \param Memory where to write the packed object
	 *
	 * \return the size of the message to pack the object
	 *
	 */
	size_t packObject(void * mem)
	{
		// Pointer is zero
		size_t ptr = 0;
		unsigned char * m = (unsigned char *)mem;

		// pack each object
		for (size_t i = 0 ; i < objects.size() ; i++)
		{
			ptr += objects[i].packObject(&m[ptr]);
		}

#ifdef DEBUG
		if (ptr != packObjectSize())
		{
			std::cerr << "Error " << __FILE__ << " " << __LINE__ << " the pack object size does not match the message" << "\n";
		}
#endif

		return ptr;
	}

	/*! \brief Calculate the size to pack an object in the array
	 *
	 * \param array object index
	 *
	 */
	size_t packObjectInArraySize(size_t i)
	{
		return objects[i].packObjectSize();
	}

	/*! \brief pack the object in the array (the message produced can be used to move one)
	 * object from one processor to another
	 *
	 * \param i index of the object to pack
	 * \param p Memory of the packed object message
	 *
	 */
	size_t packObjectInArray(size_t i, void * p)
	{
		return objects[i].packObject(p);
	}

	/*! \brief Destroy an object from the array
	 *
	 * \param i object to destroy
	 *
	 */
	void destroy(size_t i)
	{
		objects.erase(objects.begin() + i);
	}

	/*! \brief Return the object j in the array
	 *
	 * \param j element j
	 *
	 */
	T & operator[](size_t j)
	{
		return objects[j];
	}

	/*! \brief Resize the array
	 *
	 * \param size
	 *
	 */
	void resize(size_t n)
	{
		objects.resize(n);
	}
};


#endif /* VCLUSTER_OBJECT_HPP_ */