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