00001 // Torc - Copyright 2011 University of Southern California. All Rights Reserved. 00002 // $HeadURL: https://torc-isi.svn.sourceforge.net/svnroot/torc-isi/branches/staging/0.9/src/torc/architecture/DDBStreamHelper.hpp $ 00003 // $Id: DDBStreamHelper.hpp 10 2011-10-12 18:40:16Z nsteiner $ 00004 00005 // This program is free software: you can redistribute it and/or modify it under the terms of the 00006 // GNU General Public License as published by the Free Software Foundation, either version 3 of the 00007 // License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 00010 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 00011 // the GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License along with this program. If 00014 // not, see <http://www.gnu.org/licenses/>. 00015 00016 /// \file 00017 /// \brief Header for the DDBStreamHelper class. 00018 00019 #ifndef TORC_ARCHITECTURE_DDBSTREAMHELPER_HPP 00020 #define TORC_ARCHITECTURE_DDBSTREAMHELPER_HPP 00021 00022 #include "torc/common/EncapsulatedInteger.hpp" 00023 #include <ostream> 00024 #include <map> 00025 00026 namespace torc { 00027 namespace architecture { 00028 00029 /// \brief Device database stream helper class. 00030 /// \details This helper class allows the caller to associate a device database with an output 00031 /// stream. This allows extremely lightweight classes like Tilewire to decorate their 00032 /// output with additional information, without requiring their own database reference. 00033 /// \note The database reference only needs to be inserted one time into the stream. 00034 /// \details <p><pre>std::cout << ddb << ...;</pre> 00035 class DDBStreamHelper { 00036 protected: 00037 // types 00038 /// \brief Map of streams to iwords. 00039 typedef std::map<std::ostream*, int> OStreamToIntMap; 00040 // static variables 00041 /// \brief Map of streams to iwords. 00042 static OStreamToIntMap sIWordIndexes; 00043 public: 00044 // types 00045 /// \brief Encapsulation of a stream iword used as a pword index. 00046 typedef torc::common::EncapsulatedInteger<int> IWordIndex; 00047 // functions 00048 /// \brief Return the iword index for this stream, or allocate it if not defined. 00049 static IWordIndex getIWordIndex(std::ostream& os) { 00050 OStreamToIntMap::const_iterator index = sIWordIndexes.find(&os); 00051 if(index == sIWordIndexes.end()) { 00052 std::pair<OStreamToIntMap::iterator, bool> inserted 00053 = sIWordIndexes.insert(std::make_pair(&os, os.xalloc())); 00054 index = inserted.first; 00055 } 00056 return index->second; 00057 } 00058 /// \brief Return the device database pointer associated with this stream. 00059 /// \returns the device database pointer, or 0 if no database has been associated with the 00060 /// stream. 00061 static const class DDB* getDDBPtr(std::ostream& os) { 00062 return static_cast<DDB*>(os.pword(getIWordIndex(os))); 00063 } 00064 }; 00065 00066 /// \brief Insertion operator to associate the given device database with the given stream. 00067 /// \details This operator does not generate any stream output, but instead associates the 00068 /// given device database with the stream. This allows lightweight objects such as 00069 /// tilewires to decorate their output with database information, without including a 00070 /// database reference. 00071 std::ostream& operator <<(std::ostream& os, const class DDB& ddb); 00072 00073 } // namespace architecture 00074 } // namespace torc 00075 00076 #endif // TORC_ARCHITECTURE_DDBSTREAMHELPER_HPP