podify.hpp

00001 /*00002 This file is part of GraphLab.00003 00004 GraphLab is free software: you can redistribute it and/or modify00005 it under the terms of the GNU Lesser General Public License as 00006 published by the Free Software Foundation, either version 3 of 00007 the License, or (at your option) any later version.00008 00009 GraphLab is distributed in the hope that it will be useful,00010 but WITHOUT ANY WARRANTY; without even the implied warranty of00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the00012 GNU Lesser General Public License for more details.00013 00014 You should have received a copy of the GNU Lesser General Public 00015 License along with GraphLab. If not, see <http://www.gnu.org/licenses/>.00016 */00017
00018 #ifndef PODIFY_HPP00019 #define PODIFY_HPP00020 #include <graphlab/serialization/iarchive.hpp>00021 #include <graphlab/serialization/oarchive.hpp>00022 namespace graphlab {
00023 00024 /**00025 This is a macro which allows you to temporarily define00026 an object as a POD. 00027 for instance if you have a struct that has no save/load00028 function defined, but you want to treat it as a POD.00029 Now you can write:00030 00031 SOmePODStruct s;00032 oarc << PODIFY(s);00033 and00034 iarc >> PODIFY(s);00035 */00036
00037 #define PODIFY(X) (__podify__<typeof(X)>(X))00038 00039 /**00040 This wraps and adds temporary support for serialization00041 of a POD object00042 */00043 template <typename T>
00044class __podify__{
00045 public:
00046
00047 T alt;
00048 const T* ref;
00049 // if created without constructor, assign 00050 // the pointer to the built-in version00051 __podify__():ref(&alt) { }
00052
00053 __podify__(const T &a):ref(&a) { }
00054
00055 operator const T&() const{
00056 return *ref;
00057 }
00058
00059 operator T&() {
00060 return *(const_cast<T*>(ref));
00061 }
00062
00063 void save(oarchive &a) const{
00064 serialize(a, ref, sizeof(T));
00065 }
00066 // unfortunately load will not work 00067 // because we this class is only created as a temporary00068 // we are unable to get a reference to it00069 // so we make the pass by value version below00070 };
00071
00072
00073 template <typename T>
00074 iarchive& operator>>(iarchive& a, __podify__<T> i) {
00075 T* refptr = (const_cast<T*>(i.ref));
00076 deserialize(a, refptr, sizeof(T));
00077 return a;
00078 }
00079
00080 }
00081 #endif00082

Generated on Tue May 17 2011 15:43:54 for GraphLab: A framework for Parallel Machine Learning by
1.7.1