00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _COMPSERIALIZATION_H
00027 #define _COMPSERIALIZATION_H
00028
00029 #include <core/core.h>
00030 #include <core/timer.h>
00031 #include <core/propertywriter.h>
00032
00033 #include <typeinfo>
00034 #include <boost/preprocessor/cat.hpp>
00035
00036 #include <boost/archive/text_iarchive.hpp>
00037 #include <boost/archive/text_oarchive.hpp>
00038
00039 #include <boost/serialization/export.hpp>
00040
00041 #include <boost/serialization/list.hpp>
00042 #include <boost/serialization/vector.hpp>
00043
00044 #include <sstream>
00045 #include <fstream>
00046
00047
00048 template <class T>
00049 class PluginStateWriter
00050 {
00051 private:
00052 PropertyWriter mPw;
00053 Window mResource;
00054 T *mClassPtr;
00055 CompTimer mTimeout;
00056
00057 friend class boost::serialization::access;
00058
00059 bool
00060 checkTimeout ()
00061 {
00062 if (!screen->shouldSerializePlugins ())
00063 return false;
00064
00065 CompOption::Vector atomTemplate = mPw.readProperty (mResource);
00066
00067 if (atomTemplate.empty ())
00068 return false;
00069
00070 if (!(atomTemplate.at (0).value ().type () == CompOption::TypeString))
00071 return false;
00072
00073 std::istringstream iss (atomTemplate.at (0).value ().s ());
00074 boost::archive::text_iarchive ia (iss);
00075
00076 ia >> *this;
00077
00078 postLoad ();
00079
00080
00081
00082 mPw.deleteProperty (mResource);
00083
00084 return false;
00085 };
00086
00087 public:
00088
00089 template <class Archive>
00090 void serialize (Archive &ar, const unsigned int version)
00091 {
00092 ar & *mClassPtr;;
00093 };
00094
00095 virtual void postLoad () {};
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 void writeSerializedData ()
00107 {
00108 if (!screen->shouldSerializePlugins ())
00109 return;
00110
00111 CompOption::Vector atomTemplate = mPw.getReadTemplate ();
00112 std::string str;
00113 std::ostringstream oss (str);
00114 boost::archive::text_oarchive oa (oss);
00115
00116 oa << *this;
00117
00118 CompOption::Value v (oss.str ().c_str ());
00119 atomTemplate.at (0).set (v);
00120
00121 mPw.updateProperty (mResource, atomTemplate, XA_STRING);
00122 }
00123
00124 PluginStateWriter (T *instance,
00125 Window xid) :
00126 mResource (xid),
00127 mClassPtr (instance)
00128 {
00129 if (screen->shouldSerializePlugins ())
00130
00131 {
00132 CompString atomName = compPrintf ("_COMPIZ_%s_STATE",
00133 typeid (T).name ());
00134 CompOption::Vector o;
00135
00136 o.resize (1);
00137 o.at (0).setName ("data", CompOption::TypeString);
00138
00139 mPw = PropertyWriter (atomName, o);
00140
00141 mTimeout.setCallback (boost::bind (&PluginStateWriter::checkTimeout,
00142 this));
00143 mTimeout.setTimes (0, 0);
00144 mTimeout.start ();
00145 }
00146 }
00147
00148 virtual ~PluginStateWriter () {};
00149
00150 };
00151
00152 #endif