00001 /******************************************************************************* 00002 00003 Copyright (C) 2007 by Brendon Costa 00004 00005 This library is free software; you can redistribute it and/or modify 00006 it under the terms of the "LGPL Like" License defined in the file COPYING 00007 that should have been distributed along with this source. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 00013 You should have received a copy of the "LGPL Like" License 00014 along with this library; see the file COPYING. if not, it can be 00015 obtained from the EDoc++ website: 00016 http://edoc.sourceforge.net/license.html 00017 00018 *******************************************************************************/ 00019 #ifndef EDOC_LOCATION_H 00020 #define EDOC_LOCATION_H 00021 00022 #include "EDoc/DictionarySpecific.h" 00023 #include "EDoc/stdint.h" 00024 #include "EDoc/File.h" 00025 00026 #include <cstddef> 00027 #include <ostream> 00028 #include <sstream> 00029 00030 namespace EDoc 00031 { 00032 class File; 00033 class PersistenceIFace; 00034 class IndexedDictionary; 00035 class Dictionary; 00036 class PStack; 00037 00038 //=========================================================================== 00039 /** \brief Represents a location in a source file used during compilation. 00040 * 00041 * The location is represented using a file and a line number. Quite often 00042 * this information is not available and in those situations the instance 00043 * will contain an "empt" location, where the file is eithre NULL or pointing 00044 * to file type "", or the line number is 0 00045 */ 00046 class Location : public DictionarySpecific 00047 { 00048 typedef void(Location::*safe_bool_t)() const; 00049 void this_type_does_not_support_comparison() const {} 00050 public: 00051 00052 //------------------------------------------------------------------------ 00053 /** \brief See CodeBlock::CodeBlock(Dictionary* dict_in=NULL) 00054 */ 00055 Location(Dictionary* dict_in=NULL); 00056 00057 //------------------------------------------------------------------------ 00058 /** \brief See CodeBlock::CodeBlock(const CodeBlock& right) 00059 */ 00060 Location(const Location& right); 00061 00062 //------------------------------------------------------------------------ 00063 /** \brief See CodeBlock::CodeBlock(const CodeBlock& right, 00064 * Dictionary& dict_in) 00065 */ 00066 Location(const Location& right, Dictionary& dict_in); 00067 00068 //------------------------------------------------------------------------ 00069 /** \brief See CodeBlock::operator=() 00070 */ 00071 Location& operator=(const Location& right); 00072 00073 //------------------------------------------------------------------------ 00074 /** \brief See CodeBlock::Read() 00075 */ 00076 void Read(PersistenceIFace& file, IndexedDictionary& idict); 00077 00078 //------------------------------------------------------------------------ 00079 /** \brief See CodeBlock::Write() 00080 */ 00081 void Write(PersistenceIFace& file) const; 00082 00083 //------------------------------------------------------------------------ 00084 /** \brief See StringIdentifiedObject::Merge() 00085 */ 00086 void Merge(const Location& right); 00087 00088 //------------------------------------------------------------------------ 00089 /** \brief See DictionarySpecific::ReplaceReferences() 00090 */ 00091 virtual size_t ReplaceReferences(PStack& stack, 00092 void* remove, void* replace); 00093 00094 //------------------------------------------------------------------------ 00095 /** \brief See DictionarySpecific::Print() 00096 */ 00097 virtual std::ostream& Print(std::ostream& out, 00098 std::string prefix="") const; 00099 00100 //------------------------------------------------------------------------ 00101 /** \brief See DictionarySpecific::Validate() 00102 */ 00103 virtual void Validate(PStack& stack, const Dictionary& dict_in) const; 00104 00105 //------------------------------------------------------------------------ 00106 /** \brief Equivilance operator (Works across dictionaries). 00107 */ 00108 bool operator==(const Location& right) const; 00109 00110 //------------------------------------------------------------------------ 00111 /** \brief Less than comparison so this can be used for std::map keys. 00112 * 00113 * Compares File first then line. 00114 */ 00115 bool operator<(const Location& right) const; 00116 00117 //------------------------------------------------------------------------ 00118 /** \brief Returns true if location is non-empty through the boolean 00119 * evaluation operator. 00120 * 00121 * Uses the safe bool pattern which makes use of a member function pointer 00122 * type for the boolean operation. 00123 * 00124 * \return Returns true if the location provided is not an "empty" 00125 * location. I.e. it contains a file name or line number. 00126 */ 00127 operator safe_bool_t() const 00128 { 00129 return ((line || (file && file->GetName().size())) ? 00130 &Location::this_type_does_not_support_comparison : 00131 NULL); 00132 } 00133 00134 //------------------------------------------------------------------------ 00135 /** \brief Prints to given stream in single line format which is 00136 * filename:line. 00137 */ 00138 std::ostream& SingleLinePrint(std::ostream& out) const; 00139 00140 00141 //------------------------------------------------------------------------ 00142 /** \brief Returns a string for this locatrion in single line format 00143 * which is filename:line. 00144 */ 00145 inline std::string GetNormalisedName() const 00146 { 00147 std::ostringstream stream; 00148 stream << file->GetName() << ":" << line; 00149 return stream.str(); 00150 } 00151 00152 00153 //------------------------------------------------------------------------ 00154 /** \brief A reference to the file object identifying the source file. 00155 */ 00156 File* file; 00157 00158 //------------------------------------------------------------------------ 00159 /** \brief The line within the given source file. 00160 */ 00161 uint32_t line; 00162 00163 //------------------------------------------------------------------------ 00164 }; 00165 //=========================================================================== 00166 } 00167 00168 #endif // EDOC_LOCATION_H