00001 //------------------------------------------------------------------------ 00002 // 00003 // Joe Kniss 00004 // 3-22-03 00005 // ________ ____ ___ 00006 // | \ / | / / 00007 // +---+ \/ |/ / 00008 // +--+| |\ /| < 00009 // | || | \ / | |\ \ 00010 // | | \/ | | \ \ 00011 // \_____| |__| \__\ 00012 // Copyright 2003 00013 // Joe Michael Kniss 00014 // <<< jmk@cs.utah.edu >>> 00015 // "All Your Base are Belong to Us" 00016 //------------------------------------------------------------------------- 00017 00018 //simBase.h 00019 00020 00021 #ifndef __SIMIAN_BASE_DOT_H 00022 #define __SIMIAN_BASE_DOT_H 00023 00024 #include <string> 00025 #include <iostream> 00026 00027 typedef std::string SNameType; 00028 00029 ///////////////////////////////////////////////////////////////////////////// 00030 /// SimBase, base object for simianlib. 00031 /// Provides some basic functionality and a common base class for all 00032 /// simian objects. 00033 ///////////////////////////////////////////////////////////////////////////// 00034 class SimBase { 00035 public: 00036 SimBase():_name_(),_changed(0) {} 00037 virtual ~SimBase(){} 00038 00039 //////////////////////////////////////////////////// 00040 ///@name Text object-name 00041 /// from SimBase 00042 /// 00043 ///@{ 00044 std::string getObjName() {return std::string(typeid(*this).name());} 00045 std::string getName() {return _name_;} 00046 void setName(std::string name) {_name_ = name;} 00047 void setName(const char *name) {if(name) _name_ = name;} 00048 ///@} 00049 //////////////////////////////////////////////////// 00050 00051 //////////////////////////////////////////////////// 00052 ///@name Debug prints 00053 /// from SimBase: 00054 /// These print the object name with the error, mostly for 00055 /// convenience, but they are compiled to inline-no-op if you 00056 /// build in release mode 00057 ///@{ 00058 inline 00059 void derr(const char *when, const char *where=0) const; 00060 template<class T> 00061 void derr(const char *when, const char *where, const T stuff) const; 00062 ///@} 00063 //////////////////////////////////////////////////// 00064 00065 /////////////////////////////////////////////////// 00066 ///@name Modification, AKA "changed" 00067 /// from SimBase: 00068 /// id based, you can capture the current id for 00069 /// comparison later, if the id's don't match the 00070 /// class has been changed 00071 ///@{ 00072 virtual int getChangeID() {return _changed;} 00073 virtual int getChangeID() const {return _changed;} 00074 virtual void setChanged() {++_changed;} 00075 ///@} 00076 //////////////////////////////////////////////////// 00077 00078 /////////////////////////////////////////////////// 00079 ///@name Serialization: 00080 /// from SimBase 00081 /// You MUST override these pure 00082 /// virtual functions, they "should" do something 00083 /// usefull. It is also nice if you declare a 00084 /// static std::istream &create(std::istream &is) const; 00085 /// function, to create a class from a stream. This is 00086 /// done on a per-concrete class basis. 00087 /// 00088 /// These aren't pure virutal yet, but will be soon! 00089 ///@{ 00090 00091 /// serialize 00092 virtual std::ostream &saveSelf(std::ostream &os) { return os;} 00093 /// unserialize 00094 virtual std::istream &readSelf(std::istream &is) { return is;} 00095 00096 ///@} 00097 //////////////////////////////////////////////////// 00098 00099 protected: 00100 /// a text name for the object, SimBase 00101 std::string _name_; 00102 /// change id, incremented whenever setChanged is called, SimBase 00103 int _changed; 00104 }; 00105 00106 ///////////////////////////////////////////////////////////////////////////// 00107 ///////////////////////////////////////////////////////////////////////////// 00108 // Implementation 00109 ///////////////////////////////////////////////////////////////////////////// 00110 ///////////////////////////////////////////////////////////////////////////// 00111 00112 inline 00113 void SimBase::derr(const char *when, const char *where) const 00114 { 00115 /// only prints in debug mode, -D_DEBUG 00116 #ifdef _DEBUG 00117 std::cerr << "Simain::" << typeid(*this).name() << "::"; 00118 if(!_name_.empty()) 00119 std::cerr << _name_; 00120 std::cerr << std::endl; 00121 std::cerr << " " << when; 00122 if(where) 00123 std::cerr << " " << where; 00124 std::cerr << std::endl; 00125 #endif 00126 } 00127 00128 template<class T> 00129 inline 00130 void SimBase::derr(const char *when, const char *where, const T stuff) const 00131 { 00132 /// only prints in debug mode, -D_DEBUG 00133 #ifdef _DEBUG 00134 std::cerr << "Simain::" << typeid(*this).name() << "::"; 00135 if(!_name_.empty()) 00136 std::cerr << _name_; 00137 std::cerr << std::endl; 00138 std::cerr << " " << when; 00139 if(where) 00140 std::cerr << " - " << where; 00141 std::cerr << stuff; 00142 std::cerr << std::endl; 00143 #endif 00144 } 00145 00146 #endif 00147