cgma
CommandFeedback.hpp
Go to the documentation of this file.
00001 #ifndef CUBIT_COMMANDFEEDBACK_HPP
00002 #define CUBIT_COMMANDFEEDBACK_HPP
00003 
00004 #include <typeinfo>
00005 #include <cassert>
00006 
00007 // Base class for all command feedback.
00008 class CommandFeedback
00009 {
00010 public:
00011   typedef const std::type_info& Type;
00012 
00013   CommandFeedback()
00014   {}
00015 
00016   virtual ~CommandFeedback()
00017   {}
00018 
00019   virtual Type this_type() const = 0;
00020 
00021   template <typename target_type>
00022     target_type& get_reference()
00023   {
00024     assert(this->this_type() == target_type::type());
00025     return *(dynamic_cast<target_type*>(this));
00026   }
00027 
00028   template <typename target_type>
00029     const target_type& get_reference() const
00030   {
00031     assert(this->this_type() == target_type::type());
00032     return *(dynamic_cast<const target_type*>(this));
00033   }
00034 };
00035 
00036 // The rest of this file is an example to show how to use CommandFeedback
00037 #include "CubitString.hpp"
00038 
00039 class DeprecatedCommandError : public CommandFeedback
00040 {
00041 public:
00042   DeprecatedCommandError(const CubitString &some_string)
00043     : myInt(27), myString(some_string) 
00044   {}
00045 
00046   int wow_an_integer() const
00047   { return myInt; }
00048   CubitString wow_a_string() const
00049   { return myString; }
00050 
00051   static CommandFeedback::Type type()
00052   { return typeid(DeprecatedCommandError); }
00053 
00054   CommandFeedback::Type this_type() const
00055   { return DeprecatedCommandError::type(); }
00056 
00057 private:
00058   int myInt;
00059   CubitString myString;
00060 };
00061 
00062 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines