00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef IMP_OBJECT_H
00010 #define IMP_OBJECT_H
00011
00012 #include "kernel_config.h"
00013 #include "RefCounted.h"
00014 #include "exception.h"
00015 #include "VersionInfo.h"
00016 #include "macros.h"
00017 #include "log.h"
00018 #include "VectorOfRefCounted.h"
00019
00020 #include <vector>
00021
00022 IMP_BEGIN_NAMESPACE
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 class IMPEXPORT Object: public RefCounted
00050 {
00051
00052 std::string name_;
00053 protected:
00054 IMP_NO_DOXYGEN(Object(std::string name="Nameless"));
00055 IMP_REF_COUNTED_NONTRIVIAL_DESTRUCTOR(Object);
00056
00057 public:
00058 #ifndef IMP_DOXYGEN
00059
00060 bool get_is_valid() const {
00061 #if IMP_BUILD >= IMP_FAST
00062 return true;
00063 #else
00064 return check_value_==111111111;
00065 #endif
00066 }
00067 #endif
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 void set_log_level(LogLevel l) {
00080 IMP_USAGE_CHECK(l <= MEMORY && l >= DEFAULT, "Setting to invalid log level "
00081 << l);
00082 #if IMP_BUILD < IMP_FAST
00083 log_level_=l;
00084 #endif
00085 }
00086
00087 #ifndef IMP_DOXYGEN
00088 LogLevel get_log_level() const {
00089 #if IMP_BUILD >= IMP_FAST
00090 return SILENT;
00091 #else
00092 return log_level_;
00093 #endif
00094 }
00095 #endif // IMP_DOXYGEN
00096
00097
00098 virtual std::string get_type_name() const=0;
00099
00100
00101 void show(std::ostream &out=std::cout) const {
00102 out << get_name()
00103 << "(" << get_type_name() << ", "
00104 << get_version_info() << ")";
00105 do_show(out);
00106 }
00107
00108 #ifndef IMP_DOXYGEN
00109 void debugger_show() const {
00110 show(std::cout);
00111 }
00112
00113
00114 std::string get_string() const {
00115 std::ostringstream oss;
00116 show(oss);
00117 return oss.str();
00118 }
00119 #endif // IMP_DOXYGEN
00120
00121
00122 virtual VersionInfo get_version_info() const=0;
00123
00124
00125
00126
00127
00128
00129
00130
00131 const std::string& get_name() const {
00132 return name_;
00133 }
00134 void set_name(std::string name) {
00135 name_=name;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145 void set_was_used(bool tf) const {
00146 #if IMP_BUILD < IMP_FAST
00147 was_owned_=true;
00148 #endif
00149 }
00150
00151 #ifndef IMP_DOXYGEN
00152
00153 virtual void do_show(std::ostream &out) const =0;
00154 #endif
00155 private:
00156 Object(const Object &o) {}
00157 const Object& operator=(const Object &o) {return *this;}
00158
00159 #if IMP_BUILD < IMP_FAST
00160 LogLevel log_level_;
00161 mutable bool was_owned_;
00162 double check_value_;
00163 #endif
00164 };
00165
00166
00167 #if !defined(IMP_DOXYGEN) && !defined(IMP_SWIG)
00168 inline std::ostream &operator<<(std::ostream &out, const Object& o) {
00169 o.show(out);
00170 return out;
00171 }
00172 #endif
00173
00174
00175
00176
00177 typedef VectorOfRefCounted<Object*> Objects;
00178
00179
00180
00181
00182
00183 typedef std::vector<Object*> ObjectsTemp;
00184
00185
00186
00187
00188
00189
00190 template <class O, class I>
00191 O* object_cast(I *o) {
00192 O *ret= dynamic_cast<O*>(o);
00193 if (!ret) {
00194 IMP_THROW("Object " << o->get_name() << " cannot be cast to "
00195 << "desired type.", ValueException);
00196 }
00197 return ret;
00198 }
00199
00200 #ifndef IMP_DOXYGEN
00201 inline void show(std::ostream &out, Object *o) {
00202 out << o->get_name();
00203 }
00204 #endif
00205
00206 IMP_END_NAMESPACE
00207
00208
00209 #define IMP_CHECK_OBJECT(obj) do { \
00210 IMP_INTERNAL_CHECK((obj) != NULL, "NULL object"); \
00211 IMP_INTERNAL_CHECK((obj)->get_is_valid(), "Check object " << obj \
00212 << " was previously freed"); \
00213 } while (false)
00214
00215 #include "SetLogState.h"
00216
00217 #if IMP_BUILD < IMP_FAST
00218
00219
00220
00221
00222
00223 #define IMP_OBJECT_LOG SetLogState log_state_guard__(get_log_level());\
00224 IncreaseIndent object_increase_indent__
00225 #else
00226 #define IMP_OBJECT_LOG
00227 #endif
00228 #endif