Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:18

0001 #ifndef GENERS_FORWARD_LISTIO_HH_
0002 #define GENERS_FORWARD_LISTIO_HH_
0003 
0004 #include "Alignment/Geners/interface/CPP11_config.hh"
0005 #ifdef CPP11_STD_AVAILABLE
0006 
0007 #include <forward_list>
0008 #include "Alignment/Geners/interface/GenericIO.hh"
0009 
0010 // std::forward_list does not have the size() method. Because of this,
0011 // we can not use the I/O machinery developed in "GenericIO.hh" for
0012 // standard containers. Instead, we will designate std::forward_list
0013 // as an external type and will handle it separately.
0014 //
0015 gs_declare_template_external_TT(std::forward_list) gs_specialize_template_id_TT(std::forward_list, 0, 1)
0016 
0017     namespace gs {
0018   // Assuming that we want to write the list once and potentially
0019   // read it back many times, we will write it out in the reverse
0020   // order. This is because it is easy to extend the list from the
0021   // front but not from the back.
0022   //
0023   template <class Stream, class State, class T>
0024   struct GenericWriter<Stream, State, std::forward_list<T>, Int2Type<IOTraits<int>::ISEXTERNAL> > {
0025     inline static bool process(const std::forward_list<T>& s, Stream& os, State* p2, const bool processClassId) {
0026       typedef typename std::forward_list<T>::const_iterator Iter;
0027 
0028       bool status = processClassId ? ClassId::makeId<std::forward_list<T> >().write(os) : true;
0029       if (status) {
0030         const Iter listend = s.end();
0031         std::size_t sz = 0;
0032         for (Iter it = s.begin(); it != listend; ++it, ++sz) {
0033           ;
0034         }
0035         write_pod(os, sz);
0036         if (sz) {
0037           status = ClassId::makeId<T>().write(os);
0038           std::vector<Iter> iters(sz);
0039           sz = 0;
0040           for (Iter it = s.begin(); it != listend; ++it, ++sz)
0041             iters[sz] = it;
0042           for (long long number = sz - 1; number >= 0 && status; --number)
0043             status = process_const_item<GenericWriter2>(*iters[number], os, p2, false);
0044         }
0045       }
0046       return status && !os.fail();
0047     }
0048   };
0049 
0050   template <class T>
0051   struct InsertContainerItem<std::forward_list<T> > {
0052     inline static void insert(std::forward_list<T>& obj, const T& item, const std::size_t /* itemNumber */) {
0053       obj.push_front(item);
0054     }
0055   };
0056 
0057   template <class Stream, class State, class T>
0058   struct GenericReader<Stream, State, std::forward_list<T>, Int2Type<IOTraits<int>::ISEXTERNAL> > {
0059     inline static bool readIntoPtr(std::forward_list<T>*& ptr, Stream& is, State* p2, const bool processClassId) {
0060       if (processClassId) {
0061         ClassId id(is, 1);
0062         const ClassId& curr = ClassId::makeId<std::forward_list<T> >();
0063         curr.ensureSameName(id);
0064       }
0065       std::unique_ptr<std::forward_list<T> > myptr;
0066       if (ptr == 0)
0067         myptr = std::unique_ptr<std::forward_list<T> >(new std::forward_list<T>());
0068       else
0069         ptr->clear();
0070       std::size_t sz = 0;
0071       read_pod(is, &sz);
0072       bool itemStatus = true;
0073       if (sz) {
0074         ClassId itemId(is, 1);
0075         p2->push_back(itemId);
0076         std::forward_list<T>* nzptr = ptr ? ptr : myptr.get();
0077         try {
0078           for (std::size_t i = 0; i < sz && itemStatus; ++i)
0079             itemStatus =
0080                 GenericReader<Stream, State, std::forward_list<T>, InContainerCycle>::process(*nzptr, is, p2, i);
0081         } catch (...) {
0082           p2->pop_back();
0083           throw;
0084         }
0085       }
0086       const bool success = itemStatus && !is.fail();
0087       if (success && ptr == 0)
0088         ptr = myptr.release();
0089       return success;
0090     }
0091 
0092     inline static bool process(std::forward_list<T>& s, Stream& is, State* st, const bool processClassId) {
0093       std::forward_list<T>* ps = &s;
0094       return readIntoPtr(ps, is, st, processClassId);
0095     }
0096   };
0097 }
0098 
0099 #endif  // CPP11_STD_AVAILABLE
0100 #endif  // GENERS_FORWARD_LISTIO_HH_