Blob Blame Raw
#ifndef ADDRESS_H
#define ADDRESS_H


#include <cstring>

#include <string>


enum : ErrorCode  {
	ERR_ADDRESS_COMMON = ERR_ADDRESS,
	ERR_ADDRESS_INCORRECT,
	ERR_ADDRESS_NOT_FOUND,
};


class Address {
public:
	enum Type {
		NONE,
		COMMON_STRING,
		SOCKET,
	};
	
	Type type;
	std::string text;
	size_t size;
	char data[512];
	
	inline Address(): type(NONE), size(), data() { }
	
	inline void clear()
		{ type = NONE; text.clear(); size = 0; memset(data, 0, sizeof(data)); }
	
	inline void set(Type type, const char *text = nullptr) {
		this->type = type;
		if (text) this->text = text; else this->text.clear();
		size = 0;
		memset(data, 0, sizeof(data));
	}
	
	template<typename T>
	inline void set(Type type, const char *text, const T& data)
		{ set(type, text); as<T>() = data; size = sizeof(T); }
	
	template<typename T>
	inline const T& as() const
		{ assert(sizeof(T) < sizeof(data)); return *reinterpret_cast<const T*>(data); }
	
	template<typename T>
	inline T& as()
		{ assert(sizeof(T) < sizeof(data)); return *reinterpret_cast<T*>(data); }
};


#endif