1 /*
  2  *   Copyright (c) 2009, Michael Lehn
  3  *
  4  *   All rights reserved.
  5  *
  6  *   Redistribution and use in source and binary forms, with or without
  7  *   modification, are permitted provided that the following conditions
  8  *   are met:
  9  *
 10  *   1) Redistributions of source code must retain the above copyright
 11  *      notice, this list of conditions and the following disclaimer.
 12  *   2) Redistributions in binary form must reproduce the above copyright
 13  *      notice, this list of conditions and the following disclaimer in
 14  *      the documentation and/or other materials provided with the
 15  *      distribution.
 16  *   3) Neither the name of the FLENS development group nor the names of
 17  *      its contributors may be used to endorse or promote products derived
 18  *      from this software without specific prior written permission.
 19  *
 20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  */
 32 
 33 #ifndef FLENS_DEBUG_AUX_VARIABLEPOOL_TCC
 34 #define FLENS_DEBUG_AUX_VARIABLEPOOL_TCC 1
 35 
 36 #include <iostream>
 37 #include <string>
 38 #include <sstream>
 39 #include <flens/aux/issame.h>
 40 #include <flens/debug/aux/basename.h>
 41 #include <flens/debug/aux/typeid.h>
 42 
 43 namespace flens { namespace verbose {
 44 
 45 template <typename T>
 46 std::string
 47 VariablePool::name(const T &var)
 48 {
 49     unsigned long address = reinterpret_cast<unsigned long>(&var);
 50     std::string   type = typeId(var);
 51 
 52     std::stringstream sstream;
 53     sstream << type << address;
 54     std::string key = sstream.str();
 55     sstream.str("");
 56     sstream.clear();
 57 
 58     if (_typeCount.count(type)==0) {
 59         _typeCount[type] = 0;
 60     }
 61     if (tmpTron && _id.count(key)==0) {
 62         _isTmp[key] = 1;
 63     }
 64     if (_id.count(key)==0) {
 65         _id[key] = ++_typeCount[type];
 66     }
 67 
 68     if (_isTmp.count(key)>0 && _isTmp[key]==1) {
 69         sstream << "tmp_";
 70     }
 71 
 72     sstream << basename(var) << _id[key];
 73     return sstream.str();
 74 }
 75 
 76 template <typename T>
 77 std::string
 78 VariablePool::name(const T &var) const
 79 {
 80     unsigned long address = reinterpret_cast<unsigned long>(&var);
 81     std::string   type = typeId(var);
 82 
 83     std::stringstream sstream;
 84     sstream << type << address;
 85     std::string key = sstream.str();
 86     sstream.str("");
 87     sstream.clear();
 88 
 89     if (_typeCount.count(type)==0) {
 90         return "unknown";
 91     }
 92     if (_id.count(key)==0) {
 93         return "unkown";
 94     }
 95 
 96     sstream << basename(var) << _id.find(key)->second;
 97     return sstream.str();
 98 }
 99 
100 template <typename T>
101 void
102 VariablePool::addTemporary(const T &var)
103 {
104     unsigned long address = reinterpret_cast<unsigned long>(&var);
105     std::string   type = typeId(var);
106 
107     std::stringstream sstream;
108     sstream << type << address;
109     std::string key = sstream.str();
110     sstream.str("");
111     sstream.clear();
112 
113     _isTmp[key] = 1;
114 }
115 
116 template <typename T>
117 void
118 VariablePool::removeTemporary(const T &var)
119 {
120     unsigned long address = reinterpret_cast<unsigned long>(&var);
121     std::string   type = typeId(var);
122 
123     std::stringstream sstream;
124     sstream << type << address;
125     std::string key = sstream.str();
126     sstream.str("");
127     sstream.clear();
128 
129     _isTmp.erase(key);
130     _id.erase(key);
131 }
132 
133 } } // namespace verbose, namespace flens
134 
135 #endif // FLENS_DEBUG_AUX_VARIABLEPOOL_TCC