Serializable.hxx

Go to the documentation of this file.
00001 #ifndef COMMON_SERIALIZABLE_H
00002 #define COMMON_SERIALIZABLE_H
00003 
00004 /*
00005  * Copyright (c) 2004, The EROS Group, LLC and Johns Hopkins
00006  * University. All rights reserved.
00007  * 
00008  * This software was developed to support the EROS secure operating
00009  * system project (http://www.eros-os.org). The latest version of
00010  * the OpenCM software can be found at http://www.opencm.org.
00011  * 
00012  * Redistribution and use in source and binary forms, with or
00013  * without modification, are permitted provided that the following
00014  * conditions are met:
00015  * 
00016  * 1. Redistributions of source code must retain the above copyright
00017  *    notice, this list of conditions and the following disclaimer.
00018  * 
00019  * 2. Redistributions in binary form must reproduce the above
00020  *    copyright notice, this list of conditions and the following
00021  *    disclaimer in the documentation and/or other materials
00022  *    provided with the distribution.
00023  * 
00024  * 3. Neither the name of the The EROS Group, LLC nor the name of
00025  *    Johns Hopkins University, nor the names of its contributors
00026  *    may be used to endorse or promote products derived from this
00027  *    software without specific prior written permission.
00028  * 
00029  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
00030  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
00031  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00032  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00033  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
00034  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00035  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00036  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00037  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00038  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00039  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00040  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00041  * POSSIBILITY OF SUCH DAMAGE.
00042  */
00043 
00044 class Repository;
00045 class MarkState;
00046 
00047 /* Serializable -- all serializable entities obey this protocol */
00048 
00049 #define OBTYPE(code,ver,schema,nm) TY_##nm = code,
00050 enum SerTypeConst {
00051 #include "OBTYPES.def"
00052   
00053   TY_ntypes,
00054   TY_Serializable    /* means any Serializable object */
00055 };
00056 
00057 /* The serializable metaVersion field appears at the location where
00058    the obVer used to appear before the metaVersion field was
00059    introduced. At that time, the highest object version was
00060    1. We therefore set FIRST_METAVERSION to 2.
00061 
00062    If we retrieve an object whose metaVer field claims to be <
00063    FIRST_METAVERSION, then the field retrieved is really an object
00064    version. In that case, the metaversion is understood to be 0 and
00065    the affinity is also 0. */
00066 #define SERIALIZABLE_FIRST_METAVERSION 4
00067 #define SERIALIZABLE_CURRENT_METAVERSION 4
00068 /* Last metaversion where we did a major schema change */
00069 #define SERIALIZABLE_CURRENT_SCHEMA_METAVERSION 4
00070 
00071 struct SerialType;              // forward declaration
00072 struct Buffer;                  // forward declaration
00073 
00074 /* Forward declarations: */
00075 namespace sdr {
00076   class OStream;
00077   class IStream;
00078 };
00079 
00082 struct Serializable : public Countable {
00083   friend class SerMeta;
00084 
00085 public:
00086   const SerialType *serType;
00087   uint32_t metaVer;             /* version of the Serializable structure */
00088   uint32_t   obVer; /* serialized version constant for this object */
00089 
00090   /* Beginning in metaVer 2 */
00091   uint32_t affinity;            /* storage affinity */
00092 
00093   /* Following fields are in-memory only: */
00094   bool        canFreeze;
00095 private:
00096   TrueName    trueName;
00097 
00098 public:
00099   /* This constructor is used solely for the global, static
00100      declarations of prototypical SerialType templates. */
00101   inline Serializable(const SerialType* st, uint32_t mv, uint32_t ov)
00102     : serType(st)
00103   {
00104     metaVer = mv;
00105     obVer = ov;
00106     affinity = 0;
00107 
00108     canFreeze = false;
00109     trueName = TrueName::Null;
00110   }
00111 
00112   Serializable(const Serializable& other)
00113   {
00114     serType = other.serType;
00115     metaVer = other.metaVer;
00116     obVer = other.obVer;
00117     affinity = other.affinity;
00118     canFreeze = false;
00119     trueName = TrueName::Null;
00120   }
00121 
00122   virtual ~Serializable()
00123   {
00124   }
00125 
00126   inline SerTypeConst getType() const;
00127 
00128   TrueName getTrueName() const;
00129   TrueName getTrueName();
00130 
00131   GCPtr<Serializable> deepCopy() const;
00132 
00133   inline void SetAffinity(uint32_t aff)
00134   {
00135     affinity = aff;
00136   }
00137 
00138   inline uint32_t GetAffinity()
00139   {
00140     return affinity;
00141   }
00142 
00143   void toBuffer(Buffer& buf, bool forSignature = false);
00144 
00145 public:
00146   // These are intended to be used only by the low-level 
00147   // serialization routines. 
00148   void serialize_meta(sdr::OStream& strm) const;
00149   void deserialize_meta(sdr::IStream &strm);
00150 };
00151 
00159 struct SerialType {
00160   const SerTypeConst tyConst;   /* type code */
00161 
00162   /* This serializable is used as a template when creating new
00163      objects. Its version field contains the highest known version,
00164      and its ser_type field points back recursively to the containing
00165      SerialType structure */
00166   const Serializable& ser;
00167 
00168   const char *  tyName;         /* used in texty formats */
00169 
00170   static const SerialType* find(SerTypeConst tyConst);
00171 
00172   void (*serialize)(sdr::OStream& os, GCPtr<const Serializable> s);
00173   GCPtr<Serializable> (*deserialize)(sdr::IStream&, const Serializable &meta);
00174   void          (*mark)        (GCPtr<Repository>,
00175                                 GCPtr<const Serializable> container,
00176                                 GCPtr<const Serializable> ob,
00177                                 MarkState& memObs);
00178   bool          (*check)       (GCPtr<const Serializable> ob);
00179 };
00180 
00181 inline SerTypeConst Serializable::getType() const
00182 {
00183   return serType->tyConst;
00184 }
00185 
00186 /* The following declarations are common to all serializable
00187    objects. Rather than encourage keystroke and update errors, provide
00188    a macro here that can be used at the front of every object
00189    declaration to get them all:
00190  */
00191 #define SERIALIZABLE_DECLS(typename) \
00192 public: \
00193   static void serialize(sdr::OStream& os, GCPtr<const Serializable> s); \
00194   static GCPtr<Serializable> deserialize(sdr::IStream&, const Serializable &meta); \
00195   static void mark(GCPtr<Repository> r, \
00196                    GCPtr<const Serializable> container, \
00197                    GCPtr<const Serializable> ob, \
00198                    MarkState& memObs); \
00199   static bool check(GCPtr<const Serializable> ob); \
00200 \
00201   static const SerialType SerType; \
00202   static const Serializable CurMeta; \
00203 \
00204   typename(const Serializable& s = CurMeta); \
00205   virtual ~typename()
00206 
00207 
00208 /* It is occasionally useful to extract and save/restore just the
00209    metadata of an object: */
00210 typedef struct SerMeta SerMeta;
00211 struct SerMeta : public Serializable {
00212   SERIALIZABLE_DECLS(SerMeta);
00213 
00214   Serializable meta;
00215 
00216   static GCPtr<SerMeta> fromSerializable(GCPtr<const Serializable>);
00217 };
00218 
00219 #define ser_mark(r, pv, v, ms) v->serType->mark(r, pv, v, ms)
00220 
00221 void mark_truename(GCPtr<const Serializable> ob, MarkState& ms, TrueName tn);
00222 void mark_mutname(GCPtr<const Serializable> ob, MarkState& ms, MutName mutName);
00223 
00224 #if 0
00225 void ser_init(void *s, const Serializable *);
00226 
00227 
00228 /* Support for assignment guards: */
00229 #define CMVAR(x) cm_##x
00230 #define CMCHANGED(s1)   ( ((Serializable *)s1)->trueName.tnval = 0 )
00231 #define CMSET(s1,x,v)   ( CMCHANGED(s1), s1->CMVAR(x) = (v)    )
00232 #define CMGET(s,x)     s->CMVAR(x)
00233 #define CMCLOBBER(s,x) (CMCHANGED(s), s->CMVAR(x))
00234 #define CM_CANFREEZE(s) ((Serializable *)s)->canFreeze = FALSE
00235 
00236 /* Support for affinity */
00237 #define AFFGET(s) (((Serializable *)s)->affinity)
00238 #define AFFSET(s,a) (AFFGET(s) = a)
00239 #endif
00240 
00241 // Local Variables:
00242 // mode:c++
00243 // End:
00244 
00245 #endif /* COMMON_SERIALIZABLE_H */

Generated on Sun Apr 23 22:42:38 2006 for OpenCM by  doxygen 1.4.6