SST/macro
serializable.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 #ifndef SPROCKIT_COMMON_MESSAGES_SERIALIZABLE_H_INCLUDED
13 #define SPROCKIT_COMMON_MESSAGES_SERIALIZABLE_H_INCLUDED
14 
16 #include <sprockit/unordered.h>
17 #include <typeinfo>
18 #include <stdint.h>
19 
20 namespace sprockit {
21 
22 #define ImplementVirtualSerializable(obj) \
23  protected: \
24  obj(cxn_flag_t flag){}
25 
26 #define NotSerializable(obj) \
27  public: \
28  static void \
29  throw_exc(){ \
30  spkt_throw_printf(sprockit::illformed_error, \
31  "type %s should not be serialized", \
32  #obj); \
33  } \
34  virtual void \
35  serialize_order(sprockit::serializer& sst){ \
36  throw_exc(); \
37  } \
38  virtual uint32_t \
39  cls_id() const { \
40  throw_exc(); \
41  return -1; \
42  } \
43  static obj* \
44  construct_deserialize_stub() { \
45  throw_exc(); \
46  return 0; \
47  } \
48  virtual const char* \
49  cls_name() const { \
50  throw_exc(); \
51  return ""; \
52  } \
53  virtual obj* \
54  you_forgot_to_add_ImplementSerializable_to_this_class() { \
55  return 0; \
56  } \
57 
58 #define ImplementSerializableDefaultConstructor(obj) \
59  public: \
60  virtual const char* \
61  cls_name() const { \
62  return #obj; \
63  } \
64  virtual uint32_t \
65  cls_id() const { \
66  return ::sprockit::serializable_builder_impl< obj >::static_cls_id(); \
67  } \
68  static obj* \
69  construct_deserialize_stub() { \
70  return new obj; \
71  } \
72  virtual obj* \
73  you_forgot_to_add_ImplementSerializable_to_this_class() { \
74  return 0; \
75  } \
76 
77 #define ImplementSerializable(obj) \
78  public: \
79  ImplementSerializableDefaultConstructor(obj)
80 
81 
83 {
84  public:
85  virtual serializable*
86  build() const = 0;
87 
89 
90  virtual const char*
91  name() const = 0;
92 
93  virtual uint32_t
94  cls_id() const = 0;
95 
96  virtual bool
97  sanity(serializable* ser) = 0;
98 };
99 
100 template<class T>
102 {
103  protected:
104  static const char* name_;
105  static const uint32_t cls_id_;
106 
107  public:
108  serializable*
109  build() const {
110  return T::construct_deserialize_stub();
111  }
112 
113  const char*
114  name() const {
115  return name_;
116  }
117 
118  uint32_t
119  cls_id() const {
120  return cls_id_;
121  }
122 
123  static uint32_t
125  return cls_id_;
126  }
127 
128  static const char*
130  return name_;
131  }
132 
133  bool
135  return (typeid(T) == typeid(*ser));
136  }
137 };
138 
139 
141 {
142  protected:
143  typedef spkt_unordered_map<long, serializable_builder*> builder_map;
144  static builder_map* builders_;
145 
146  public:
147  static serializable*
148  get_serializable(uint32_t cls_id);
149 
150  /**
151  @return The cls id for the given builder
152  */
153  static uint32_t
154  add_builder(serializable_builder* builder, const char* name);
155 
156  static bool
157  sanity(serializable* ser, uint32_t cls_id) {
158  return (*builders_)[cls_id]->sanity(ser);
159  }
160 
161  static void
162  delete_statics();
163 
164 };
165 
166 template<class T> const char* serializable_builder_impl<T>::name_ = typeid(T).name();
167 template<class T> const uint32_t serializable_builder_impl<T>::cls_id_
169  typeid(T).name());
170 
171 }
172 
174 
175 #define DeclareSerializable(...)
176 
177 
178 #endif
179 
static const char * static_cls_name()
Definition: serializable.h:129
static uint32_t add_builder(serializable_builder *builder, const char *name)
virtual uint32_t cls_id() const =0
static bool sanity(serializable *ser, uint32_t cls_id)
Definition: serializable.h:157
virtual bool sanity(serializable *ser)=0
static builder_map * builders_
Definition: serializable.h:144
spkt_unordered_map< long, serializable_builder * > builder_map
Definition: serializable.h:143
virtual const char * name() const =0
bool sanity(serializable *ser)
Definition: serializable.h:134
virtual serializable * build() const =0
serializable * build() const
Definition: serializable.h:109