修订版 | efca055c3be52f6ff146b88d6196513b18572b2c (tree) |
---|---|
时间 | 2013-07-03 22:21:35 |
作者 | Katsuhiko Nishimra <ktns.87@gmai...> |
Commiter | Katsuhiko Nishimra |
Add key value storage in MolDSException.
@@ -17,10 +17,12 @@ | ||
17 | 17 | // along with MolDS. If not, see <http://www.gnu.org/licenses/>. // |
18 | 18 | //************************************************************************// |
19 | 19 | #include<execinfo.h> |
20 | +#include<sstream> | |
20 | 21 | #include<string> |
21 | 22 | #include<stdexcept> |
22 | 23 | #include<boost/format.hpp> |
23 | 24 | #include"MolDSException.h" |
25 | +#include"Enums.h" | |
24 | 26 | using namespace std; |
25 | 27 | namespace MolDS_base{ |
26 | 28 | MolDSException::MolDSException(string cause) : domain_error(cause), backtraceSize(0){ |
@@ -46,5 +48,45 @@ void MolDSException::PrintBacktrace(){ | ||
46 | 48 | cout<<"backtrace:" << endl; |
47 | 49 | backtrace_symbols_fd(this->backtracePtr.get(), this->backtraceSize, 1); |
48 | 50 | } |
51 | + | |
52 | +template<> | |
53 | +int MolDSException::GetKeyValue(int key){ | |
54 | + return intKeyValueMap[key]; | |
55 | +} | |
56 | + | |
57 | +/* | |
58 | +template<> | |
59 | +other MolDSException::GetKeyValue(int key){ | |
60 | + return otherKeyValueMap[key]; | |
61 | +} | |
62 | +*/ | |
63 | + | |
64 | +template<> | |
65 | +void MolDSException::SetKeyValue(int key, int value){ | |
66 | + intKeyValueMap[key]=value; | |
67 | +} | |
68 | + | |
69 | +/* | |
70 | +template<> | |
71 | +void MolDSException::SetKeyValue(int key, other value){ | |
72 | + otherKeyValueMap.insert(otherKeyValueMap::pair(key,value)); | |
73 | +} | |
74 | +*/ | |
75 | + | |
76 | +bool MolDSException::HasKey(int key){ | |
77 | + if(!(intKeyValueMap.find(key)==intKeyValueMap.end())) return true; | |
78 | + //if(otherKeyValueMap.find(key)!=otherKeyValueMap::end) return true; | |
79 | + return false; | |
49 | 80 | } |
50 | 81 | |
82 | +const char* MolDSException::what() const throw(){ | |
83 | + static string str; | |
84 | + stringstream ss; | |
85 | + ss << domain_error::what() << "key value pairs:"; | |
86 | + for(intKeyValueMap_t::const_iterator i = intKeyValueMap.begin(); i != intKeyValueMap.end(); i++){ | |
87 | + ss << endl << '\t' << ExceptionKeyStr(i->first) << ":" << i->second; | |
88 | + } | |
89 | + str = ss.str(); | |
90 | + return str.c_str(); | |
91 | +} | |
92 | +} |
@@ -18,6 +18,7 @@ | ||
18 | 18 | //************************************************************************// |
19 | 19 | #ifndef INCLUDED_MOLDSEXCEPTION |
20 | 20 | #define INCLUDED_MOLDSEXCEPTION |
21 | +#include<map> | |
21 | 22 | #include<boost/shared_array.hpp> |
22 | 23 | namespace MolDS_base{ |
23 | 24 | class MolDSException : public std::domain_error { |
@@ -28,10 +29,20 @@ public: | ||
28 | 29 | #endif |
29 | 30 | ~MolDSException() throw(){}; |
30 | 31 | void PrintBacktrace(); |
32 | + template <class T> | |
33 | + T GetKeyValue(int key); | |
34 | + template <class T> | |
35 | + void SetKeyValue(int key, T value); | |
36 | + bool HasKey(int key); | |
37 | + virtual const char* what() const throw(); | |
31 | 38 | private: |
32 | 39 | void GetBacktrace(int bufsize); |
33 | 40 | size_t backtraceSize; |
34 | 41 | boost::shared_array<void*> backtracePtr; |
42 | + typedef std::map<int, int> intKeyValueMap_t; | |
43 | + intKeyValueMap_t intKeyValueMap; | |
44 | + //typedef std::map<int, other> otherKeyValueMap_t; | |
45 | + //otherKeyValueMap_t otherKeyValueMap; | |
35 | 46 | }; |
36 | 47 | } |
37 | 48 | #endif |