xref: /freebsd/lib/libdevdctl/guid.h (revision f5147e312f43a9050468de539aeafa072caa1a60)
1 /*-
2  * Copyright (c) 2012, 2013 Spectra Logic Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  *
30  * Authors: Alan Somers         (Spectra Logic Corporation)
31  *
32  * $FreeBSD$
33  */
34 
35 /**
36  * \file devdctl_guid.h
37  *
38  * Definition of the Guid class.
39  */
40 #ifndef	_DEVDCTL_GUID_H_
41 #define	_DEVDCTL_GUID_H_
42 
43 /*============================ Namespace Control =============================*/
44 namespace DevdCtl
45 {
46 
47 /*============================= Class Definitions ============================*/
48 /*----------------------------------- Guid -----------------------------------*/
49 /**
50  * \brief Object that represents guids.
51  *
52  * It can generally be manipulated as a uint64_t, but with a special
53  * value INVALID_GUID that does not equal any valid guid.
54  *
55  * As of this writing, this class is only used to represent ZFS
56  * guids in events and spa_generate_guid() in spa_misc.c explicitly
57  * refuses to return a guid of 0.  So this class uses 0 as the value
58  * for INVALID_GUID.  In the future, if 0 is allowed to be a valid
59  * guid, the implementation of this class must change.
60  */
61 class Guid
62 {
63 public:
64 	/* Constructors */
65 	/* Default constructor: an Invalid guid */
66 	Guid();
67 	/* Construct a guid from a provided integer */
68 	Guid(uint64_t guid);
69 	/* Construct a guid from a string in base 8, 10, or 16 */
70 	Guid(const std::string &guid);
71 	static Guid InvalidGuid();
72 
73 	/* Assignment */
74 	Guid& operator=(const Guid& rhs);
75 
76 	/* Test the validity of this guid. */
77 	bool IsValid()			 const;
78 
79 	/* Comparison to other Guid operators */
80 	bool operator==(const Guid& rhs) const;
81 	bool operator!=(const Guid& rhs) const;
82 
83 	/* Integer conversion operators */
84 	operator uint64_t()		 const;
85 	operator bool()			 const;
86 
87 protected:
88 	static const uint64_t INVALID_GUID = 0;
89 
90 	/* The integer value of the GUID. */
91 	uint64_t  m_GUID;
92 };
93 
94 //- Guid Inline Public Methods ------------------------------------------------
95 inline
96 Guid::Guid()
97   : m_GUID(INVALID_GUID)
98 {
99 }
100 
101 inline
102 Guid::Guid(uint64_t guid)
103   : m_GUID(guid)
104 {
105 }
106 
107 inline Guid
108 Guid::InvalidGuid()
109 {
110 	return (Guid(INVALID_GUID));
111 }
112 
113 inline Guid&
114 Guid::operator=(const Guid &rhs)
115 {
116 	m_GUID = rhs.m_GUID;
117 	return (*this);
118 }
119 
120 inline bool
121 Guid::IsValid() const
122 {
123 	return (m_GUID != INVALID_GUID);
124 }
125 
126 inline bool
127 Guid::operator==(const Guid& rhs) const
128 {
129 	return (m_GUID == rhs.m_GUID);
130 }
131 
132 inline bool
133 Guid::operator!=(const Guid& rhs) const
134 {
135 	return (m_GUID != rhs.m_GUID);
136 }
137 
138 inline
139 Guid::operator uint64_t() const
140 {
141 	return (m_GUID);
142 }
143 
144 inline
145 Guid::operator bool() const
146 {
147 	return (m_GUID != INVALID_GUID);
148 }
149 
150 /** Convert the GUID into its string representation */
151 std::ostream& operator<< (std::ostream& out, Guid g);
152 
153 } // namespace DevdCtl
154 #endif /* _DEVDCTL_GUID_H_ */
155