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 // Static init
00074 map< EMObject::ObjectType, string>  EMObject::type_registry;
00075 
00076 //-------------------------------EMObjectTypes-----------------------------------------
00077 void EMObject::init()
00078 {
00079         static bool first_construction = true;
00080         if ( first_construction )
00081         {
00082                 // Initialize the the type registry once and for all
00083                 type_registry[BOOL] = "BOOL";
00084                 type_registry[INT] = "INT";
00085                 type_registry[UNSIGNEDINT] = "UNSIGNEDINT";
00086                 type_registry[FLOAT] = "FLOAT";
00087                 type_registry[DOUBLE] = "DOUBLE";
00088                 type_registry[STRING] = "STRING";
00089                 type_registry[EMDATA] = "EMDATA";
00090                 type_registry[XYDATA] = "XYDATA";
00091                 type_registry[INTARRAY] = "INTARRAY";
00092                 type_registry[FLOATARRAY] = "FLOATARRAY";
00093                 type_registry[STRINGARRAY] = "STRINGARRAY";
00094                 type_registry[TRANSFORM] = "TRANFORM";
00095                 type_registry[CTF] = "CTF";
00096                 type_registry[FLOAT_POINTER] = "FLOAT_POINTER";
00097                 type_registry[INT_POINTER] = "INT_POINTER";
00098                 type_registry[UNKNOWN] = "UNKNOWN";
00099                 type_registry[VOID_POINTER] = "VOID_POINTER";
00100                 first_construction = false;
00101         }
00102 }
00103 
00104 //-------------------------------EMObject--------------------------------------------
00105 
00106 void EMObject::printInfo() const
00107 {
00108         cout << "The address of my type is " << &type << endl;
00109         cout << " Now printing the enumerated values in type_registry " << endl;
00110         for( map< ObjectType, string>::const_iterator it = type_registry.begin(); it != type_registry.end(); ++it )
00111         {
00112                 cout << it->first << " " << it->second << endl;
00113         }
00114         cout << "My type is " << to_str(type) << " and its enumerated value is " << type << endl;
00115         cout << "The address of the static type registry is " << &type_registry <<", it should be same for all EMObjects" << endl;
00116 }
00117 
00118 EMObject::~EMObject() {}
00119 
00120 EMObject::EMObject() :
00121         d(0), type(UNKNOWN)
00122 {
00123         init();
00124 }
00125 
00126 EMObject::EMObject(bool boolean) :
00127         b(boolean), type(BOOL)
00128 {
00129         init();
00130 }
00131 
00132 EMObject::EMObject(int num) :
00133         n(num), type(INT)
00134 {
00135         init();
00136 }
00137 
00138 EMObject::EMObject(unsigned int num) :
00139         ui(num), type(UNSIGNEDINT)
00140 {
00141         init();
00142 }
00143 
00144 EMObject::EMObject(float ff) :
00145         type(FLOAT)
00146 {
00147         if(Util::goodf(&ff)) {
00148                 f = ff;
00149         }
00150         else{
00151                 f = 0.0f;
00152         }
00153         init();
00154 }
00155 
00156 EMObject::EMObject(double dd) :
00157         type(DOUBLE)
00158 {
00159         if(Util::goodf(&dd)) {
00160                 d = dd;
00161         }
00162         else{
00163                 d = 0.0;
00164         }
00165         init();
00166 }
00167 
00168 EMObject::EMObject(const char *s) :
00169         str(s), type(STRING)
00170 {
00171         init();
00172 }
00173 
00174 EMObject::EMObject(const string & s) :
00175         str(s), type(STRING)
00176 {
00177         init();
00178 }
00179 
00180 EMObject::EMObject(float *f) :
00181         fp(f), type(FLOAT_POINTER)
00182 {
00183         init();
00184 }
00185 
00186 EMObject::EMObject(int *i) :
00187         ip(i), type(INT_POINTER)
00188 {
00189         init();
00190 }
00191 
00192 EMObject::EMObject(void *v) :
00193         vp(v), type(VOID_POINTER)
00194 {
00195         init();
00196 }
00197 
00198 EMObject::EMObject(EMData * em) :
00199         emdata(em), type(EMDATA)
00200 {
00201         init();
00202 }
00203 
00204 EMObject::EMObject(XYData * xy) :
00205         xydata(xy), type(XYDATA)
00206 {
00207         init();
00208 }
00209 EMObject::EMObject(Transform* t) :
00210         farray(t->get_matrix()), type(TRANSFORM)
00211 {
00212         init();
00213 }
00214 
00215 EMObject::EMObject(Ctf * ctf) :
00216         str(ctf->to_string()), type(CTF)
00217 {
00218         init();
00219 }
00220 
00221 EMObject::EMObject(const vector< int >& v ) :
00222         iarray(v), type(INTARRAY)
00223 {
00224         init();
00225 }
00226 
00227 EMObject::EMObject(const vector < float >&v) :
00228         farray(v), type(FLOATARRAY)
00229 {
00230         init();
00231 }
00232 
00233 EMObject:: EMObject(const vector <string>& sarray) :
00234         strarray(sarray), type(STRINGARRAY)
00235 {
00236         init();
00237 }
00238 
00239 EMObject::operator bool () const
00240 {
00241         if (type == BOOL) {
00242                 return b;
00243         }
00244         else if (type == INT) {
00245                 return n != 0;
00246         }
00247         else if (type == UNSIGNEDINT) {
00248                 return ui != 0;
00249         }
00250         else if (type == FLOAT) {
00251                 return f != 0;
00252         }
00253         else if (type == DOUBLE) {
00254                 return d != 0;
00255         }
00256         else if (type == EMDATA) {
00257                 return emdata != 0;
00258         }
00259         else if (type == XYDATA) {
00260                 return xydata != 0;
00261         }
00262         else if (type == FLOAT_POINTER) {
00263                 return fp != 0;
00264         }
00265         else if (type == INT_POINTER) {
00266                 return ip != 0;
00267         }
00268         else if (type == VOID_POINTER) {
00269                 return vp != 0;
00270         }
00271 //      else if (type == TRANSFORM) {
00272 //              return transform != 0;
00273 //      }
00274         // It seemed unconventional to return a boolean for the stl objects
00275         else {
00276                 if (type != UNKNOWN) {
00277                         throw TypeException("Cannot convert to bool this data type ",
00278                                                                 get_object_type_name(type));
00279                 }
00280         }
00281         return 0;
00282 }
00283 
00284 EMObject::operator int () const
00285 {
00286         if (type == INT) {
00287                 return n;
00288         }
00289         else if (type == UNSIGNEDINT) {
00290                 return (int) ui;
00291         }
00292         else if (type == FLOAT) {
00293                 return (int) f;
00294         }
00295         else if (type == DOUBLE) {
00296                 return (int) d;
00297         }
00298         else if (type == BOOL) {
00299                 return b?1:0;
00300         }
00301         else {
00302                 if (type != UNKNOWN) {
00303                         throw TypeException("Cannot convert to int this data type ",
00304                                                                 get_object_type_name(type));
00305                 }
00306         }
00307         return 0;
00308 }
00309 
00310 EMObject::operator unsigned int () const
00311 {
00312         if (type == UNSIGNEDINT) {
00313                 return (unsigned int) ui;
00314         }
00315         else {
00316                 if (type != UNKNOWN) {
00317                         throw TypeException("Cannot convert to int this data type ",
00318                                                                 get_object_type_name(type));
00319                 }
00320         }
00321         return 0;
00322 }
00323 
00324 EMObject::operator float () const
00325 {
00326         if (type == BOOL) {
00327                 return b?1.0f:0.0f;
00328         }
00329         else if (type == FLOAT) {
00330                 return f;
00331         }
00332         else if (type == INT) {
00333                 return (float) n;
00334         }
00335         else if (type == UNSIGNEDINT) {
00336                 return (float) ui;
00337         }
00338         else if (type == DOUBLE) {
00339                 return (float) d;
00340         }
00341         else {
00342                 if (type != UNKNOWN) {
00343                         throw TypeException("Cannot convert to float from this data type",
00344                                                                 get_object_type_name(type));
00345                 }
00346         }
00347 
00348         return 0;
00349 }
00350 
00351 EMObject::operator double () const
00352 {
00353         if (type == BOOL) {
00354         return b?1.0:0.0;
00355         }
00356         else if (type == DOUBLE) {
00357                 return d;
00358         }
00359         else if (type == INT) {
00360                 return (double) n;
00361         }
00362         else if (type == UNSIGNEDINT) {
00363                 return (double) ui;
00364         }
00365         else if (type == FLOAT) {
00366                 return (double) f;
00367         }
00368         else {
00369                 if (type != UNKNOWN) {
00370                         throw TypeException("Cannot convert to double from this data type",
00371                                                                 get_object_type_name(type));
00372                 }
00373         }
00374         return 0;
00375 }
00376 
00377 EMObject::operator int * () const
00378 {
00379         if (type != INT_POINTER)
00380         {
00381                 if (type != UNKNOWN)
00382                         throw TypeException("Cannot convert to float pointer from this data type",
00383                                                                 get_object_type_name(type));
00384 
00385                 return 0;
00386         }
00387 
00388         return ip;
00389 }
00390 
00391 
00392 EMObject::operator float * () const
00393 {
00394         if (type != FLOAT_POINTER)
00395         {
00396                 if (type != UNKNOWN)
00397                         throw TypeException("Cannot convert to float pointer from this data type",
00398                                                                 get_object_type_name(type));
00399 
00400                 return 0;
00401         }
00402 
00403         return fp;
00404 }
00405 
00406 // MAYBE REMOVE? FIXME
00407 EMObject::operator void * () const
00408 {
00409         if (type == VOID_POINTER) return vp;
00410         else if (type == FLOAT_POINTER) return (void *)fp;
00411         else if (type == INT_POINTER) return (void *)ip;
00412         else if (type == EMDATA) return (void *) emdata;
00413         else if (type == XYDATA) return (void *) xydata;
00414 //      else if (type == TRANSFORM) return (void *) transform;
00415         else throw TypeException("Cannot convert to void pointer from this data type", get_object_type_name(type));
00416 }
00417 
00418 EMObject::operator const char * () const
00419 {
00420         if (type != STRING && type != CTF) {
00421                 stringstream ss;
00422                 string return_string;
00423                 if ( type == INT )
00424                 {
00425                         ss << n;
00426                         ss >> return_string;
00427                         return return_string.c_str();
00428                 }
00429                 if ( type == UNSIGNEDINT )
00430                 {
00431                         ss << ui;
00432                         ss >> return_string;
00433                         return return_string.c_str();
00434                 }
00435                 else
00436                 if ( type == FLOAT )
00437                 {
00438                         ss << f;
00439                         ss >> return_string;
00440                         return return_string.c_str();
00441                 }
00442                 else
00443                 if ( type == DOUBLE )
00444                 {
00445                         ss << d;
00446                         ss >> return_string;
00447                         return return_string.c_str();
00448                 }
00449                 else if (type != UNKNOWN) {
00450                         throw TypeException("Cannot convert to string from this data type",
00451                                                                 get_object_type_name(type));
00452                 }
00453 
00454                 return "";
00455         }
00456         return str.c_str();
00457 }
00458 
00459 EMObject::operator EMData * () const
00460 {
00461         if (type != EMDATA) {
00462                 if (type != UNKNOWN) {
00463                         throw TypeException("Cannot convert to EMData* from this data type",
00464                                    get_object_type_name(type));
00465                 }
00466                 return 0;
00467         }
00468         return emdata;
00469 }
00470 
00471 EMObject::operator XYData * () const
00472 {
00473         if (type != XYDATA) {
00474                 if (type != UNKNOWN) {
00475                         throw TypeException("Cannot convert to XYData* from this data type",
00476                                    get_object_type_name(type));
00477                 }
00478                 return 0;
00479         }
00480         return xydata;
00481 }
00482 
00483 EMObject::operator Transform* () const
00484 {
00485         if(type != TRANSFORM) {
00486                 if(type != UNKNOWN) {
00487                         throw TypeException("Cannot convert to TRANSFORM* from this data type",
00488                                                                 get_object_type_name(type));
00489                 }
00490         }
00491         Transform * transform = new Transform();
00492         transform->set_matrix(farray);
00493         return transform;
00494 }
00495 
00496 EMObject::operator Ctf* () const
00497 {
00498 /*      if(type != CTF) {
00499                 if(type != CTF) {
00500                         throw TypeException("Cannot convert to TRANSFORM* from this data type",
00501                                                                 get_object_type_name(type));
00502                 }
00503         }*/
00504         Ctf * ctf = 0;
00505         if(str[0] == 'O') {
00506                 ctf = new EMAN1Ctf();
00507                 ctf->from_string(str);
00508         }
00509         else if(str[0] == 'E') {
00510                 ctf = new EMAN2Ctf();
00511                 ctf->from_string(str);
00512         }
00513         return ctf;
00514 }
00515 
00516 EMObject::operator vector<int>() const
00517 {
00518     if( type != INTARRAY )
00519     {
00520         if( type != UNKNOWN ) {
00521                 throw TypeException("Cannot convert to vector<int> from this data type", get_object_type_name(type) );
00522                 }
00523                 return vector<int>();
00524     }
00525     return iarray;
00526 }
00527 
00528 EMObject::operator vector < float > () const
00529 {
00530         if (type != FLOATARRAY) {
00531                 if (type != UNKNOWN) {
00532                         throw TypeException("Cannot convert to vector<float> from this data type",
00533                                                                 get_object_type_name(type));
00534                 }
00535                 return vector < float >();
00536         }
00537         return farray;
00538 }
00539 
00540 EMObject::operator vector<string> () const
00541 {
00542         if (type != STRINGARRAY) {
00543                 if (type != UNKNOWN) {
00544                         throw TypeException("Cannot convert to vector<string> from this data type",
00545                                                                 get_object_type_name(type));
00546                 }
00547                 return vector<string>();
00548         }
00549         return strarray;
00550 }
00551 
00552 bool EMObject::is_null() const
00553 {
00554         return (type == UNKNOWN);
00555 }
00556 
00557 string EMObject::to_str() const
00558 {
00559         return to_str(type);
00560 }
00561 
00562 string EMObject::to_str(ObjectType argtype) const
00563 {
00564         if (argtype == STRING) {
00565                 return str;
00566         }
00567         else {
00568                 char tmp_str[32];
00569                 if (argtype == BOOL) {
00570                         if (b)
00571                                 sprintf(tmp_str, "true");
00572                         else
00573                                 sprintf(tmp_str, "false");
00574                 }
00575                 else if (argtype == INT) {
00576                         sprintf(tmp_str, "%d", n);
00577                 }
00578                 else if (argtype == UNSIGNEDINT) {
00579                         sprintf(tmp_str, "%d", ui);
00580                 }
00581                 else if (argtype == FLOAT) {
00582                         sprintf(tmp_str, "%f", f);
00583                 }
00584                 else if (argtype == DOUBLE) {
00585                         sprintf(tmp_str, "%f", d);
00586                 }
00587                 else if (argtype == EMDATA) {
00588                         sprintf(tmp_str, "EMDATA");
00589                 }
00590                 else if (argtype == FLOAT_POINTER) {
00591                         sprintf(tmp_str, "FLOAT_POINTER");
00592                 }
00593                 else if (argtype == INT) {
00594                         sprintf(tmp_str, "INT_POINTER");
00595                 }
00596                 else if (argtype == VOID_POINTER) {
00597                         sprintf(tmp_str, "VOID_POINTER");
00598                 }
00599                 else if (argtype == XYDATA) {
00600                         sprintf(tmp_str, "XYDATA");
00601                 }
00602                 else if (argtype == INTARRAY) {
00603                         sprintf(tmp_str, "INTARRAY");
00604                 }
00605                 else if (argtype == FLOATARRAY) {
00606                         sprintf(tmp_str, "FLOATARRAY");
00607                 }
00608                 else if (argtype == STRINGARRAY) {
00609                         sprintf(tmp_str, "STRINGARRAY");
00610                 }
00611                 else if (argtype == TRANSFORM) {
00612                         sprintf(tmp_str, "TRANSFORMD");
00613                 }
00614                 else if (argtype == CTF) {
00615                         sprintf(tmp_str, "CTF");
00616                 }
00617                 else if (argtype == UNKNOWN) {
00618                         sprintf(tmp_str, "UNKNOWN");
00619                 }
00620                 else {
00621                         LOGERR("No such EMObject defined");
00622                         throw NotExistingObjectException("EMObject", "unknown type");
00623                 }
00624                 return string(tmp_str);
00625         }
00626 }
00627 
00628 string EMObject::get_object_type_name(ObjectType t)
00629 {
00630 #ifdef _WIN32
00631         if (t == BOOL) {
00632                 return "BOOL";
00633         }else
00634         if ( t == INT){
00635                 return "INT";
00636         }else
00637         if ( t == UNSIGNEDINT){
00638                 return "UNSIGNEDINT";
00639         } else
00640         if ( t == FLOAT){
00641                 return "FLOAT";
00642         } else
00643         if ( t == DOUBLE){
00644                 return "DOUBLE";
00645         }else
00646         if ( t == STRING){
00647                 return "STRING";
00648         }else
00649         if ( t == EMDATA){
00650                 return "EMDATA";
00651         }
00652         else
00653         if ( t == XYDATA){
00654                 return "XYDATA";
00655         }else
00656         if ( t == INTARRAY){
00657                 return "INTARRAY";
00658         }else
00659         if ( t == FLOATARRAY){
00660                 return "FLOATARRAY";
00661         } else
00662         if ( t == STRINGARRAY){
00663                 return "STRINGARRAY";
00664         }else
00665         if ( t == TRANSFORM){
00666                 return "TRANSFORM";
00667         }else
00668         if ( t == CTF){
00669                 return "CTF";
00670         }else
00671         if ( t == FLOAT_POINTER){
00672                 return "FLOAT_POINTER";
00673         }else
00674         if ( t == INT_POINTER){
00675                 return "INT_POINTER";
00676         }else
00677         if ( t == UNKNOWN){
00678                 return "UNKNOWN";
00679         } else
00680         if ( t == VOID_POINTER){
00681                 return "VOID_POINTER";
00682         }
00683         else {
00684                 LOGERR("No such EMObject defined");
00685                 throw NotExistingObjectException("EMObject", "unknown type");
00686         }
00687 
00688 #else
00689 
00690         if  ( type_registry.find(t) != type_registry.end() )
00691                 return type_registry[t];
00692         else
00693                 LOGERR("No such EMObject defined");
00694                 throw NotExistingObjectException("EMObject", "unknown type");
00695 #endif  //_WIN32
00696 }
00697 
00698 bool EMAN::operator==(const EMObject &e1, const EMObject & e2)
00699 {
00700 
00701         if (e1.type != e2.type) {
00702                 return false;
00703         }
00704 
00705         switch (e1.type) {
00706         case  EMObject::BOOL:
00707                 return (e1.b == e2.b);
00708         break;
00709         case  EMObject::INT:
00710                 return (e1.n == e2.n);
00711         break;
00712         case  EMObject::UNSIGNEDINT:
00713                 return (e1.ui == e2.ui);
00714         break;
00715         case  EMObject::FLOAT:
00716                 return (e1.f == e2.f);
00717         break;
00718         case  EMObject::DOUBLE:
00719                 return (e1.d == e2.d);
00720         break;
00721         case EMObject::CTF:
00722         case  EMObject::STRING:
00723                 return (e1.str == e2.str);
00724         break;
00725         case  EMObject::FLOAT_POINTER:
00726                 return (e1.fp == e2.fp);
00727         break;
00728         case  EMObject::INT_POINTER:
00729                 return (e1.ip == e2.ip);
00730         break;
00731         case  EMObject::VOID_POINTER:
00732                 return (e1.vp == e2.vp);
00733         break;
00734         case  EMObject::EMDATA:
00735                 return (e1.emdata == e2.emdata);
00736         break;
00737         case  EMObject::XYDATA:
00738                 return (e1.xydata == e2.xydata);
00739         break;
00740         case  EMObject::TRANSFORM:
00741         case  EMObject::FLOATARRAY:
00742                 if (e1.farray.size() == e2.farray.size()) {
00743                         for (size_t i = 0; i < e1.farray.size(); i++) {
00744                                 if (e1.farray[i] != e2.farray[i]) {
00745                                         return false;
00746                                 }
00747                         }
00748                         return true;
00749                 }
00750                 else {
00751                         return false;
00752                 }
00753         break;
00754         case  EMObject::INTARRAY:
00755                 if (e1.iarray.size() == e2.iarray.size()) {
00756                         for (size_t i = 0; i < e1.iarray.size(); i++) {
00757                                 if (e1.iarray[i] != e2.iarray[i]) {
00758                                         return false;
00759                                 }
00760                         }
00761                         return true;
00762                 }
00763         break;
00764         case  EMObject::STRINGARRAY:
00765                 if (e1.strarray.size() == e2.strarray.size()) {
00766                         for (size_t i = 0; i < e1.strarray.size(); i++) {
00767                                 if (e1.strarray[i] != e2.strarray[i]) {
00768                                         return false;
00769                                 }
00770                         }
00771                         return true;
00772                 }
00773                 else {
00774                         return false;
00775                 }
00776         break;
00777         case  EMObject::UNKNOWN:
00778                 // UNKNOWN really means "no type" and if two objects both have
00779                 // type UNKNOWN they really are the same
00780                 return (e1.type == e2.type);
00781         break;
00782         default:
00783                 return false;
00784         break;
00785         }
00786         return false;
00787 }
00788 
00789 bool EMAN::operator!=(const EMObject &e1, const EMObject & e2)
00790 {
00791         return !(e1 == e2);
00792 }
00793 
00794 // Copy constructor
00795 EMObject::EMObject(const EMObject& that)
00796 {
00797         // init isn't necessary because that must have already called it!
00798         *this = that;
00799 }
00800 
00801 
00802 // Assignment operator -  - copies only the variable associated with the type of the argument.
00803 // It would be possible just to do a dumb copy of everything, but that seems opposed to
00804 // the concept of an EMObject, which is always of a single type.
00805 EMObject& EMObject::operator=( const EMObject& that )
00806 {
00807 
00808 // This test breaks assignment when either the current or assigned values are (float)nan
00809 // it's also somewhat inherently stupid, since testing for equivalence may cost as much
00810 // as assignment.
00811 //      if ( *this != that )
00812         {
00813                 // First store the type of the input, At first I forgot to do this and it was a very
00814                 // difficult bug to track down
00815                 type = that.type;
00816 
00817 //              if ( type != this_type ) throw
00818 
00819 
00820                 switch (type)
00821                 {
00822                 case BOOL:
00823                         b = that.b;
00824                 break;
00825                 case INT:
00826                         n = that.n;
00827                 break;
00828                 case UNSIGNEDINT:
00829                         ui = that.ui;
00830                 break;
00831                 case FLOAT:
00832                         f = that.f;
00833                 break;
00834                 case DOUBLE:
00835                         d = that.d;
00836                 break;
00837                 case CTF:
00838                 case STRING:
00839                         str = that.str;
00840                 break;
00841                 case FLOAT_POINTER:
00842                         // Warning - Pointer address copy.
00843                         fp = that.fp;
00844                 break;
00845                 case INT_POINTER:
00846                 // Warning - Pointer address copy.
00847                         ip = that.ip;
00848                 break;
00849                 case VOID_POINTER:
00850                         // Warning - Pointer address copy.
00851                         vp = that.vp;
00852                 break;
00853                 case EMDATA:
00854                         // Warning - Pointer address copy.
00855                         emdata = that.emdata;
00856                 break;
00857                 case XYDATA:
00858                         // Warning - Pointer address copy.
00859                         xydata = that.xydata;
00860                 break;
00861                 case TRANSFORM:
00862                 case FLOATARRAY:
00863                         farray = that.farray;
00864                 break;
00865                 case INTARRAY:
00866                         iarray = that.iarray;
00867                 break;
00868                 case STRINGARRAY:
00869                         strarray = that.strarray;
00870                 break;
00871                 case UNKNOWN:
00872                         // This is possible, nothing should happen
00873                         // The EMObject's default constructor has been called and
00874                         // as yet has no type - doing nothing is exactly as the
00875                         // the assignment operator should work.
00876                 break;
00877                 default:
00878                         LOGERR("No such EMObject defined");
00879                         throw NotExistingObjectException("EMObject", "unknown type");
00880                 break;
00881                 }
00882         }
00883 //      else
00884 //      {
00885 //              cerr << "Warning - attempt to assign EMObject onto itself. No action taken" << endl;
00886 //              cerr << "My type is " << get_object_type_name(type) << endl;
00887 //      }
00888 
00889         return *this;
00890 }
00891 
00892 //-------------------------------TypeDict--------------------------------------------
00893 
00894 void TypeDict::dump()
00895 {
00896         map < string, string >::iterator p;
00897         for (p = type_dict.begin(); p != type_dict.end(); p++) {
00898                 printf("\t%s    %s  %s\n",
00899                            p->first.c_str(), p->second.c_str(), desc_dict[p->first].c_str());
00900         }
00901 }
00902 
00903 //-------------------------------Dict--------------------------------------------
00904 
00905 Dict::Dict(const Dict& that)
00906 {
00907         *this = that;
00908 }
00909 
00910 Dict& Dict::operator=(const Dict& that)
00911 {
00912         if ( this != &that )
00913         {
00914                 dict.clear();
00915                 copy(that.begin(), that.end(), inserter(dict, dict.begin()));
00916                 // or use this
00917                 // dict.insert( that.begin(), that.end());
00918         }
00919         else
00920         {
00921                 cerr << "Warning - attempted to assign a Dict object to itself. No action taken" << endl;
00922         }
00923 
00924         return *this;
00925 }
00926 
00927 bool EMAN::operator==(const Dict& d1, const Dict& d2)
00928 {
00929         // Just make use of map's version of operator==
00930         return (d1.dict == d2.dict);
00931 }
00932 
00933 bool EMAN::operator!=(const Dict& d1, const Dict& d2)
00934 {
00935         return !(d1 == d2);
00936 }
00937 
00938 
00939 // Iterator support
00940 // This is just a wrapper, everything is inherited from the map<string,EMObject>::iterator
00941 // so the interface is the same as you would expect
00942 // iterator support added by d.woolford May 2007
00943 
00944 Dict::iterator Dict::begin( void )
00945 {
00946         return iterator( dict.begin() );
00947 }
00948 
00949 Dict::const_iterator Dict::begin( void ) const
00950 {
00951         return const_iterator( (map < string, EMObject >::const_iterator) dict.begin() );
00952 }
00953 
00954 // Wraps map.find(const string& key)
00955 Dict::iterator Dict::find( const string& key )
00956 {
00957         return iterator( dict.find(key) );
00958 }
00959 
00960 Dict::iterator Dict::end( void )
00961 {
00962         return iterator( dict.end() );
00963 }
00964 
00965 Dict::const_iterator Dict::end( void ) const
00966 {
00967         return const_iterator( (map < string, EMObject >::const_iterator)dict.end() );
00968 }
00969 
00970 Dict::const_iterator Dict::find( const string& key ) const
00971 {
00972         return const_iterator( (map < string, EMObject >::const_iterator)dict.find(key) );
00973 }
00974 
00975 //
00976 // iterator
00977 //
00978 Dict::iterator::iterator( map< string, EMObject >::iterator parent_it  ) :
00979         map< string, EMObject >::iterator( parent_it )
00980 {
00981 }
00982 
00983 
00984 Dict::iterator::iterator( const iterator& that ) :
00985         map < string, EMObject >::iterator( that )
00986 {
00987 }
00988 
00989 
00990 Dict::iterator& Dict::iterator::operator=( const iterator& that )
00991 {
00992         if( this != &that )
00993         {
00994                 map < string, EMObject >::iterator::operator=( that );
00995         }
00996         return *this;
00997 }
00998 
00999 //
01000 // const_iterator
01001 //
01002 
01003 Dict::const_iterator::const_iterator( const map < string, EMObject >::const_iterator parent_it  ) :
01004         map< string, EMObject >::const_iterator( parent_it )
01005 {
01006 }
01007 
01008 Dict::const_iterator::const_iterator( const Dict::iterator& it ) :
01009         map< string, EMObject >::const_iterator(it)
01010 {
01011 }
01012 
01013 Dict::const_iterator::const_iterator( const const_iterator& it ) :
01014         map< string, EMObject >::const_iterator(it)
01015 {
01016 }
01017 
01018 Dict::const_iterator& Dict::const_iterator::operator=( const const_iterator& that )
01019 {
01020         if( this != &that )
01021         {
01022                 map < string, EMObject >::const_iterator::operator=( that );
01023         }
01024         return *this;
01025 }
01026 
01027 EMObject Dict::get_ci(const string & key) const
01028 {
01029         string lower_key = Util::str_to_lower(key);
01030 
01031         for (map < string, EMObject >::const_iterator it = dict.begin(); it != dict.end(); ++it ) {
01032                 string lower = Util::str_to_lower(it->first);
01033                 if (lower == lower_key) return it->second;
01034         }
01035 
01036         throw NotExistingObjectException("EMObject", "Nonexisting key (" + key + ") in Dict");
01037 }
01038 
01039 bool Dict::has_key_ci(const string & key) const
01040 {
01041         string lower_key = Util::str_to_lower(key);
01042 
01043         for (map < string, EMObject >::const_iterator it = dict.begin(); it != dict.end(); ++it ) {
01044                 string lower = Util::str_to_lower(it->first);
01045                 if (lower == lower_key) return true;
01046         }
01047         return false;
01048 }

Generated on Sat Nov 21 02:19:15 2009 for EMAN2 by  doxygen 1.5.6