00001 #include "IOManager.h" 00002 #include "Streamer.h" 00003 #include "Streamable.h" 00004 #include "Serializable.h" 00005 #include "IStream.h" 00006 #include "OStream.h" 00007 #include "SctNames.h" 00008 #include <sstream> 00009 00010 using namespace std; 00011 00012 namespace Sct { 00013 00014 typedef map<string, shared_ptr<Streamer> > StreamerMap; 00015 00016 bool IOManager::addToMap(const string& className, std::auto_ptr<Streamer> s) throw() { 00017 StreamerMap& map = getMap(); 00018 if (map.find(className) != map.end()) 00019 return false; 00020 map[className] = s; 00021 return true; 00022 } 00023 00024 StreamerMap& IOManager::getMap() throw() { 00025 static StreamerMap map; 00026 return map; 00027 } 00028 00029 Streamer& IOManager::getStreamer(const string& className) const { 00030 StreamerMap& map = getMap(); 00031 if (map.find(className) == map.end()) { 00032 ostringstream oss; 00033 oss << "Known streamers:" << endl; 00034 for (StreamerMap::const_iterator it=map.begin(); 00035 it!=map.end(); 00036 ++it){ 00037 oss << (*it).first << endl; 00038 } 00039 string diag = oss.str(); 00040 SctNames::Mrs() << "SCT_IO" << MRS_TEXT(diag) << MRS_DIAGNOSTIC << ENDM; 00041 throw NoSuchStreamerException(className, "IOManager can't find a Streamer", __FILE__, __LINE__); 00042 } 00043 return *(*map.find(className)).second; 00044 } 00045 00046 void IOManager::writeImpl(OStream& out, const Streamable& ob, bool bWriteClassName) const { 00047 Streamer& s = getStreamer(ob.getClassName()); 00048 if (bWriteClassName) writeClassName(out, ob.getClassName()); 00049 s.write(out, ob, *this); 00050 } 00051 00052 shared_ptr<Streamable> IOManager::readImpl(IStream& in) const { 00053 string className = readClassName(in); 00054 Streamer& s = getStreamer(className); 00055 return s.read(in, *this); 00056 } 00057 00058 shared_ptr<Streamable> IOManager::readImpl(IStream& in, const string& className) const { 00059 Streamer& s = getStreamer(className); 00060 return s.read(in, *this); 00061 } 00062 00063 void IOManager::readImpl(IStream& in, Streamable& ob, bool bReadClassName) const { 00064 if (bReadClassName) { 00065 string className = readClassName(in); 00066 if (className != ob.getClassName()) { 00067 string str = "Can't read: "; 00068 str += ob.getClassName(); 00069 str += " from stream because stream contains a: "; 00070 str += className; 00071 throw StreamCorruptedException(str, __FILE__, __LINE__); 00072 } 00073 } 00074 Streamer& s = getStreamer(ob.getClassName()); 00075 s.read(in, ob, *this); 00076 } 00077 00078 void IOManager::writeClassName(OStream& out, const string& className) const { 00079 out << "SctClassName:" + className; 00080 } 00081 00082 string IOManager::readClassName(IStream& in) const { 00083 string className = ""; 00084 in >> className; 00085 if (className.find("SctClassName:") == 0) { 00086 return className.substr(13); 00087 } 00088 throw StreamCorruptedException("Read string but it wasn't a className. Got: " + className, __FILE__, __LINE__); 00089 } 00090 00091 00092 00093 }