Loading...   

  • Created By Uleat On: 10:20 PM February 08, 2018
  • Link

[*.h]
template <class T, size_t S>
struct CommonUseArray {
	static_assert(sizeof(T) > 0, "Undefined types are not allowed in CommonUseArray<T>.");
	static_assert(S > 0, "Zero-sized arrays are not allowed in CommonUseArray<T>.");

	inline size_t sizeof_array() const { return S; }
	inline size_t sizeof_type() const { return sizeof(T); }
	inline size_t sizeof_data() const { return (sizeof(T) * S); }
	
	T Data[S];
};

template<size_t S>
struct CommonUseArray <char, S> {
	static_assert(S > 0, "Zero-sized arrays are not allowed in CommonUseArray<char>.");

	inline size_t sizeof_array() const { return S; }
	inline size_t sizeof_type() const { return sizeof(char); }
	inline size_t sizeof_data() const { return (sizeof(char) * S); }

	inline size_t str_capacity() const { return (S - 1); }
	inline void str_safe() { Data[S - 1] = '\0'; }
	
	void mem_cpy(const char* r);
	void str_cpy(const char* r);
	void str_n_cpy(const char* r);

	char Data[S];
};

---
[*.cpp]
template<size_t S>
void CommonUseArray <char, S>::mem_cpy(const char* r) {
	int pos = 0;
	bool read = (r != nullptr);

	while (pos < S) {
		if (read)
			Data[pos] = r[pos];
		else
			Data[pos] = '\0';
		
		++pos;
	}
}

template<size_t S>
void CommonUseArray <char, S>::str_cpy(const char* r) {
	int pos = 0;
	
	if (r != nullptr) {
		while (pos < (S - 1)) {
			Data[pos] = r[pos];
			
			if (r[pos] == '\0')
				break;

			++pos;
		}
	}
	else {
		Data[0] = '\0';
	}

	str_safe();
}

template<size_t S>
void CommonUseArray <char, S>::str_n_cpy(const char* r) {
	int pos = 0;
	bool read = (r != nullptr);

	while (pos < (S - 1)) {
		if (read)
			Data[pos] = r[pos];
		else
			Data[pos] = '\0';

		if (r[pos] == '\0')
			read = false;

		++pos;
	}

	str_safe();
}

Raw Paste Data