torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DDBStreamHelper.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the DDBStreamHelper class.
18 
19 #ifndef TORC_ARCHITECTURE_DDBSTREAMHELPER_HPP
20 #define TORC_ARCHITECTURE_DDBSTREAMHELPER_HPP
21 
23 #include <ostream>
24 #include <map>
25 
26 namespace torc {
27 namespace architecture {
28 
29  /// \brief Device database stream helper class.
30  /// \details This helper class allows the caller to associate a device database with an output
31  /// stream. This allows extremely lightweight classes like Tilewire to decorate their
32  /// output with additional information, without requiring their own database reference.
33  /// \note The database reference only needs to be inserted one time into the stream.
34  /// \details <p><pre>std::cout << ddb << ...;</pre>
36  protected:
37  // friends
38  /// \brief Insertion operator to associate the given device database with the given stream.
39  friend std::ostream& operator <<(std::ostream& os, const class DDB& ddb);
40  // types
41  /// \brief Map of streams to database pointers.
42  typedef std::map<std::ostream*, const class DDB*> OStreamToDDBMap;
43  // static variables
44  /// \brief Map of streams to databases.
46  public:
47  /// \brief Return the device database pointer associated with this stream.
48  /// \returns the device database pointer, or 0 if no database has been associated with the
49  /// stream.
50  static const class DDB* getDDBPtr(std::ostream& os) {
51  OStreamToDDBMap::const_iterator p = sStreamToDatabase.find(&os);
52  if(p == sStreamToDatabase.end()) return 0;
53  return p->second;
54  }
55  /// \brief Dissociate the given device database from any stream.
56  static void dissociate(const class DDB& ddb) {
57  const DDB* ddbPtr = &ddb;
58  // iterate through all streams that have an association
59  OStreamToDDBMap::iterator p = sStreamToDatabase.begin();
60  OStreamToDDBMap::iterator e = sStreamToDatabase.end();
61  while(p != e) {
62  // if we find a stream associated with this database, remove the association
63  if(p->second == ddbPtr) sStreamToDatabase.erase(p);
64  p++;
65  }
66  }
67  };
68 
69  /// \brief Insertion operator to associate the given device database with the given stream.
70  /// \details This operator does not generate any stream output, but instead associates the
71  /// given device database with the stream. This allows lightweight objects such as
72  /// tilewires to decorate their output with database information, without including a
73  /// database reference.
74  std::ostream& operator <<(std::ostream& os, const class DDB& ddb);
75 
76 } // namespace architecture
77 } // namespace torc
78 
79 #endif // TORC_ARCHITECTURE_DDBSTREAMHELPER_HPP
80 
static void dissociate(const class DDB &ddb)
Dissociate the given device database from any stream.
Device database, including complete wiring and logic support.
Definition: DDB.hpp:42
static OStreamToDDBMap sStreamToDatabase
Map of streams to databases.
std::ostream & operator<<(std::ostream &os, const DDB &ddb)
std::map< std::ostream *, const class DDB * > OStreamToDDBMap
Map of streams to database pointers.
static const class DDB * getDDBPtr(std::ostream &os)
Return the device database pointer associated with this stream.
Device database stream helper class.
friend std::ostream & operator<<(std::ostream &os, const class DDB &ddb)
Insertion operator to associate the given device database with the given stream.
Header for the EncapsulatedInteger template.