1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef CUBIT_COMMANDFEEDBACK_HPP
#define CUBIT_COMMANDFEEDBACK_HPP

#include <typeinfo>
#include <cassert>

// Base class for all command feedback.
class CommandFeedback
{
public:
  typedef const std::type_info& Type;

  CommandFeedback()
  {}

  virtual ~CommandFeedback()
  {}

  virtual Type this_type() const = 0;

  template <typename target_type>
    target_type& get_reference()
  {
    assert(this->this_type() == target_type::type());
    return *(dynamic_cast<target_type*>(this));
  }

  template <typename target_type>
    const target_type& get_reference() const
  {
    assert(this->this_type() == target_type::type());
    return *(dynamic_cast<const target_type*>(this));
  }
};

// The rest of this file is an example to show how to use CommandFeedback
#include "CubitString.hpp"

class DeprecatedCommandError : public CommandFeedback
{
public:
  DeprecatedCommandError(const CubitString &some_string)
    : myInt(27), myString(some_string) 
  {}

  int wow_an_integer() const
  { return myInt; }
  CubitString wow_a_string() const
  { return myString; }

  static CommandFeedback::Type type()
  { return typeid(DeprecatedCommandError); }

  CommandFeedback::Type this_type() const
  { return DeprecatedCommandError::type(); }<--- The extra qualification 'DeprecatedCommandError::' is unnecessary and is considered an error by many compilers.

private:
  int myInt;
  CubitString myString;
};

#endif