emobject.cpp

Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
00007  * Probable contributor: Liwei Peng (what dates?)
00008  * Contributing author: David Woolford 06/11/2007
00009  *
00010  * Copyright (c) 2000-2007 Baylor College of Medicine
00011  *
00012  * This software is issued under a joint BSD/GNU license. You may use the
00013  * source code in this file under either license. However, note that the
00014  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00015  * so you are responsible for compliance with the licenses of these packages
00016  * if you opt to use BSD licensing. The warranty disclaimer below holds
00017  * in either instance.
00018  *
00019  * This complete copyright notice must be included in any revised version of the
00020  * source code. Additional authorship citations may be added, but existing
00021  * author citations must be preserved.
00022  *
00023  * This program is free software; you can redistribute it and/or modify
00024  * it under the terms of the GNU General Public License as published by
00025  * the Free Software Foundation; either version 2 of the License, or
00026  * (at your option) any later version.
00027  *
00028  * This program is distributed in the hope that it will be useful,
00029  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00030  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00031  * GNU General Public License for more details.
00032  *
00033  * You should have received a copy of the GNU General Public License
00034  * along with this program; if not, write to the Free Software
00035  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00036  *
00037  * */
00038 
00039 #include "emobject.h"
00040 #include <cmath>
00041 #ifdef WIN32
00042 #define M_PI 3.14159265358979323846f
00043 #endif
00044 
00045 #include <algorithm>
00046 // using copy
00047 
00048 #include "util.h"
00049 
00050 using namespace EMAN;
00051 
00052 #include <iostream>
00053 using std::cout;
00054 using std::cerr;
00055 using std::endl;
00056 
00057 const float EMConsts::I2G = (float) (4.0 / (M_PI*M_PI));
00058 const float EMConsts::I3G = (float) (6.4 / (M_PI*M_PI));
00059 const float EMConsts::I4G = (float) (8.8 / (M_PI*M_PI));
00060 const float EMConsts::I5G = (float) (10.4 / (M_PI*M_PI));
00061 // This stolen from wikipedia.org
00062 const double EMConsts::pi = 3.141592653589793238462643383279502884197169399;
00063 const double EMConsts::deg2rad = pi/180.0;
00064 const double EMConsts::rad2deg = 180.0/pi;
00065 
00066 
00067 #include <sstream>
00068 using std::stringstream;
00069 
00070 #include "transform.h"
00071 #include "ctf.h"
00072 
00073 #ifdef MEMDEBUG
00074 set<EMObject*> allemobjlist;
00075 #endif
00076 
00077 
00078 // Static init
00079 map< EMObject::ObjectType, string>  EMObject::type_registry = init();;
00080 
00081 //-------------------------------EMObjectTypes-----------------------------------------
00082 map< EMObject::ObjectType, string> EMObject::init()
00083 {
00084         map< EMObject::ObjectType, string> mymap;
00085         static bool first_construction = true;
00086         if ( first_construction )
00087         {
00088                 // Initialize the the type registry once and for all
00089                 mymap[BOOL] = "BOOL";
00090                 mymap[INT] = "INT";
00091                 mymap[UNSIGNEDINT] = "UNSIGNEDINT";
00092                 mymap[FLOAT] = "FLOAT";
00093                 mymap[DOUBLE] = "DOUBLE";
00094                 mymap[STRING] = "STRING";
00095                 mymap[EMDATA] = "EMDATA";
00096                 mymap[XYDATA] = "XYDATA";
00097                 mymap[INTARRAY] = "INTARRAY";
00098                 mymap[FLOATARRAY] = "FLOATARRAY";
00099                 mymap[STRINGARRAY] = "STRINGARRAY";
00100                 mymap[TRANSFORM] = "TRANFORM";
00101                 mymap[CTF] = "CTF";
00102                 mymap[FLOAT_POINTER] = "FLOAT_POINTER";
00103                 mymap[INT_POINTER] = "INT_POINTER";
00104                 mymap[UNKNOWN] = "UNKNOWN";
00105                 mymap[VOID_POINTER] = "VOID_POINTER";
00106                 mymap[TRANSFORMARRAY] = "TRANSFORMARRAY";
00107                 first_construction = false;
00108         }
00109         
00110         return mymap;
00111 }
00112 
00113 //-------------------------------EMObject--------------------------------------------
00114 
00115 void EMObject::printInfo() const
00116 {
00117         cout << "The address of my type is " << &type << endl;
00118         cout << " Now printing the enumerated values in type_registry " << endl;
00119         for( map< ObjectType, string>::const_iterator it = type_registry.begin(); it != type_registry.end(); ++it )
00120         {
00121                 cout << it->first << " " << it->second << endl;
00122         }
00123         cout << "My type is " << to_str(type) << " and its enumerated value is " << type << endl;
00124         cout << "The address of the static type registry is " << &type_registry <<", it should be same for all EMObjects" << endl;
00125 }
00126 
00127 EMObject::~EMObject() {
00128 #ifdef MEMDEBUG
00129         allemobjlist.erase(this);
00130         printf("  -(%6d) %p\n",(int)allemobjlist.size(),this);
00131 #endif
00132 }
00133 
00134 EMObject::EMObject() :
00135         d(0), type(UNKNOWN)
00136 {
00137 #ifdef MEMDEBUG
00138         allemobjlist.insert(this);
00139         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00140 #endif
00141 }
00142 
00143 EMObject::EMObject(bool boolean) :
00144         b(boolean), type(BOOL)
00145 {
00146 #ifdef MEMDEBUG
00147         allemobjlist.insert(this);
00148         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00149 #endif
00150 }
00151 
00152 EMObject::EMObject(int num) :
00153         n(num), type(INT)
00154 {
00155 #ifdef MEMDEBUG
00156         allemobjlist.insert(this);
00157         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00158 #endif
00159 }
00160 
00161 EMObject::EMObject(unsigned int num) :
00162         ui(num), type(UNSIGNEDINT)
00163 {
00164 #ifdef MEMDEBUG
00165         allemobjlist.insert(this);
00166         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00167 #endif
00168 }
00169 
00170 EMObject::EMObject(float ff) :
00171         type(FLOAT)
00172 {
00173 
00174 #ifdef MEMDEBUG
00175         allemobjlist.insert(this);
00176         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00177 #endif
00178         if(Util::goodf(&ff)) {
00179                 f = ff;
00180         }
00181         else{
00182                 f = 0.0f;
00183         }
00184 }
00185 
00186 EMObject::EMObject(double dd) :
00187         type(DOUBLE)
00188 {
00189 
00190 #ifdef MEMDEBUG
00191         allemobjlist.insert(this);
00192         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00193 #endif
00194         if(Util::goodf(&dd)) {
00195                 d = dd;
00196         }
00197         else{
00198                 d = 0.0;
00199         }
00200 }
00201 
00202 EMObject::EMObject(const char *s) :
00203         str(s), type(STRING)
00204 {
00205 #ifdef MEMDEBUG
00206         allemobjlist.insert(this);
00207         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00208 #endif
00209 }
00210 
00211 EMObject::EMObject(const string & s) :
00212         str(s), type(STRING)
00213 {
00214 #ifdef MEMDEBUG
00215         allemobjlist.insert(this);
00216         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00217 #endif
00218 }
00219 
00220 EMObject::EMObject(float *f) :
00221         fp(f), type(FLOAT_POINTER)
00222 {
00223 #ifdef MEMDEBUG
00224         allemobjlist.insert(this);
00225         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00226 #endif
00227 }
00228 
00229 EMObject::EMObject(int *i) :
00230         ip(i), type(INT_POINTER)
00231 {
00232 #ifdef MEMDEBUG
00233         allemobjlist.insert(this);
00234         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00235 #endif
00236 }
00237 
00238 EMObject::EMObject(void *v) :
00239         vp(v), type(VOID_POINTER)
00240 {
00241 #ifdef MEMDEBUG
00242         allemobjlist.insert(this);
00243         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00244 #endif
00245 }
00246 
00247 EMObject::EMObject(EMData * em) :
00248         emdata(em), type(EMDATA)
00249 {
00250 #ifdef MEMDEBUG
00251         allemobjlist.insert(this);
00252         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00253 #endif
00254 }
00255 
00256 EMObject::EMObject(XYData * xy) :
00257         xydata(xy), type(XYDATA)
00258 {
00259 #ifdef MEMDEBUG
00260         allemobjlist.insert(this);
00261         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00262 #endif
00263 }
00264 
00265 EMObject::EMObject(Transform* t) :
00266         farray(t->get_matrix()), type(TRANSFORM)
00267 {
00268 #ifdef MEMDEBUG
00269         allemobjlist.insert(this);
00270         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00271 #endif
00272 }
00273 
00274 EMObject::EMObject(Ctf * ctf) :
00275         str(ctf->to_string()), type(CTF)
00276 {
00277 #ifdef MEMDEBUG
00278         allemobjlist.insert(this);
00279         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00280 #endif
00281 }
00282 
00283 EMObject::EMObject(const vector< int >& v ) :
00284         iarray(v), type(INTARRAY)
00285 {
00286 #ifdef MEMDEBUG
00287         allemobjlist.insert(this);
00288         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00289 #endif
00290 }
00291 
00292 EMObject::EMObject(const vector < float >&v) :
00293         farray(v), type(FLOATARRAY)
00294 {
00295 #ifdef MEMDEBUG
00296         allemobjlist.insert(this);
00297         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00298 #endif
00299 }
00300 
00301 EMObject::EMObject(const vector <string>& sarray) :
00302         strarray(sarray), type(STRINGARRAY)
00303 {
00304 #ifdef MEMDEBUG
00305         allemobjlist.insert(this);
00306         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00307 #endif
00308 }
00309 
00310 EMObject::EMObject(const vector <Transform>& tarray) :
00311         transformarray(tarray), type(TRANSFORMARRAY)
00312 {
00313 #ifdef MEMDEBUG
00314         allemobjlist.insert(this);
00315         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00316 #endif
00317 }
00318 
00319 EMObject::operator bool () const
00320 {
00321         if (type == BOOL) {
00322                 return b;
00323         }
00324         else if (type == INT) {
00325                 return n != 0;
00326         }
00327         else if (type == UNSIGNEDINT) {
00328                 return ui != 0;
00329         }
00330         else if (type == FLOAT) {
00331                 return f != 0;
00332         }
00333         else if (type == DOUBLE) {
00334                 return d != 0;
00335         }
00336         else if (type == EMDATA) {
00337                 return emdata != 0;
00338         }
00339         else if (type == XYDATA) {
00340                 return xydata != 0;
00341         }
00342         else if (type == FLOAT_POINTER) {
00343                 return fp != 0;
00344         }
00345         else if (type == INT_POINTER) {
00346                 return ip != 0;
00347         }
00348         else if (type == VOID_POINTER) {
00349                 return vp != 0;
00350         }
00351 //      else if (type == TRANSFORM) {
00352 //              return transform != 0;
00353 //      }
00354         // It seemed unconventional to return a boolean for the stl objects
00355         else {
00356                 if (type != UNKNOWN) {
00357                         throw TypeException("Cannot convert to bool this data type ",
00358                                                                 get_object_type_name(type));
00359                 }
00360         }
00361         return 0;
00362 }
00363 
00364 EMObject::operator int () const
00365 {
00366         if (type == INT) {
00367                 return n;
00368         }
00369         else if (type == UNSIGNEDINT) {
00370                 return (int) ui;
00371         }
00372         else if (type == FLOAT) {
00373                 return (int) f;
00374         }
00375         else if (type == DOUBLE) {
00376                 return (int) d;
00377         }
00378         else if (type == BOOL) {
00379                 return b?1:0;
00380         }
00381         else {
00382                 if (type != UNKNOWN) {
00383                         throw TypeException("Cannot convert to int this data type ",
00384                                                                 get_object_type_name(type));
00385                 }
00386         }
00387         return 0;
00388 }
00389 
00390 EMObject::operator unsigned int () const
00391 {
00392         if (type == UNSIGNEDINT) {
00393                 return (unsigned int) ui;
00394         }
00395         else {
00396                 if (type != UNKNOWN) {
00397                         throw TypeException("Cannot convert to int this data type ",
00398                                                                 get_object_type_name(type));
00399                 }
00400         }
00401         return 0;
00402 }
00403 
00404 EMObject::operator float () const
00405 {
00406         if (type == BOOL) {
00407                 return b?1.0f:0.0f;
00408         }
00409         else if (type == FLOAT) {
00410                 return f;
00411         }
00412         else if (type == INT) {
00413                 return (float) n;
00414         }
00415         else if (type == UNSIGNEDINT) {
00416                 return (float) ui;
00417         }
00418         else if (type == DOUBLE) {
00419                 return (float) d;
00420         }
00421         else {
00422                 if (type != UNKNOWN) {
00423                         throw TypeException("Cannot convert to float from this data type",
00424                                                                 get_object_type_name(type));
00425                 }
00426         }
00427 
00428         return 0;
00429 }
00430 
00431 EMObject::operator double () const
00432 {
00433         if (type == BOOL) {
00434         return b?1.0:0.0;
00435         }
00436         else if (type == DOUBLE) {
00437                 return d;
00438         }
00439         else if (type == INT) {
00440                 return (double) n;
00441         }
00442         else if (type == UNSIGNEDINT) {
00443                 return (double) ui;
00444         }
00445         else if (type == FLOAT) {
00446                 return (double) f;
00447         }
00448         else {
00449                 if (type != UNKNOWN) {
00450                         throw TypeException("Cannot convert to double from this data type",
00451                                                                 get_object_type_name(type));
00452                 }
00453         }
00454         return 0;
00455 }
00456 
00457 EMObject::operator int * () const
00458 {
00459         if (type != INT_POINTER)
00460         {
00461                 if (type != UNKNOWN)
00462                         throw TypeException("Cannot convert to float pointer from this data type",
00463                                                                 get_object_type_name(type));
00464 
00465                 return 0;
00466         }
00467 
00468         return ip;
00469 }
00470 
00471 
00472 EMObject::operator float * () const
00473 {
00474         if (type != FLOAT_POINTER)
00475         {
00476                 if (type != UNKNOWN)
00477                         throw TypeException("Cannot convert to float pointer from this data type",
00478                                                                 get_object_type_name(type));
00479 
00480                 return 0;
00481         }
00482 
00483         return fp;
00484 }
00485 
00486 // MAYBE REMOVE? FIXME
00487 EMObject::operator void * () const
00488 {
00489         if (type == VOID_POINTER) return vp;
00490         else if (type == FLOAT_POINTER) return (void *)fp;
00491         else if (type == INT_POINTER) return (void *)ip;
00492         else if (type == EMDATA) return (void *) emdata;
00493         else if (type == XYDATA) return (void *) xydata;
00494 //      else if (type == TRANSFORM) return (void *) transform;
00495         else throw TypeException("Cannot convert to void pointer from this data type", get_object_type_name(type));
00496 }
00497 
00498 EMObject::operator const char * () const
00499 {
00500         if (type != STRING && type != CTF) {
00501                 stringstream ss;
00502                 string return_string;
00503                 if ( type == INT )
00504                 {
00505                         ss << n;
00506                         ss >> return_string;
00507                         return return_string.c_str();
00508                 }
00509                 if ( type == UNSIGNEDINT )
00510                 {
00511                         ss << ui;
00512                         ss >> return_string;
00513                         return return_string.c_str();
00514                 }
00515                 else
00516                 if ( type == FLOAT )
00517                 {
00518                         ss << f;
00519                         ss >> return_string;
00520                         return return_string.c_str();
00521                 }
00522                 else
00523                 if ( type == DOUBLE )
00524                 {
00525                         ss << d;
00526                         ss >> return_string;
00527                         return return_string.c_str();
00528                 }
00529                 else if (type != UNKNOWN) {
00530                         throw TypeException("Cannot convert to string from this data type",
00531                                                                 get_object_type_name(type));
00532                 }
00533 
00534                 return "";
00535         }
00536         return str.c_str();
00537 }
00538 
00539 EMObject::operator EMData * () const
00540 {
00541         if (type != EMDATA) {
00542                 if (type != UNKNOWN) {
00543                         throw TypeException("Cannot convert to EMData* from this data type",
00544                                    get_object_type_name(type));
00545                 }
00546                 return 0;
00547         }
00548         return emdata;
00549 }
00550 
00551 EMObject::operator XYData * () const
00552 {
00553         if (type != XYDATA) {
00554                 if (type != UNKNOWN) {
00555                         throw TypeException("Cannot convert to XYData* from this data type",
00556                                    get_object_type_name(type));
00557                 }
00558                 return 0;
00559         }
00560         return xydata;
00561 }
00562 
00563 EMObject::operator Transform* () const
00564 {
00565         if(type != TRANSFORM) {
00566                 if(type != UNKNOWN) {
00567                         throw TypeException("Cannot convert to TRANSFORM* from this data type",
00568                                                                 get_object_type_name(type));
00569                 }
00570         }
00571         Transform * transform = new Transform();
00572         transform->set_matrix(farray);
00573         return transform;
00574 }
00575 
00576 EMObject::operator Ctf* () const
00577 {
00578 /*      if(type != CTF) {
00579                 if(type != CTF) {
00580                         throw TypeException("Cannot convert to TRANSFORM* from this data type",
00581                                                                 get_object_type_name(type));
00582                 }
00583         }*/
00584         Ctf * ctf = 0;
00585         if(str[0] == 'O') {
00586                 ctf = new EMAN1Ctf();
00587                 ctf->from_string(str);
00588         }
00589         else if(str[0] == 'E') {
00590                 ctf = new EMAN2Ctf();
00591                 ctf->from_string(str);
00592         }
00593         return ctf;
00594 }
00595 
00596 EMObject::operator vector<int>() const
00597 {
00598     if( type != INTARRAY )
00599     {
00600         if( type != UNKNOWN ) {
00601                 throw TypeException("Cannot convert to vector<int> from this data type", get_object_type_name(type) );
00602                 }
00603                 return vector<int>();
00604     }
00605     return iarray;
00606 }
00607 
00608 EMObject::operator vector < float > () const
00609 {
00610         if (type != FLOATARRAY) {
00611                 if (type != UNKNOWN) {
00612                         throw TypeException("Cannot convert to vector<float> from this data type",
00613                                                                 get_object_type_name(type));
00614                 }
00615                 return vector < float >();
00616         }
00617         return farray;
00618 }
00619 
00620 EMObject::operator vector<string> () const
00621 {
00622         if (type != STRINGARRAY) {
00623                 if (type != UNKNOWN) {
00624                         throw TypeException("Cannot convert to vector<string> from this data type",
00625                                                                 get_object_type_name(type));
00626                 }
00627                 return vector<string>();
00628         }
00629         return strarray;
00630 }
00631 
00632 EMObject::operator vector<Transform> () const
00633 {
00634         if(type != TRANSFORMARRAY) {
00635                 if (type != UNKNOWN) {
00636                         throw TypeException("Cannot convert to vector<string> from this data type",
00637                                                                 get_object_type_name(type));
00638                 }
00639                 return vector<Transform>();
00640         }
00641         return transformarray;
00642 }
00643 
00644 bool EMObject::is_null() const
00645 {
00646         return (type == UNKNOWN);
00647 }
00648 
00649 string EMObject::to_str() const
00650 {
00651         return to_str(type);
00652 }
00653 
00654 string EMObject::to_str(ObjectType argtype) const
00655 {
00656         if (argtype == STRING) {
00657                 return str;
00658         }
00659         else {
00660                 char tmp_str[32];
00661                 if (argtype == BOOL) {
00662                         if (b)
00663                                 sprintf(tmp_str, "true");
00664                         else
00665                                 sprintf(tmp_str, "false");
00666                 }
00667                 else if (argtype == INT) {
00668                         sprintf(tmp_str, "%d", n);
00669                 }
00670                 else if (argtype == UNSIGNEDINT) {
00671                         sprintf(tmp_str, "%d", ui);
00672                 }
00673                 else if (argtype == FLOAT) {
00674                         sprintf(tmp_str, "%f", f);
00675                 }
00676                 else if (argtype == DOUBLE) {
00677                         sprintf(tmp_str, "%f", d);
00678                 }
00679                 else if (argtype == EMDATA) {
00680                         sprintf(tmp_str, "EMDATA");
00681                 }
00682                 else if (argtype == FLOAT_POINTER) {
00683                         sprintf(tmp_str, "FLOAT_POINTER");
00684                 }
00685                 else if (argtype == INT) {
00686                         sprintf(tmp_str, "INT_POINTER");
00687                 }
00688                 else if (argtype == VOID_POINTER) {
00689                         sprintf(tmp_str, "VOID_POINTER");
00690                 }
00691                 else if (argtype == XYDATA) {
00692                         sprintf(tmp_str, "XYDATA");
00693                 }
00694                 else if (argtype == INTARRAY) {
00695                         sprintf(tmp_str, "INTARRAY");
00696                 }
00697                 else if (argtype == FLOATARRAY) {
00698                         sprintf(tmp_str, "FLOATARRAY");
00699                 }
00700                 else if (argtype == STRINGARRAY) {
00701                         sprintf(tmp_str, "STRINGARRAY");
00702                 }
00703                 else if (argtype == TRANSFORM) {
00704                         sprintf(tmp_str, "TRANSFORM");
00705                 }
00706                 else if (argtype == TRANSFORMARRAY) {
00707                         sprintf(tmp_str, "TRANSFORMARRAY");
00708                 }
00709                 else if (argtype == CTF) {
00710                         sprintf(tmp_str, "CTF");
00711                 }
00712                 else if (argtype == UNKNOWN) {
00713                         sprintf(tmp_str, "UNKNOWN");
00714                 }
00715                 else {
00716                         LOGERR("No such EMObject defined");
00717                         throw NotExistingObjectException("EMObject", "unknown type");
00718                 }
00719                 return string(tmp_str);
00720         }
00721 }
00722 
00723 string EMObject::get_object_type_name(ObjectType t)
00724 {
00725 #ifdef _WIN32
00726         if (t == BOOL) {
00727                 return "BOOL";
00728         }else
00729         if ( t == INT){
00730                 return "INT";
00731         }else
00732         if ( t == UNSIGNEDINT){
00733                 return "UNSIGNEDINT";
00734         } else
00735         if ( t == FLOAT){
00736                 return "FLOAT";
00737         } else
00738         if ( t == DOUBLE){
00739                 return "DOUBLE";
00740         }else
00741         if ( t == STRING){
00742                 return "STRING";
00743         }else
00744         if ( t == EMDATA){
00745                 return "EMDATA";
00746         }
00747         else
00748         if ( t == XYDATA){
00749                 return "XYDATA";
00750         }else
00751         if ( t == INTARRAY){
00752                 return "INTARRAY";
00753         }else
00754         if ( t == FLOATARRAY){
00755                 return "FLOATARRAY";
00756         } else
00757         if ( t == STRINGARRAY){
00758                 return "STRINGARRAY";
00759         }else
00760         if ( t == TRANSFORM){
00761                 return "TRANSFORM";
00762         }else
00763         if ( t == TRANSFORMARRAY){
00764                 return "TRANSFORMARRAY";
00765         }
00766         if ( t == CTF){
00767                 return "CTF";
00768         }else
00769         if ( t == FLOAT_POINTER){
00770                 return "FLOAT_POINTER";
00771         }else
00772         if ( t == INT_POINTER){
00773                 return "INT_POINTER";
00774         }else
00775         if ( t == UNKNOWN){
00776                 return "UNKNOWN";
00777         } else
00778         if ( t == VOID_POINTER){
00779                 return "VOID_POINTER";
00780         }
00781         else {
00782                 LOGERR("No such EMObject defined");
00783                 throw NotExistingObjectException("EMObject", "unknown type");
00784         }
00785 
00786 #else
00787 
00788         if  ( type_registry.find(t) != type_registry.end() )
00789                 return type_registry[t];
00790         else
00791                 LOGERR("No such EMObject defined");
00792                 throw NotExistingObjectException("EMObject", "unknown type");
00793 #endif  //_WIN32
00794 }
00795 
00796 bool EMAN::operator==(const EMObject &e1, const EMObject & e2)
00797 {
00798 
00799         if (e1.type != e2.type) {
00800                 return false;
00801         }
00802 
00803         switch (e1.type) {
00804         case  EMObject::BOOL:
00805                 return (e1.b == e2.b);
00806         break;
00807         case  EMObject::INT:
00808                 return (e1.n == e2.n);
00809         break;
00810         case  EMObject::UNSIGNEDINT:
00811                 return (e1.ui == e2.ui);
00812         break;
00813         case  EMObject::FLOAT:
00814                 return (e1.f == e2.f);
00815         break;
00816         case  EMObject::DOUBLE:
00817                 return (e1.d == e2.d);
00818         break;
00819         case EMObject::CTF:
00820         case  EMObject::STRING:
00821                 return (e1.str == e2.str);
00822         break;
00823         case  EMObject::FLOAT_POINTER:
00824                 return (e1.fp == e2.fp);
00825         break;
00826         case  EMObject::INT_POINTER:
00827                 return (e1.ip == e2.ip);
00828         break;
00829         case  EMObject::VOID_POINTER:
00830                 return (e1.vp == e2.vp);
00831         break;
00832         case  EMObject::EMDATA:
00833                 return (e1.emdata == e2.emdata);
00834         break;
00835         case  EMObject::XYDATA:
00836                 return (e1.xydata == e2.xydata);
00837         break;
00838         case  EMObject::TRANSFORM:
00839         case  EMObject::FLOATARRAY:
00840                 if (e1.farray.size() == e2.farray.size()) {
00841                         for (size_t i = 0; i < e1.farray.size(); i++) {
00842                                 if (e1.farray[i] != e2.farray[i]) {
00843                                         return false;
00844                                 }
00845                         }
00846                         return true;
00847                 }
00848                 else {
00849                         return false;
00850                 }
00851         break;
00852         case  EMObject::INTARRAY:
00853                 if (e1.iarray.size() == e2.iarray.size()) {
00854                         for (size_t i = 0; i < e1.iarray.size(); i++) {
00855                                 if (e1.iarray[i] != e2.iarray[i]) {
00856                                         return false;
00857                                 }
00858                         }
00859                         return true;
00860                 }
00861         break;
00862         case  EMObject::STRINGARRAY:
00863                 if (e1.strarray.size() == e2.strarray.size()) {
00864                         for (size_t i = 0; i < e1.strarray.size(); i++) {
00865                                 if (e1.strarray[i] != e2.strarray[i]) {
00866                                         return false;
00867                                 }
00868                         }
00869                         return true;
00870                 }
00871                 else {
00872                         return false;
00873                 }
00874         break;
00875         case EMObject::TRANSFORMARRAY:
00876                 if (e1.transformarray.size() == e2.transformarray.size()) {
00877                         for (size_t i = 0; i < e1.transformarray.size(); i++) {
00878                                 if (e1.transformarray[i] != e2.transformarray[i]) {
00879                                         return false;
00880                                 }
00881                         }
00882                 }
00883         break;
00884         case  EMObject::UNKNOWN:
00885                 // UNKNOWN really means "no type" and if two objects both have
00886                 // type UNKNOWN they really are the same
00887                 return (e1.type == e2.type);
00888         break;
00889         default:
00890                 return false;
00891         break;
00892         }
00893         return false;
00894 }
00895 
00896 bool EMAN::operator!=(const EMObject &e1, const EMObject & e2)
00897 {
00898         return !(e1 == e2);
00899 }
00900 
00901 // Copy constructor
00902 EMObject::EMObject(const EMObject& that)
00903 {
00904         // init isn't necessary because that must have already called it!
00905         *this = that;
00906 #ifdef MEMDEBUG
00907         allemobjlist.insert(this);
00908         printf("  +(%6d) %p\n",(int)allemobjlist.size(),this);
00909 #endif
00910 }
00911 
00912 
00913 // Assignment operator -  - copies only the variable associated with the type of the argument.
00914 // It would be possible just to do a dumb copy of everything, but that seems opposed to
00915 // the concept of an EMObject, which is always of a single type.
00916 EMObject& EMObject::operator=( const EMObject& that )
00917 {
00918 
00919 // This test breaks assignment when either the current or assigned values are (float)nan
00920 // it's also somewhat inherently stupid, since testing for equivalence may cost as much
00921 // as assignment.
00922 //      if ( *this != that )
00923         {
00924                 // First store the type of the input, At first I forgot to do this and it was a very
00925                 // difficult bug to track down
00926                 type = that.type;
00927 
00928 //              if ( type != this_type ) throw
00929 
00930 
00931                 switch (type)
00932                 {
00933                 case BOOL:
00934                         b = that.b;
00935                 break;
00936                 case INT:
00937                         n = that.n;
00938                 break;
00939                 case UNSIGNEDINT:
00940                         ui = that.ui;
00941                 break;
00942                 case FLOAT:
00943                         f = that.f;
00944                 break;
00945                 case DOUBLE:
00946                         d = that.d;
00947                 break;
00948                 case CTF:
00949                 case STRING:
00950                         str = that.str;
00951                 break;
00952                 case FLOAT_POINTER:
00953                         // Warning - Pointer address copy.
00954                         fp = that.fp;
00955                 break;
00956                 case INT_POINTER:
00957                 // Warning - Pointer address copy.
00958                         ip = that.ip;
00959                 break;
00960                 case VOID_POINTER:
00961                         // Warning - Pointer address copy.
00962                         vp = that.vp;
00963                 break;
00964                 case EMDATA:
00965                         // Warning - Pointer address copy.
00966                         emdata = that.emdata;
00967                 break;
00968                 case XYDATA:
00969                         // Warning - Pointer address copy.
00970                         xydata = that.xydata;
00971                 break;
00972                 case TRANSFORM:
00973                 case FLOATARRAY:
00974                         farray = that.farray;
00975                 break;
00976                 case INTARRAY:
00977                         iarray = that.iarray;
00978                 break;
00979                 case STRINGARRAY:
00980                         strarray = that.strarray;
00981                 break;
00982                 case TRANSFORMARRAY:
00983                         transformarray = that.transformarray;
00984                 break;
00985                 case UNKNOWN:
00986                         // This is possible, nothing should happen
00987                         // The EMObject's default constructor has been called and
00988                         // as yet has no type - doing nothing is exactly as the
00989                         // the assignment operator should work.
00990                 break;
00991                 default:
00992                         LOGERR("No such EMObject defined");
00993                         throw NotExistingObjectException("EMObject", "unknown type");
00994                 break;
00995                 }
00996         }
00997 //      else
00998 //      {
00999 //              cerr << "Warning - attempt to assign EMObject onto itself. No action taken" << endl;
01000 //              cerr << "My type is " << get_object_type_name(type) << endl;
01001 //      }
01002 
01003         return *this;
01004 }
01005 
01006 //-------------------------------TypeDict--------------------------------------------
01007 
01008 void TypeDict::dump()
01009 {
01010         map < string, string >::iterator p;
01011         for (p = type_dict.begin(); p != type_dict.end(); p++) {
01012                 printf("\t%s    %s  %s\n",
01013                            p->first.c_str(), p->second.c_str(), desc_dict[p->first].c_str());
01014         }
01015 }
01016 
01017 //-------------------------------Dict--------------------------------------------
01018 
01019 Dict::Dict(const Dict& that)
01020 {
01021         *this = that;
01022 }
01023 
01024 Dict& Dict::operator=(const Dict& that)
01025 {
01026         if ( this != &that )
01027         {
01028                 dict.clear();
01029                 copy(that.begin(), that.end(), inserter(dict, dict.begin()));
01030                 // or use this
01031                 // dict.insert( that.begin(), that.end());
01032         }
01033         else
01034         {
01035                 cerr << "Warning - attempted to assign a Dict object to itself. No action taken" << endl;
01036         }
01037 
01038         return *this;
01039 }
01040 
01041 bool EMAN::operator==(const Dict& d1, const Dict& d2)
01042 {
01043         // Just make use of map's version of operator==
01044         return (d1.dict == d2.dict);
01045 }
01046 
01047 bool EMAN::operator!=(const Dict& d1, const Dict& d2)
01048 {
01049         return !(d1 == d2);
01050 }
01051 
01052 
01053 // Iterator support
01054 // This is just a wrapper, everything is inherited from the map<string,EMObject>::iterator
01055 // so the interface is the same as you would expect
01056 // iterator support added by d.woolford May 2007
01057 
01058 Dict::iterator Dict::begin( void )
01059 {
01060         return iterator( dict.begin() );
01061 }
01062 
01063 Dict::const_iterator Dict::begin( void ) const
01064 {
01065         return const_iterator( (map < string, EMObject >::const_iterator) dict.begin() );
01066 }
01067 
01068 // Wraps map.find(const string& key)
01069 Dict::iterator Dict::find( const string& key )
01070 {
01071         return iterator( dict.find(key) );
01072 }
01073 
01074 Dict::iterator Dict::end( void )
01075 {
01076         return iterator( dict.end() );
01077 }
01078 
01079 Dict::const_iterator Dict::end( void ) const
01080 {
01081         return const_iterator( (map < string, EMObject >::const_iterator)dict.end() );
01082 }
01083 
01084 Dict::const_iterator Dict::find( const string& key ) const
01085 {
01086         return const_iterator( (map < string, EMObject >::const_iterator)dict.find(key) );
01087 }
01088 
01089 //
01090 // iterator
01091 //
01092 Dict::iterator::iterator( map< string, EMObject >::iterator parent_it  ) :
01093         map< string, EMObject >::iterator( parent_it )
01094 {
01095 }
01096 
01097 
01098 Dict::iterator::iterator( const iterator& that ) :
01099         map < string, EMObject >::iterator( that )
01100 {
01101 }
01102 
01103 
01104 Dict::iterator& Dict::iterator::operator=( const iterator& that )
01105 {
01106         if( this != &that )
01107         {
01108                 map < string, EMObject >::iterator::operator=( that );
01109         }
01110         return *this;
01111 }
01112 
01113 //
01114 // const_iterator
01115 //
01116 
01117 Dict::const_iterator::const_iterator( const map < string, EMObject >::const_iterator parent_it  ) :
01118         map< string, EMObject >::const_iterator( parent_it )
01119 {
01120 }
01121 
01122 Dict::const_iterator::const_iterator( const Dict::iterator& it ) :
01123         map< string, EMObject >::const_iterator(it)
01124 {
01125 }
01126 
01127 Dict::const_iterator::const_iterator( const const_iterator& it ) :
01128         map< string, EMObject >::const_iterator(it)
01129 {
01130 }
01131 
01132 Dict::const_iterator& Dict::const_iterator::operator=( const const_iterator& that )
01133 {
01134         if( this != &that )
01135         {
01136                 map < string, EMObject >::const_iterator::operator=( that );
01137         }
01138         return *this;
01139 }
01140 
01141 EMObject Dict::get_ci(const string & key) const
01142 {
01143         string lower_key = Util::str_to_lower(key);
01144 
01145         for (map < string, EMObject >::const_iterator it = dict.begin(); it != dict.end(); ++it ) {
01146                 string lower = Util::str_to_lower(it->first);
01147                 if (lower == lower_key) return it->second;
01148         }
01149 
01150         throw NotExistingObjectException("EMObject", "Nonexisting key (" + key + ") in Dict");
01151 }
01152 
01153 bool Dict::has_key_ci(const string & key) const
01154 {
01155         string lower_key = Util::str_to_lower(key);
01156 
01157         for (map < string, EMObject >::const_iterator it = dict.begin(); it != dict.end(); ++it ) {
01158                 string lower = Util::str_to_lower(it->first);
01159                 if (lower == lower_key) return true;
01160         }
01161         return false;
01162 }

Generated on Tue Jul 12 13:45:47 2011 for EMAN2 by  doxygen 1.4.7