xref: /freebsd/lib/libdevdctl/guid.h (revision b3e76948)
17a0c41d5SAlan Somers /*-
27a0c41d5SAlan Somers  * Copyright (c) 2012, 2013 Spectra Logic Corporation
37a0c41d5SAlan Somers  * All rights reserved.
47a0c41d5SAlan Somers  *
57a0c41d5SAlan Somers  * Redistribution and use in source and binary forms, with or without
67a0c41d5SAlan Somers  * modification, are permitted provided that the following conditions
77a0c41d5SAlan Somers  * are met:
87a0c41d5SAlan Somers  * 1. Redistributions of source code must retain the above copyright
97a0c41d5SAlan Somers  *    notice, this list of conditions, and the following disclaimer,
107a0c41d5SAlan Somers  *    without modification.
117a0c41d5SAlan Somers  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127a0c41d5SAlan Somers  *    substantially similar to the "NO WARRANTY" disclaimer below
137a0c41d5SAlan Somers  *    ("Disclaimer") and any redistribution must be conditioned upon
147a0c41d5SAlan Somers  *    including a substantially similar Disclaimer requirement for further
157a0c41d5SAlan Somers  *    binary redistribution.
167a0c41d5SAlan Somers  *
177a0c41d5SAlan Somers  * NO WARRANTY
187a0c41d5SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
197a0c41d5SAlan Somers  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
207a0c41d5SAlan Somers  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
217a0c41d5SAlan Somers  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
227a0c41d5SAlan Somers  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
237a0c41d5SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
247a0c41d5SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
257a0c41d5SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
267a0c41d5SAlan Somers  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
277a0c41d5SAlan Somers  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
287a0c41d5SAlan Somers  * POSSIBILITY OF SUCH DAMAGES.
297a0c41d5SAlan Somers  *
307a0c41d5SAlan Somers  * Authors: Alan Somers         (Spectra Logic Corporation)
317a0c41d5SAlan Somers  */
327a0c41d5SAlan Somers 
337a0c41d5SAlan Somers /**
347a0c41d5SAlan Somers  * \file devdctl_guid.h
357a0c41d5SAlan Somers  *
367a0c41d5SAlan Somers  * Definition of the Guid class.
377a0c41d5SAlan Somers  */
387a0c41d5SAlan Somers #ifndef	_DEVDCTL_GUID_H_
397a0c41d5SAlan Somers #define	_DEVDCTL_GUID_H_
407a0c41d5SAlan Somers 
417a0c41d5SAlan Somers /*============================ Namespace Control =============================*/
427a0c41d5SAlan Somers namespace DevdCtl
437a0c41d5SAlan Somers {
447a0c41d5SAlan Somers 
457a0c41d5SAlan Somers /*============================= Class Definitions ============================*/
467a0c41d5SAlan Somers /*----------------------------------- Guid -----------------------------------*/
477a0c41d5SAlan Somers /**
487a0c41d5SAlan Somers  * \brief Object that represents guids.
497a0c41d5SAlan Somers  *
507a0c41d5SAlan Somers  * It can generally be manipulated as a uint64_t, but with a special
517a0c41d5SAlan Somers  * value INVALID_GUID that does not equal any valid guid.
527a0c41d5SAlan Somers  *
537a0c41d5SAlan Somers  * As of this writing, this class is only used to represent ZFS
547a0c41d5SAlan Somers  * guids in events and spa_generate_guid() in spa_misc.c explicitly
557a0c41d5SAlan Somers  * refuses to return a guid of 0.  So this class uses 0 as the value
567a0c41d5SAlan Somers  * for INVALID_GUID.  In the future, if 0 is allowed to be a valid
577a0c41d5SAlan Somers  * guid, the implementation of this class must change.
587a0c41d5SAlan Somers  */
597a0c41d5SAlan Somers class Guid
607a0c41d5SAlan Somers {
617a0c41d5SAlan Somers public:
627a0c41d5SAlan Somers 	/* Constructors */
6333d9904aSAlan Somers 	/* Default constructor: an Invalid guid */
6433d9904aSAlan Somers 	Guid();
6533d9904aSAlan Somers 	/* Construct a guid from a provided integer */
667a0c41d5SAlan Somers 	Guid(uint64_t guid);
6733d9904aSAlan Somers 	/* Construct a guid from a string in base 8, 10, or 16 */
687a0c41d5SAlan Somers 	Guid(const std::string &guid);
6912a88a3dSAlan Somers 	static Guid InvalidGuid();
707a0c41d5SAlan Somers 
717a0c41d5SAlan Somers 	/* Test the validity of this guid. */
727a0c41d5SAlan Somers 	bool IsValid()			 const;
737a0c41d5SAlan Somers 
747a0c41d5SAlan Somers 	/* Comparison to other Guid operators */
757a0c41d5SAlan Somers 	bool operator==(const Guid& rhs) const;
767a0c41d5SAlan Somers 	bool operator!=(const Guid& rhs) const;
777a0c41d5SAlan Somers 
787a0c41d5SAlan Somers 	/* Integer conversion operators */
797a0c41d5SAlan Somers 	operator uint64_t()		 const;
807a0c41d5SAlan Somers 	operator bool()			 const;
817a0c41d5SAlan Somers 
827a0c41d5SAlan Somers protected:
8312a88a3dSAlan Somers 	static const uint64_t INVALID_GUID = 0;
8412a88a3dSAlan Somers 
857a0c41d5SAlan Somers 	/* The integer value of the GUID. */
867a0c41d5SAlan Somers 	uint64_t  m_GUID;
877a0c41d5SAlan Somers };
887a0c41d5SAlan Somers 
897a0c41d5SAlan Somers //- Guid Inline Public Methods ------------------------------------------------
907a0c41d5SAlan Somers inline
Guid()9133d9904aSAlan Somers Guid::Guid()
9233d9904aSAlan Somers   : m_GUID(INVALID_GUID)
9333d9904aSAlan Somers {
9433d9904aSAlan Somers }
9533d9904aSAlan Somers 
9633d9904aSAlan Somers inline
Guid(uint64_t guid)977a0c41d5SAlan Somers Guid::Guid(uint64_t guid)
987a0c41d5SAlan Somers   : m_GUID(guid)
997a0c41d5SAlan Somers {
1007a0c41d5SAlan Somers }
1017a0c41d5SAlan Somers 
10212a88a3dSAlan Somers inline Guid
InvalidGuid()10312a88a3dSAlan Somers Guid::InvalidGuid()
10412a88a3dSAlan Somers {
10512a88a3dSAlan Somers 	return (Guid(INVALID_GUID));
10612a88a3dSAlan Somers }
10712a88a3dSAlan Somers 
1087a0c41d5SAlan Somers inline bool
IsValid()1097a0c41d5SAlan Somers Guid::IsValid() const
1107a0c41d5SAlan Somers {
1117a0c41d5SAlan Somers 	return (m_GUID != INVALID_GUID);
1127a0c41d5SAlan Somers }
1137a0c41d5SAlan Somers 
1147a0c41d5SAlan Somers inline bool
1157a0c41d5SAlan Somers Guid::operator==(const Guid& rhs) const
1167a0c41d5SAlan Somers {
1177a0c41d5SAlan Somers 	return (m_GUID == rhs.m_GUID);
1187a0c41d5SAlan Somers }
1197a0c41d5SAlan Somers 
1207a0c41d5SAlan Somers inline bool
1217a0c41d5SAlan Somers Guid::operator!=(const Guid& rhs) const
1227a0c41d5SAlan Somers {
1237a0c41d5SAlan Somers 	return (m_GUID != rhs.m_GUID);
1247a0c41d5SAlan Somers }
1257a0c41d5SAlan Somers 
1267a0c41d5SAlan Somers inline
uint64_t()1277a0c41d5SAlan Somers Guid::operator uint64_t() const
1287a0c41d5SAlan Somers {
1297a0c41d5SAlan Somers 	return (m_GUID);
1307a0c41d5SAlan Somers }
1317a0c41d5SAlan Somers 
1327a0c41d5SAlan Somers inline
1337a0c41d5SAlan Somers Guid::operator bool() const
1347a0c41d5SAlan Somers {
1357a0c41d5SAlan Somers 	return (m_GUID != INVALID_GUID);
1367a0c41d5SAlan Somers }
1377a0c41d5SAlan Somers 
1387a0c41d5SAlan Somers /** Convert the GUID into its string representation */
1397a0c41d5SAlan Somers std::ostream& operator<< (std::ostream& out, Guid g);
1407a0c41d5SAlan Somers 
1417a0c41d5SAlan Somers } // namespace DevdCtl
1427a0c41d5SAlan Somers #endif /* _DEVDCTL_GUID_H_ */
143