00001 // Copyright (C) 2001--2002 Petter Urkedal 00002 // 00003 // This file is free software; you can redistribute it and/or modify 00004 // it under the terms of the GNU General Public License as published by 00005 // the Free Software Foundation; either version 2 of the License, or 00006 // (at your option) any later version. 00007 // 00008 // This file is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00016 // 00017 // As a special exception, you may use this file as part of a free 00018 // software library without restriction. Specifically, if other files 00019 // instantiate templates or use macros or inline functions from this 00020 // file, or you compile this file and link it with other files to 00021 // produce an executable, this file does not by itself cause the 00022 // resulting executable to be covered by the GNU General Public 00023 // License. This exception does not however invalidate any other 00024 // reasons why the executable file might be covered by the GNU General 00025 // Public License. 00026 // 00027 // $Id: identifier.h,v 1.2 2002/08/24 13:58:10 petter_urkedal Exp $ 00028 00029 00030 #ifndef MORE_GEN_IDENTIFIER_H 00031 #define MORE_GEN_IDENTIFIER_H 00032 00033 #include <iosfwd> 00034 #include <more/io/fwd.h> 00035 #include <cstring> 00036 00037 namespace more { 00038 namespace gen { 00039 00040 /** \class identifier identifier.h more/gen/identifier.h 00041 ** 00042 ** An identifier. This is just a simple string-like class with 00043 ** constant-time comparison. String manipulation is not provided 00044 ** by intention, as that would lead to generation of redundant 00045 ** unique string pointers. Use \c std::string or \c gc_string to 00046 ** create the string first, if needed. */ 00047 struct identifier 00048 { 00049 typedef std::size_t size_type; 00050 00051 /** Construct an invalid identifier. Ideally this ctor would 00052 create an anonymous unique identifier, but that may have 00053 lead to many redundant allocations declared and defined 00054 separately. */ 00055 identifier() 00056 : m_str(0) {} 00057 00058 /// Construct an object identified by "str". 00059 identifier(char const* str); 00060 00061 /** Return a C string which is lexically equal to the string 00062 this object was constructed from. */ 00063 char const* c_str() const { return m_str; } 00064 00065 size_type length() const { return m_str? std::strlen(m_str) : 0; } 00066 00067 /// True iff the identifier is valid. 00068 bool is_defined() const { return m_str; } 00069 00070 /// Equivalent to "strcmp(c_str(), rhs.c_str()) == 0", but faster. 00071 bool operator==(identifier const& rhs) const 00072 { 00073 return m_str == rhs.m_str; 00074 } 00075 00076 /// True if the identifiers are different. 00077 bool operator!=(identifier const& rhs) const 00078 { 00079 return m_str != rhs.m_str; 00080 } 00081 00082 /// Provides an arbitrary ordering if identifiers. 00083 bool operator<(identifier const& rhs) const 00084 { 00085 return m_str < rhs.m_str; 00086 } 00087 00088 void sync(io::syncstream&); 00089 00090 friend std::ostream& operator<<(std::ostream&, identifier const&); 00091 00092 private: 00093 char const* m_str; 00094 }; 00095 00096 }} 00097 00098 #endif