File: id.h

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (55 lines) | stat: -rw-r--r-- 1,554 bytes parent folder | download | duplicates (2)
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
#pragma once

#include <iosfwd>

namespace util {

/**
 * @brief A generic class for creating type safe handle types
 *
 * This type should be used when a function returns a handle to an internal resource instead of using an int.
 * Example:
 * @code{.cpp}
 * struct gamesnd_id_tag{};
 * typedef util::ID<gamesnd_id_tag, int, -1> gamesnd_id;
 * @endcode
 *
 * The implementation value should be very light-weight since this class is designed to be passed around by-value.
 *
 * @note This class was copied from http://www.ilikebigbits.com/blog/2014/5/6/type-safe-identifiers-in-c
 *
 * @tparam Tag A unique type tag which is used for distinguishing between two types which use the same Impl type
 * @tparam Impl The internal representation of the handle.
 * @tparam default_value The default value of the handle. If the handle has this value it is considered invalid.
 */
template<class Tag, class Impl, Impl default_value>
class ID
{
public:
	typedef Tag tag_type;
	typedef Impl impl_type;

	static ID invalid() { return ID(); }

	// Defaults to ID::invalid()
	ID() : m_val(default_value) { }

	// Explicit constructor:
	explicit ID(Impl val) : m_val(val) { }

	inline Impl value() const { return m_val; }

	friend bool operator==(ID a, ID b) { return a.m_val == b.m_val; }
	friend bool operator!=(ID a, ID b) { return a.m_val != b.m_val; }
	friend std::ostream& operator<< (std::ostream& stream, const ID& id) {
		stream << id.value();
		return stream;
	}

	inline bool isValid() const { return m_val != default_value; }

protected:
	Impl m_val;
};

}