1300906Sasomers/*- 2300906Sasomers * Copyright (c) 2012, 2013 Spectra Logic Corporation 3300906Sasomers * All rights reserved. 4300906Sasomers * 5300906Sasomers * Redistribution and use in source and binary forms, with or without 6300906Sasomers * modification, are permitted provided that the following conditions 7300906Sasomers * are met: 8300906Sasomers * 1. Redistributions of source code must retain the above copyright 9300906Sasomers * notice, this list of conditions, and the following disclaimer, 10300906Sasomers * without modification. 11300906Sasomers * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12300906Sasomers * substantially similar to the "NO WARRANTY" disclaimer below 13300906Sasomers * ("Disclaimer") and any redistribution must be conditioned upon 14300906Sasomers * including a substantially similar Disclaimer requirement for further 15300906Sasomers * binary redistribution. 16300906Sasomers * 17300906Sasomers * NO WARRANTY 18300906Sasomers * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19300906Sasomers * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20300906Sasomers * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 21300906Sasomers * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22300906Sasomers * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23300906Sasomers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24300906Sasomers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25300906Sasomers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26300906Sasomers * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27300906Sasomers * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28300906Sasomers * POSSIBILITY OF SUCH DAMAGES. 29300906Sasomers * 30300906Sasomers * Authors: Alan Somers (Spectra Logic Corporation) 31300906Sasomers * 32300906Sasomers * $FreeBSD: stable/11/lib/libdevdctl/guid.h 360302 2020-04-25 13:03:37Z dim $ 33300906Sasomers */ 34300906Sasomers 35300906Sasomers/** 36300906Sasomers * \file devdctl_guid.h 37300906Sasomers * 38300906Sasomers * Definition of the Guid class. 39300906Sasomers */ 40300906Sasomers#ifndef _DEVDCTL_GUID_H_ 41300906Sasomers#define _DEVDCTL_GUID_H_ 42300906Sasomers 43300906Sasomers/*============================ Namespace Control =============================*/ 44300906Sasomersnamespace DevdCtl 45300906Sasomers{ 46300906Sasomers 47300906Sasomers/*============================= Class Definitions ============================*/ 48300906Sasomers/*----------------------------------- Guid -----------------------------------*/ 49300906Sasomers/** 50300906Sasomers * \brief Object that represents guids. 51300906Sasomers * 52300906Sasomers * It can generally be manipulated as a uint64_t, but with a special 53300906Sasomers * value INVALID_GUID that does not equal any valid guid. 54300906Sasomers * 55300906Sasomers * As of this writing, this class is only used to represent ZFS 56300906Sasomers * guids in events and spa_generate_guid() in spa_misc.c explicitly 57300906Sasomers * refuses to return a guid of 0. So this class uses 0 as the value 58300906Sasomers * for INVALID_GUID. In the future, if 0 is allowed to be a valid 59300906Sasomers * guid, the implementation of this class must change. 60300906Sasomers */ 61300906Sasomersclass Guid 62300906Sasomers{ 63300906Sasomerspublic: 64300906Sasomers /* Constructors */ 65326321Sasomers /* Default constructor: an Invalid guid */ 66300906Sasomers Guid(); 67326321Sasomers /* Construct a guid from a provided integer */ 68300906Sasomers Guid(uint64_t guid); 69326321Sasomers /* Construct a guid from a string in base 8, 10, or 16 */ 70300906Sasomers Guid(const std::string &guid); 71326321Sasomers static Guid InvalidGuid(); 72300906Sasomers 73300906Sasomers /* Test the validity of this guid. */ 74300906Sasomers bool IsValid() const; 75300906Sasomers 76300906Sasomers /* Comparison to other Guid operators */ 77300906Sasomers bool operator==(const Guid& rhs) const; 78300906Sasomers bool operator!=(const Guid& rhs) const; 79300906Sasomers 80300906Sasomers /* Integer conversion operators */ 81300906Sasomers operator uint64_t() const; 82300906Sasomers operator bool() const; 83300906Sasomers 84326321Sasomersprotected: 85300906Sasomers static const uint64_t INVALID_GUID = 0; 86326321Sasomers 87300906Sasomers /* The integer value of the GUID. */ 88300906Sasomers uint64_t m_GUID; 89300906Sasomers}; 90300906Sasomers 91300906Sasomers//- Guid Inline Public Methods ------------------------------------------------ 92300906Sasomersinline 93300906SasomersGuid::Guid() 94300906Sasomers : m_GUID(INVALID_GUID) 95300906Sasomers{ 96300906Sasomers} 97300906Sasomers 98300906Sasomersinline 99300906SasomersGuid::Guid(uint64_t guid) 100300906Sasomers : m_GUID(guid) 101300906Sasomers{ 102300906Sasomers} 103300906Sasomers 104326321Sasomersinline Guid 105326321SasomersGuid::InvalidGuid() 106326321Sasomers{ 107326321Sasomers return (Guid(INVALID_GUID)); 108326321Sasomers} 109326321Sasomers 110300906Sasomersinline bool 111300906SasomersGuid::IsValid() const 112300906Sasomers{ 113300906Sasomers return (m_GUID != INVALID_GUID); 114300906Sasomers} 115300906Sasomers 116300906Sasomersinline bool 117300906SasomersGuid::operator==(const Guid& rhs) const 118300906Sasomers{ 119300906Sasomers return (m_GUID == rhs.m_GUID); 120300906Sasomers} 121300906Sasomers 122300906Sasomersinline bool 123300906SasomersGuid::operator!=(const Guid& rhs) const 124300906Sasomers{ 125300906Sasomers return (m_GUID != rhs.m_GUID); 126300906Sasomers} 127300906Sasomers 128300906Sasomersinline 129300906SasomersGuid::operator uint64_t() const 130300906Sasomers{ 131300906Sasomers return (m_GUID); 132300906Sasomers} 133300906Sasomers 134300906Sasomersinline 135300906SasomersGuid::operator bool() const 136300906Sasomers{ 137300906Sasomers return (m_GUID != INVALID_GUID); 138300906Sasomers} 139300906Sasomers 140300906Sasomers/** Convert the GUID into its string representation */ 141300906Sasomersstd::ostream& operator<< (std::ostream& out, Guid g); 142300906Sasomers 143300906Sasomers} // namespace DevdCtl 144300906Sasomers#endif /* _DEVDCTL_GUID_H_ */ 145