00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef eman__object__h__
00037 #define eman__object__h__ 1
00038
00039 #include <map>
00040 using std::map;
00041
00042 #include <vector>
00043 using std::vector;
00044
00045 #include <string>
00046 using std::string;
00047
00048 #include <utility>
00049 using std::pair;
00050
00051 #include <algorithm>
00052
00053
00054 #include <iterator>
00055
00056 #include "log.h"
00057 #include "exception.h"
00058
00059
00060
00061
00062 #include <iostream>
00063 using std::cout;
00064 using std::endl;
00065
00066 #include <cctype>
00067 #include <algorithm>
00068 namespace EMAN
00069 {
00070 class EMConsts {
00071 public:
00072 static const float I2G;
00073 static const float I3G;
00074 static const float I4G;
00075 static const float I5G;
00076
00077 static const double rad2deg;
00078 static const double deg2rad;
00079 static const double pi;
00080 };
00081
00082 class EMData;
00083 class XYData;
00084 class Aligner;
00085 class Averager;
00086 class Cmp;
00087 class Processor;
00088 class Projector;
00089 class Reconstructor;
00090 class Analyzer;
00091 class Transform;
00092 class Ctf;
00093
00094 enum MapInfoType {
00095 NORMAL,
00096 ICOS2F_FIRST_OCTANT,
00097 ICOS2F_FULL,
00098 ICOS2F_HALF,
00099 ICOS3F_HALF,
00100 ICOS3F_FULL,
00101 ICOS5F_HALF,
00102 ICOS5F_FULL,
00103 ICOS_UNKNOWN
00104 };
00105
00122 class EMObject
00123 {
00124 public:
00125 enum ObjectType {
00126 UNKNOWN,
00127 BOOL,
00128 UNSIGNEDINT,
00129 INT,
00130 FLOAT,
00131 DOUBLE,
00132 STRING,
00133 EMDATA,
00134 XYDATA,
00135 INTARRAY,
00136 FLOATARRAY,
00137 STRINGARRAY,
00138 TRANSFORM,
00139 CTF,
00140 FLOAT_POINTER,
00141 INT_POINTER,
00142 VOID_POINTER
00143 };
00144
00145
00150 EMObject();
00151 EMObject(bool boolean);
00152 EMObject(int num);
00153 EMObject(unsigned int num);
00154 EMObject(float ff);
00155 EMObject(double dd);
00156 EMObject(const char *s);
00157 EMObject(const string & s);
00158 EMObject(float * fp);
00159 EMObject(int * ip);
00160 EMObject(void * vp);
00161 EMObject(EMData * em);
00162 EMObject(XYData * xy);
00163 EMObject(Transform * t);
00164 EMObject(Ctf * ctf);
00165 EMObject(const vector< int >& v );
00166 EMObject(const vector < float >&v);
00167 EMObject(const vector <string>& sarray);
00168
00173 EMObject(const EMObject& that);
00174
00179 EMObject& operator=(const EMObject& that);
00180
00184 ~EMObject();
00187 operator bool () const;
00188 operator int () const;
00189 operator unsigned int () const;
00190 operator float () const;
00191 operator double () const;
00192 operator const char *() const;
00193 operator float * () const;
00194 operator int * () const;
00195 operator void * () const;
00196 operator EMData *() const;
00197 operator XYData *() const;
00198 operator Transform *() const;
00199 operator Ctf *() const;
00200 operator vector < int > () const;
00201 operator vector < float > () const;
00202 operator vector<string> () const;
00203
00208 bool is_null() const;
00209
00212 string to_str() const;
00213
00218 ObjectType get_type() const { return type; }
00219
00224 string get_type_string() const { return get_object_type_name(type); }
00225
00226
00232 string to_str(ObjectType type) const;
00233
00237 static string get_object_type_name(ObjectType t);
00238
00242 friend bool operator==(const EMObject &e1, const EMObject & e2);
00243
00247 friend bool operator!=(const EMObject &e1, const EMObject & e2);
00248
00249 private:
00250 union
00251 {
00252 bool b;
00253 int n;
00254 unsigned int ui;
00255 float f;
00256 double d;
00257 float * fp;
00258 int * ip;
00259 void * vp;
00260 EMData *emdata;
00261 XYData *xydata;
00262 };
00263
00264 string str;
00265 vector < int > iarray;
00266 vector < float >farray;
00267 vector < string> strarray;
00268 ObjectType type;
00269
00272 void printInfo() const;
00273
00274 void init();
00275
00276 static map< ObjectType, string> type_registry;
00277
00278 };
00279
00280 bool operator==(const EMObject &e1, const EMObject & e2);
00281 bool operator!=(const EMObject &e1, const EMObject & e2);
00282
00294 class TypeDict
00295 {
00296 public:
00297 TypeDict()
00298 {
00299 }
00300
00301 ~TypeDict()
00302 {
00303 }
00304
00305 vector < string > keys() const
00306 {
00307 vector < string > result;
00308 map < string, string >::const_iterator p;
00309
00310 for (p = type_dict.begin(); p != type_dict.end(); p++) {
00311 result.push_back(p->first);
00312 }
00313
00314 return result;
00315 }
00316
00317 size_t size() const
00318 {
00319 return type_dict.size();
00320 }
00321
00322 void put(const string& key, EMObject::ObjectType o, const string& desc = "")
00323 {
00324 type_dict[key] = EMObject::get_object_type_name(o);
00325 desc_dict[key] = desc;
00326 }
00327
00328 string get_type(const string& key)
00329 {
00330 return type_dict[key];
00331 }
00332
00333 string get_desc(const string& key)
00334 {
00335 return desc_dict[key];
00336 }
00337
00338 string operator[] (const string & key)
00339 {
00340 return type_dict[key];
00341 }
00342
00343 void dump();
00344
00345 inline bool find_type( const string& type ) { if ( type_dict.find(type) != type_dict.end() ) return true; return false; }
00346
00347 private:
00348 map < string, string > type_dict;
00349 map < string, string > desc_dict;
00350 };
00351
00352
00376 class Dict
00377 {
00378 public:
00379 Dict()
00380 {
00381 }
00382
00387 Dict(const string & key1, EMObject val1)
00388 {
00389 dict[key1] = val1;
00390 }
00391
00394 Dict(const string & key1, EMObject val1,
00395 const string & key2, EMObject val2)
00396 {
00397 dict[key1] = val1;
00398 dict[key2] = val2;
00399 }
00400
00403 Dict(const string & key1, EMObject val1,
00404 const string & key2, EMObject val2,
00405 const string & key3, EMObject val3)
00406 {
00407 dict[key1] = val1;
00408 dict[key2] = val2;
00409 dict[key3] = val3;
00410 }
00411
00414 Dict(const string & key1, EMObject val1,
00415 const string & key2, EMObject val2,
00416 const string & key3, EMObject val3,
00417 const string & key4, EMObject val4)
00418 {
00419 dict[key1] = val1;
00420 dict[key2] = val2;
00421 dict[key3] = val3;
00422 dict[key4] = val4;
00423 }
00424
00428 Dict(const map < string, EMObject > &d)
00429 {
00430 copy(d.begin(), d.end(), inserter(dict, dict.begin()));
00431
00432
00433 }
00434
00438 ~Dict() {}
00439
00443 Dict( const Dict& that);
00444
00448 Dict& operator=(const Dict& that);
00449
00452 vector < string > keys()const
00453 {
00454 vector < string > result;
00455
00456 map < string, EMObject >::const_iterator p;
00457 for (p = dict.begin(); p != dict.end(); p++) {
00458 result.push_back(p->first);
00459 }
00460
00461 return result;
00462 }
00463
00466 vector < EMObject > values()const
00467 {
00468 vector < EMObject > result;
00469
00470 map < string, EMObject >::const_iterator p;
00471 for (p = dict.begin(); p != dict.end(); p++) {
00472 result.push_back(p->second);
00473 }
00474
00475 return result;
00476 }
00477
00481 bool has_key_ci(const string & key) const;
00482
00486 bool has_key(const string & key) const
00487 {
00488 map < string, EMObject >::const_iterator p = dict.find(key);
00489 if (p != dict.end()) {
00490 return true;
00491 }
00492 return false;
00493 }
00494
00497 size_t size() const
00498 {
00499 return dict.size();
00500 }
00501
00505 EMObject get(const string & key) const
00506 {
00507 if( has_key(key) ) {
00508 return dict[key];
00509 }
00510 else {
00511 LOGERR("No such key exist in this Dict");
00512 throw NotExistingObjectException("EMObject", "Nonexisting key (" + key + ") in Dict");
00513 }
00514 }
00515
00519 EMObject get_ci(const string & key) const;
00523 void put(const string & key, EMObject val)
00524 {
00525 dict[key] = val;
00526 }
00527
00530 void erase(const string & key)
00531 {
00532 dict.erase(key);
00533 }
00534
00538 void clear()
00539 {
00540 dict.clear();
00541 }
00542
00546 template<typename type>
00547 type set_default(const string & key, type val)
00548 {
00549 if (!has_key(key)) {
00550 dict[key] = val;
00551 }
00552 return dict[key];
00553 }
00554
00555 Dict copy_exclude_keys(const vector<string>& excluded_keys) const
00556 {
00557 Dict ret(*this);
00558
00559 for ( vector<string>::const_iterator it = excluded_keys.begin(); it != excluded_keys.end(); ++it ) {
00560 if (ret.has_key(*it)) ret.erase(*it);
00561 }
00562
00563 return ret;
00564 }
00565
00566 Dict copy_exclusive_keys(const vector<string>& exclusive_keys) const
00567 {
00568 Dict ret;
00569 for ( vector<string>::const_iterator it = exclusive_keys.begin(); it != exclusive_keys.end(); ++it ) {
00570 if (has_key(*it)) ret[*it] = (*this)[*it];
00571 }
00572
00573 return ret;
00574 }
00575
00576 Dict copy_keys_in( const TypeDict& tdict ) const {
00577 vector<string> keys = tdict.keys();
00578 return copy_exclusive_keys(keys);
00579 }
00580
00581 EMObject & operator[] (const string & key)
00582 {
00583
00584
00585
00586
00587
00588 return dict[key];
00589
00590
00591
00592
00593
00594 }
00595
00596 EMObject operator[] (const string & key) const
00597 {
00598
00599
00600 return dict[key];
00601
00602
00603
00604
00605
00606 }
00607
00611 friend bool operator==(const Dict& d1, const Dict& d2);
00612
00616 friend bool operator!=(const Dict& d1, const Dict& d2);
00617
00618 private:
00619 mutable map < string, EMObject > dict;
00620
00621 public:
00629 class iterator : public map < string, EMObject >::iterator
00630 {
00631 public:
00632 typedef std::bidirectional_iterator_tag iterator_category;
00633 typedef pair<string, EMObject> value_type;
00634
00635 public:
00636 iterator( map < string, EMObject >::iterator parent_it );
00637 virtual ~iterator(){}
00638
00639 iterator( const iterator& that );
00640 iterator& operator=( const iterator& that );
00641 };
00642
00650 class const_iterator : public map < string, EMObject >::const_iterator
00651 {
00652 public:
00653 typedef std::bidirectional_iterator_tag iterator_category;
00654 typedef pair<string, EMObject> value_type;
00655 public:
00656 const_iterator( const map < string, EMObject >::const_iterator parent_it);
00657 virtual ~const_iterator(){}
00658 const_iterator( const Dict::iterator& it );
00659
00660 const_iterator( const const_iterator& that );
00661 const_iterator& operator=( const const_iterator& that );
00662 };
00663
00664
00665 iterator begin( void );
00666 const_iterator begin( void ) const;
00667
00668 iterator end( void );
00669 const_iterator end( void ) const;
00670
00671
00672 iterator find( const string& key );
00673 const_iterator find( const string& key ) const;
00674 };
00675
00676
00677
00678 bool operator==(const Dict &d1, const Dict& d2);
00679 bool operator!=(const Dict &d1, const Dict& d2);
00680
00681
00700 template < class T > class Factory
00701 {
00702 public:
00703 typedef T *(*InstanceType) ();
00704
00705 static void add(InstanceType i);
00706 static T *get(const string & instance_name);
00707 static T *get(const string & instance_name, const Dict & params);
00708 static vector < string > get_list();
00709
00710 private:
00711 Factory();
00712 Factory(const Factory < T > &);
00713 ~Factory();
00714 static void init();
00715 void force_add(InstanceType i);
00716
00717 static Factory < T > *my_instance;
00718 map < string, InstanceType > my_dict;
00719 };
00720
00721 template < class T > Factory < T > *Factory < T >::my_instance = 0;
00722
00723 template < class T > void Factory < T >::init()
00724 {
00725 if (!my_instance) {
00726 my_instance = new Factory < T > ();
00727 }
00728 }
00729
00730 template < class T > void Factory < T >::force_add(InstanceType new_instance)
00731 {
00732 T *i = new_instance();
00733 string name = i->get_name();
00734 my_dict[name] = new_instance;
00735 if( i )
00736 {
00737 delete i;
00738 i = 0;
00739 }
00740 }
00741
00742
00743 template < class T > void Factory < T >::add(InstanceType new_instance)
00744 {
00745 init();
00746
00747 T *i = new_instance();
00748 string name = i->get_name();
00749 typename map < string, InstanceType >::iterator fi =
00750 my_instance->my_dict.find(name);
00751
00752 if (fi == my_instance->my_dict.end()) {
00753 my_instance->my_dict[name] = new_instance;
00754 }
00755 if( i )
00756 {
00757 delete i;
00758 i = 0;
00759 }
00760 }
00761
00762 template < class T > T * Factory < T >::get(const string & instancename)
00763 {
00764 init();
00765 typename map < string, InstanceType >::iterator fi =
00766 my_instance->my_dict.find(instancename);
00767 if (fi != my_instance->my_dict.end()) {
00768 return my_instance->my_dict[instancename] ();
00769 }
00770
00771 string lower = instancename;
00772 for (unsigned int i=0; i<lower.length(); i++) lower[i]=tolower(lower[i]);
00773
00774 fi = my_instance->my_dict.find(lower);
00775 if (fi != my_instance->my_dict.end()) {
00776 return my_instance->my_dict[lower] ();
00777 }
00778
00779 throw NotExistingObjectException(instancename, "The named object doesn't exist");
00780 }
00781
00782 template < class T > T * Factory < T >::get(const string & instancename,
00783 const Dict & params)
00784 {
00785 init();
00786
00787 typename map < string, InstanceType >::iterator fi =
00788 my_instance->my_dict.find(instancename);
00789
00790 string lower = instancename;
00791 if (fi == my_instance->my_dict.end()) {
00792 for (unsigned int i=0; i<lower.length(); i++) lower[i]=tolower(lower[i]);
00793 fi = my_instance->my_dict.find(lower);
00794 }
00795
00796 if (fi != my_instance->my_dict.end()) {
00797 T *i = my_instance->my_dict[lower] ();
00798
00799 const vector<string> para_keys = params.keys();
00800
00801 const vector<string> valid_keys = i->get_param_types().keys();
00802 typename vector<string>::const_iterator it;
00803 for(it=para_keys.begin(); it!=para_keys.end(); ++it) {
00804
00805 if( find(valid_keys.begin(), valid_keys.end(), *it) == valid_keys.end() ) {
00806 throw InvalidParameterException(*it);
00807 }
00808 }
00809
00810 i->set_params(params);
00811 return i;
00812 }
00813
00814
00815 throw NotExistingObjectException(instancename, "No such an instance existing");
00816 }
00817
00818 template < class T > vector < string > Factory < T >::get_list() {
00819 init();
00820 vector < string > result;
00821 typename map < string, InstanceType >::const_iterator p;
00822 for (p = my_instance->my_dict.begin(); p != my_instance->my_dict.end(); p++) {
00823 result.push_back(p->first);
00824 }
00825
00826 return result;
00827 }
00828
00829 template < class T > void dump_factory()
00830 {
00831 vector < string > item_names = Factory < T >::get_list();
00832
00833 for (size_t i = 0; i < item_names.size(); i++) {
00834 T *item = Factory < T >::get(item_names[i]);
00835 printf("%s : %s\n", item->get_name().c_str(),item->get_desc().c_str());
00836 TypeDict td = item->get_param_types();
00837 td.dump();
00838 }
00839 }
00840
00841 template < class T > map<string, vector<string> > dump_factory_list()
00842 {
00843 vector < string > item_names = Factory < T >::get_list();
00844 map<string, vector<string> > factory_list;
00845
00846 typename vector<string>::const_iterator p;
00847 for(p = item_names.begin(); p !=item_names.end(); ++p) {
00848 T *item = Factory<T>::get(*p);
00849
00850 string name = item->get_name();
00851
00852 vector<string> content;
00853 content.push_back(item->get_desc());
00854 TypeDict td = item->get_param_types();
00855 vector<string> keys = td.keys();
00856 for(unsigned int i=0; i<td.size(); ++i) {
00857 content.push_back(keys[i]);
00858 content.push_back( td.get_type(keys[i]) );
00859 content.push_back( td.get_desc(keys[i]) );
00860 }
00861 factory_list[name] = content;
00862 }
00863
00864 return factory_list;
00865 }
00866
00873 class FactoryBase
00874 {
00875 public:
00876 FactoryBase() {}
00877 virtual ~FactoryBase() {};
00878
00882 virtual string get_name() const = 0;
00883
00887 virtual string get_desc() const = 0;
00888
00892 Dict get_params() const { return params; }
00893
00897 void set_params(const Dict & new_params)
00898 {
00899 params.clear();
00900 insert_params(new_params);
00901 }
00902
00905 virtual TypeDict get_param_types() const = 0;
00906
00910 void insert_params(const Dict & new_params)
00911 {
00912
00913
00914 TypeDict permissable_params = get_param_types();
00915 for ( Dict::const_iterator it = new_params.begin(); it != new_params.end(); ++it )
00916 {
00917
00918 if ( !permissable_params.find_type(it->first) )
00919 {
00920 throw InvalidParameterException(it->first);
00921 }
00922 params[it->first] = it->second;
00923 }
00924 }
00925
00926 Dict copy_relevant_params(const FactoryBase* const that) const
00927 {
00928 return params.copy_keys_in(that->get_param_types());
00929
00930 }
00931
00932 protected:
00934 mutable Dict params;
00935 };
00936 }
00937
00938 #endif