xref: /freebsd/lib/libdevdctl/guid.h (revision 12a88a3d637e7e7a3726e8dedbcf82cb96cea529)
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  * $FreeBSD$
337a0c41d5SAlan Somers  */
347a0c41d5SAlan Somers 
357a0c41d5SAlan Somers /**
367a0c41d5SAlan Somers  * \file devdctl_guid.h
377a0c41d5SAlan Somers  *
387a0c41d5SAlan Somers  * Definition of the Guid class.
397a0c41d5SAlan Somers  */
407a0c41d5SAlan Somers #ifndef	_DEVDCTL_GUID_H_
417a0c41d5SAlan Somers #define	_DEVDCTL_GUID_H_
427a0c41d5SAlan Somers 
437a0c41d5SAlan Somers /*============================ Namespace Control =============================*/
447a0c41d5SAlan Somers namespace DevdCtl
457a0c41d5SAlan Somers {
467a0c41d5SAlan Somers 
477a0c41d5SAlan Somers /*============================= Class Definitions ============================*/
487a0c41d5SAlan Somers /*----------------------------------- Guid -----------------------------------*/
497a0c41d5SAlan Somers /**
507a0c41d5SAlan Somers  * \brief Object that represents guids.
517a0c41d5SAlan Somers  *
527a0c41d5SAlan Somers  * It can generally be manipulated as a uint64_t, but with a special
537a0c41d5SAlan Somers  * value INVALID_GUID that does not equal any valid guid.
547a0c41d5SAlan Somers  *
557a0c41d5SAlan Somers  * As of this writing, this class is only used to represent ZFS
567a0c41d5SAlan Somers  * guids in events and spa_generate_guid() in spa_misc.c explicitly
577a0c41d5SAlan Somers  * refuses to return a guid of 0.  So this class uses 0 as the value
587a0c41d5SAlan Somers  * for INVALID_GUID.  In the future, if 0 is allowed to be a valid
597a0c41d5SAlan Somers  * guid, the implementation of this class must change.
607a0c41d5SAlan Somers  */
617a0c41d5SAlan Somers class Guid
627a0c41d5SAlan Somers {
637a0c41d5SAlan Somers public:
647a0c41d5SAlan Somers 	/* Constructors */
657a0c41d5SAlan Somers 	Guid(uint64_t guid);
667a0c41d5SAlan Somers 	Guid(const std::string &guid);
67*12a88a3dSAlan Somers 	static Guid InvalidGuid();
687a0c41d5SAlan Somers 
697a0c41d5SAlan Somers 	/* Assignment */
707a0c41d5SAlan Somers 	Guid& operator=(const Guid& rhs);
717a0c41d5SAlan Somers 
727a0c41d5SAlan Somers 	/* Test the validity of this guid. */
737a0c41d5SAlan Somers 	bool IsValid()			 const;
747a0c41d5SAlan Somers 
757a0c41d5SAlan Somers 	/* Comparison to other Guid operators */
767a0c41d5SAlan Somers 	bool operator==(const Guid& rhs) const;
777a0c41d5SAlan Somers 	bool operator!=(const Guid& rhs) const;
787a0c41d5SAlan Somers 
797a0c41d5SAlan Somers 	/* Integer conversion operators */
807a0c41d5SAlan Somers 	operator uint64_t()		 const;
817a0c41d5SAlan Somers 	operator bool()			 const;
827a0c41d5SAlan Somers 
837a0c41d5SAlan Somers protected:
84*12a88a3dSAlan Somers 	static const uint64_t INVALID_GUID = 0;
85*12a88a3dSAlan Somers 
867a0c41d5SAlan Somers 	/* The integer value of the GUID. */
877a0c41d5SAlan Somers 	uint64_t  m_GUID;
887a0c41d5SAlan Somers };
897a0c41d5SAlan Somers 
907a0c41d5SAlan Somers //- Guid Inline Public Methods ------------------------------------------------
917a0c41d5SAlan Somers inline
927a0c41d5SAlan Somers Guid::Guid(uint64_t guid)
937a0c41d5SAlan Somers   : m_GUID(guid)
947a0c41d5SAlan Somers {
957a0c41d5SAlan Somers }
967a0c41d5SAlan Somers 
97*12a88a3dSAlan Somers inline Guid
98*12a88a3dSAlan Somers Guid::InvalidGuid()
99*12a88a3dSAlan Somers {
100*12a88a3dSAlan Somers 	return (Guid(INVALID_GUID));
101*12a88a3dSAlan Somers }
102*12a88a3dSAlan Somers 
1037a0c41d5SAlan Somers inline Guid&
1047a0c41d5SAlan Somers Guid::operator=(const Guid &rhs)
1057a0c41d5SAlan Somers {
1067a0c41d5SAlan Somers 	m_GUID = rhs.m_GUID;
1077a0c41d5SAlan Somers 	return (*this);
1087a0c41d5SAlan Somers }
1097a0c41d5SAlan Somers 
1107a0c41d5SAlan Somers inline bool
1117a0c41d5SAlan Somers Guid::IsValid() const
1127a0c41d5SAlan Somers {
1137a0c41d5SAlan Somers 	return (m_GUID != INVALID_GUID);
1147a0c41d5SAlan Somers }
1157a0c41d5SAlan Somers 
1167a0c41d5SAlan Somers inline bool
1177a0c41d5SAlan Somers Guid::operator==(const Guid& rhs) const
1187a0c41d5SAlan Somers {
1197a0c41d5SAlan Somers 	return (m_GUID == rhs.m_GUID);
1207a0c41d5SAlan Somers }
1217a0c41d5SAlan Somers 
1227a0c41d5SAlan Somers inline bool
1237a0c41d5SAlan Somers Guid::operator!=(const Guid& rhs) const
1247a0c41d5SAlan Somers {
1257a0c41d5SAlan Somers 	return (m_GUID != rhs.m_GUID);
1267a0c41d5SAlan Somers }
1277a0c41d5SAlan Somers 
1287a0c41d5SAlan Somers inline
1297a0c41d5SAlan Somers Guid::operator uint64_t() const
1307a0c41d5SAlan Somers {
1317a0c41d5SAlan Somers 	return (m_GUID);
1327a0c41d5SAlan Somers }
1337a0c41d5SAlan Somers 
1347a0c41d5SAlan Somers inline
1357a0c41d5SAlan Somers Guid::operator bool() const
1367a0c41d5SAlan Somers {
1377a0c41d5SAlan Somers 	return (m_GUID != INVALID_GUID);
1387a0c41d5SAlan Somers }
1397a0c41d5SAlan Somers 
1407a0c41d5SAlan Somers /** Convert the GUID into its string representation */
1417a0c41d5SAlan Somers std::ostream& operator<< (std::ostream& out, Guid g);
1427a0c41d5SAlan Somers 
1437a0c41d5SAlan Somers } // namespace DevdCtl
1447a0c41d5SAlan Somers #endif /* _DEVDCTL_GUID_H_ */
145