include/EDoc/TranslationUnit.h

Go to the documentation of this file.
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_TRANSLATIONUNIT_H
00020 #define EDOC_TRANSLATIONUNIT_H
00021 
00022 #include "EDoc/stdint.h"
00023 #include <string>
00024 #include <list>
00025 #include <vector>
00026 
00027 
00028 namespace EDoc
00029 {
00030    class File;
00031    
00032    //===========================================================================
00033    /** \brief Represents a translation unit in code.
00034     *
00035     * Translation unit instances when created remain around for the entire
00036     * duration of the program execution. 
00037     *
00038     * A translation unit instance should be created for every new translation
00039     * unit encounteded from a .edc file. This is how we ensure uniqueness of
00040     * translation units. We assign an integer identifier to a translation unit
00041     * when it is created that is used throught the execution of the program, but
00042     * not stored in any .edc files. It is assumed that in a single execution
00043     * there are NO DUPLICATE TRANSLATION UNITS CREATED/LOADED. This is an
00044     * important limitation. 
00045     *
00046     *
00047     * In order to create a new translation unit use the Create() function().
00048     *
00049     *
00050     * HISTORY
00051     *
00052     * Before this class was created a translation unit was identified by the
00053     * main_input_source as supplied by GCC. However this can be have the same
00054     * name for different translation units that are linked together. So we
00055     * created this to ensure that every translation unit encountered is
00056     * considered unique and so there is no consideration that we can load a
00057     * single translation unit twice in a single execution of EDoc.
00058     */
00059    class TranslationUnit
00060    {
00061    public:
00062       
00063       //------------------------------------------------------------------------
00064       /** \brief NOT TO BE USED.
00065        *
00066        * This was created specifically because I have yet to find out how to get
00067        * SWIG to work when there is no default constructor for a class.
00068        */
00069       TranslationUnit();
00070       
00071       //------------------------------------------------------------------------
00072       /** \brief returns the TranslationUnit instance for the special 
00073        * "unknown" translation unit.
00074        *
00075        * This is primarily used whena translation unit instance is required for
00076        * hand crafted special objects like 
00077        * Dictionary::SUBSTITUTION_EXCEPTION_TYPE
00078        */
00079       static TranslationUnit* GetUnknown();
00080 
00081       //------------------------------------------------------------------------
00082       /** \brief Create a new translation unit.
00083        *
00084        * \param name_in Is a string that identifies the translation unit but is
00085        * not necessarily unique. This is usually the .c or .cpp file name that
00086        * was used to compile the translation unit.
00087        */
00088       static TranslationUnit* Create(std::string name_in);
00089       
00090       //------------------------------------------------------------------------
00091       /** \brief Returns the name of the main code file used to compile this
00092        * translation unit.
00093        */
00094       const std::string& GetName() const;
00095 
00096       //------------------------------------------------------------------------
00097       /** \brief Returns the unique identified for this translation unit.
00098        *
00099        * Unique within a single execution of EDoc. A translation unit will not
00100        * necessarily have the same identifier between EDoc executions.
00101        */
00102       uint32_t GetID() const;
00103       
00104       //------------------------------------------------------------------------
00105       /** \brief Print the current object to the given stream in a user readable
00106        * manner.
00107        *
00108        * \param out The stream to print to.
00109        *
00110        * \param prefix A string prefix to display at the start of every line. As
00111        * an example this can be used in order to indent the print by passing
00112        * "\t" as the string.
00113        */
00114       std::ostream& Print(std::ostream& out, std::string prefix="") const;
00115 
00116       //------------------------------------------------------------------------
00117       /** \brief Returns a string representing the current object in a user
00118        * readable manner.
00119        *
00120        * This will return as a string exactly what Print() would display. This
00121        * is primarily here for use from within python and for debugging.
00122        */
00123       std::string ToString(std::string prefix="") const;
00124       
00125       //------------------------------------------------------------------------
00126 
00127    private:
00128 
00129       //------------------------------------------------------------------------
00130       /** \brief The id that will be used for the next translation units unique
00131        * id.
00132        */
00133       static uint32_t next_id;
00134 
00135       //------------------------------------------------------------------------
00136       /** \brief A list of all translation units created during this program
00137        * execution.
00138        */
00139       static std::list<TranslationUnit> translation_units;
00140 
00141       //------------------------------------------------------------------------
00142       /** \brief The unknown translation unit instance.
00143        */
00144       static TranslationUnit* UNKNOWN;
00145 
00146       //------------------------------------------------------------------------
00147       /** \brief Constructor.
00148        *
00149        * Private so onlt Create() can be used to create translation unit
00150        * instances.
00151        */
00152       TranslationUnit(std::string name_in, uint32_t id_in);
00153       
00154       //------------------------------------------------------------------------
00155       /** \brief The unique id of this translation unit.
00156        */
00157       uint32_t id;
00158 
00159       //------------------------------------------------------------------------
00160       /** \brief The not-necessarily unique name given to this translation unit.
00161        */
00162       std::string name;
00163 
00164       //------------------------------------------------------------------------
00165 
00166    };
00167    //===========================================================================
00168    typedef std::vector<TranslationUnit*> TUnitPVec;
00169    
00170 }
00171 
00172 namespace std
00173 {
00174    //===========================================================================
00175    /** \brief See TranslationUnit::Print()
00176     */
00177    static inline std::ostream& operator<<(std::ostream& out, 
00178       const EDoc::TranslationUnit& value)
00179    {
00180       return value.Print(out);
00181    }
00182    //===========================================================================
00183 }
00184 
00185 #endif // EDOC_TRANSLATIONUNIT_H

Generated on Tue Jan 20 18:26:07 2009 for EDoc-0.2.1 by  doxygen 1.5.1