GURLS++  2.0.00
C++ Implementation of GURLS Matlab Toolbox
optarray.cpp
00001 #include "gurls++/optarray.h"
00002 #include "gurls++/serialization.h"
00003 
00004 #include <fstream>
00005 
00006 using namespace std;
00007 
00008 namespace gurls{
00009 
00010 OptArray::OptArray():GurlsOption(OptArrayOption)
00011 {
00012     value = new ValueType();
00013 }
00014 
00015 OptArray::OptArray(const OptArray& other):GurlsOption(OptArrayOption)
00016 {
00017     value = new ValueType(*(other.value));
00018 }
00019 
00020 OptArray::~OptArray()
00021 {
00022     clear();
00023 
00024     delete value;
00025 }
00026 
00027 const OptArray::ValueType& OptArray::getValue() const
00028 {
00029     return *value;
00030 }
00031 
00032 void OptArray::push_back(GurlsOption* opt)
00033 {
00034     value->push_back(opt);
00035 }
00036 
00037 void OptArray::erase(unsigned long index, bool deleteMembers)
00038 {
00039     GurlsOption* opt = (*this)[index];
00040 
00041     value->erase(value->begin()+index);
00042 
00043     if(deleteMembers)
00044         delete opt;
00045 }
00046 
00047 void OptArray::clear()
00048 {
00049     for (ValueType::iterator it = value->begin(); it != value->end(); ++it)
00050         delete *it;
00051 
00052     value->clear();
00053 }
00054 
00055 void OptArray::reserve(unsigned long size)
00056 {
00057     value->reserve(size);
00058 }
00059 
00060 bool OptArray::isA(OptTypes id) const
00061 {
00062     return (id == OptArrayOption);
00063 }
00064 
00065 unsigned long OptArray::size() const
00066 {
00067     return value->size();
00068 }
00069 
00070 OptArray* OptArray::dynacast(GurlsOption* opt)
00071 {
00072     if (opt->isA(OptArrayOption))
00073         return static_cast<OptArray*>(opt);
00074 
00075     throw gException(gurls::Exception_Illegal_Dynamic_Cast);
00076 }
00077 
00078 const OptArray* OptArray::dynacast(const GurlsOption* opt)
00079 {
00080     if (opt->isA(OptArrayOption))
00081         return static_cast<const OptArray*>(opt);
00082 
00083     throw gException(gurls::Exception_Illegal_Dynamic_Cast);
00084 }
00085 
00086 GurlsOption* OptArray::operator[] (unsigned long i) const
00087 {
00088     if ( i >= value->size() )
00089         throw gException(gurls::Exception_Index_Out_of_Bound);
00090 
00091     return (*value)[i];
00092 }
00093 
00097 GURLS_EXPORT std::ostream& operator<<(std::ostream& os, const OptArray& opt)
00098 {
00099     os << endl << "~~~~~~~ OptArray: " << endl;
00100 
00101     const unsigned long size = opt.size();
00102 
00103     for(unsigned long i=0; i< size; ++i)
00104         os << "[ " << i << " ] = " << (*(opt[i])) << endl;
00105 
00106     os << "~~~~~~~";
00107 
00108     return os;
00109 }
00110 
00111 std::ostream& OptArray::operator<<(std::ostream& os) const
00112 {
00113     return os << *this;
00114 }
00115 
00116 void OptArray::save(const std::string& fileName) const
00117 {
00118 #ifndef USE_BINARY_ARCHIVES
00119     std::ofstream outstream(fileName.c_str());
00120 #else
00121     std::ofstream outstream(fileName.c_str(), ios_base::binary);
00122 #endif
00123 
00124     if(!outstream.is_open())
00125         throw gException("Could not open file " + fileName);
00126 
00127     oarchive outar(outstream);
00128     outar << *this;
00129 
00130     outstream.close();
00131 }
00132 
00133 
00134 void OptArray::load(const std::string& fileName)
00135 {
00136 #ifndef USE_BINARY_ARCHIVES
00137     std::ifstream instream(fileName.c_str());
00138 #else
00139     std::ifstream instream(fileName.c_str(), ios_base::binary);
00140 #endif
00141 
00142     if(!instream.is_open())
00143         throw gException("Could not open file " + fileName);
00144 
00145     try
00146     {
00147         iarchive inar(instream);
00148         inar >> *this;
00149     }
00150     catch(boost::archive::archive_exception&)
00151     {
00152         instream.close();
00153         throw gException("Invalid file format for " + fileName);
00154     }
00155 
00156     instream.close();
00157 }
00158 
00159 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends