SST/macro
ptr_type.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_PTR_TYPE_H_INCLUDED
13 #define SPROCKIT_COMMON_PTR_TYPE_H_INCLUDED
14 
15 namespace sprockit {
16 class ptr_type;
17 class simple_ptr_type;
18 }
19 
20 #include <sprockit/errors.h>
21 #include <sprockit/refcount_ptr.h>
22 #include <sprockit/spkt_config.h>
23 #include <typeinfo>
24 
25 namespace sprockit {
26 
27 class ptr_type
28 {
29  private:
30  template <class T> friend class refcount_ptr;
31  mutable int references;
32 
33  public:
34  ptr_type() : references(0) {}
35 
36  int nreferences() const {
37  return references;
38  }
39 
40  protected:
41  void
42  incref() {
43  ref_increment(references);
44  }
45 
46  int
47  decref() {
48  return ref_decrement_return(references);
49  }
50 
51  public:
52  virtual ~ptr_type(){}
53 
54 };
55 
57 {
58  public:
59  virtual std::string
60  to_string() const = 0;
61 };
62 
63 template <class Out, class In>
65 __safe_ptr_cast__(const char* objname,
66  const char* file,
67  int line,
69  const char* error_msg = "error")
70 {
71  sprockit::refcount_ptr<Out> out = dynamic_cast<Out*>(in.get());
72  if (!out) {
74  "%s: failed to cast to %s at %s:%d - %s",
75  error_msg, objname, file, line,
76  (in ? "wrong type" : "null object"));
77  }
78  return out;
79 }
80 
81 /**
82  * First entry in VA_ARGS is the obj
83  * Second entry is optional being an error msg
84 */
85 #define ptr_safe_cast(type,...) \
86  ::sprockit::__safe_ptr_cast__<type>(#type, __FILE__, __LINE__, __VA_ARGS__)
87 
88 #define ptr_interface_cast(type,obj) \
89  dynamic_cast<type*>(obj.get())
90 
91 #define ptr_static_cast(type,obj) \
92  static_cast<type*>(obj.get())
93 
94 #define ptr_test_cast(type, obj) \
95  dynamic_cast<type*>(obj.get())
96 
97 #define ptr_known_cast(type,...) \
98  ptr_safe_cast(type, __VA_ARGS__)
99 
100 
101 } // end of namespace sprockit
102 
103 #endif
104 
#define ref_decrement_return(refcount)
Definition: refcount_ptr.h:19
sprockit::refcount_ptr< Out > __safe_ptr_cast__(const char *objname, const char *file, int line, const sprockit::refcount_ptr< In > &in, const char *error_msg="error")
Definition: ptr_type.h:65
virtual ~ptr_type()
Definition: ptr_type.h:52
#define spkt_throw_printf(exc, template_str,...)
Definition: errors.h:37
int nreferences() const
Definition: ptr_type.h:36
#define ref_increment(refcount)
Definition: refcount_ptr.h:18
Error indicating some internal value was unexpected.
Definition: errors.h:83