/*
 * expressionserializer.h
 *
 *  Created on: Jan 4, 2016
 *      Author: pgess
 */

#ifndef SRC_EXPRESSIONSERIALIZER_H_
#define SRC_EXPRESSIONSERIALIZER_H_

#include "ast.h"
#include <string>
#include <boost/optional.hpp>

namespace xreate {

struct PackedExpression{
	PackedExpression(){};
	PackedExpression(PackedExpression&& other);
	~PackedExpression();
	void operator<< (const std::pair<size_t, size_t>& value);
	char* operator*(){return __storage;}
	bool operator==(const PackedExpression& other) const;
	bool operator!=(const PackedExpression& other) const;
	bool operator<(const PackedExpression& other) const;
	private:
		PackedExpression (const PackedExpression&)=delete;
		PackedExpression& operator=(const PackedExpression&)=delete;
		PackedExpression& operator=(PackedExpression&&)=delete;

		char* __storage = nullptr;
		size_t size =0;
		unsigned char countRemainedBits =0;
};
typedef boost::optional<PackedExpression> OptionalPackedExpression;

class ExpressionSerializerPrivate;

class ExpressionSerializer {
public:
	ExpressionSerializer();
	~ExpressionSerializer();
	ExpressionSerializer(ExpressionSerializer&& other)
		: strategy(other.strategy), registry(std::move(other.registry)) 	{}
	void registerExpression(const Expression&e);
	PackedExpression pack(const Expression& e);
	OptionalPackedExpression packOptional(const Expression& e) const;

private:
	ExpressionSerializerPrivate* strategy;
	std::map<PackedExpression, size_t> registry;
};

} /* namespace xreate */

#endif /* SRC_EXPRESSIONSERIALIZER_H_ */
