Generated on Sun Feb 17 15:24:24 2019 for Gecode by doxygen 1.6.3

archive.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Guido Tack, 2011
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 namespace Gecode {
00035 
00042   class Archive {
00043   private:
00045     int _size;
00047     int _n;
00049     unsigned int* _a;
00051     int _pos;
00053     GECODE_KERNEL_EXPORT void resize(int n);
00054   public:
00056     Archive(void);
00058     GECODE_KERNEL_EXPORT ~Archive(void);
00060     GECODE_KERNEL_EXPORT Archive(const Archive& e);
00062     GECODE_KERNEL_EXPORT Archive& operator =(const Archive& e);
00064     void put(unsigned int i);
00066     int size(void) const;
00068     unsigned int operator [](int i) const;
00070     unsigned int get(void);
00071   };
00072 
00076   Archive&
00077   operator <<(Archive& e, unsigned int i);
00081   Archive&
00082   operator <<(Archive& e, int i);
00086   Archive&
00087   operator <<(Archive& e, unsigned short i);
00091   Archive&
00092   operator <<(Archive& e, short i);
00096   Archive&
00097   operator <<(Archive& e, unsigned char i);
00101   Archive&
00102   operator <<(Archive& e, char i);
00106   Archive&
00107   operator <<(Archive& e, bool i);
00111   Archive&
00112   operator <<(Archive& e, float d);
00116   Archive&
00117   operator <<(Archive& e, double d);
00118 
00122   Archive&
00123   operator >>(Archive& e, unsigned int& i);
00127   Archive&
00128   operator >>(Archive& e, int& i);
00132   Archive&
00133   operator >>(Archive& e, unsigned short& i);
00137   Archive&
00138   operator >>(Archive& e, short& i);
00142   Archive&
00143   operator >>(Archive& e, unsigned char& i);
00147   Archive&
00148   operator >>(Archive& e, char& i);
00152   Archive&
00153   operator >>(Archive& e, bool& i);
00157   Archive&
00158   operator >>(Archive& e, float& d);
00162   Archive&
00163   operator >>(Archive& e, double& d);
00164 
00165   /*
00166    * Implementation
00167    *
00168    */
00169 
00170   forceinline
00171   Archive::Archive(void) : _size(0), _n(0), _a(NULL), _pos(0) {}
00172 
00173   forceinline void
00174   Archive::put(unsigned int i) {
00175     if (_n==_size)
00176       resize(_n+1);
00177     _a[_n++] = i;
00178   }
00179 
00180   forceinline int
00181   Archive::size(void) const { return _n; }
00182 
00183   forceinline unsigned int
00184   Archive::operator [](int i) const {
00185     assert(i < _n);
00186     return _a[i];
00187   }
00188 
00189   forceinline unsigned int
00190   Archive::get(void) {
00191     assert(_pos < _n);
00192     return _a[_pos++];
00193   }
00194 
00195   forceinline Archive&
00196   operator <<(Archive& e, unsigned int i) {
00197     e.put(i);
00198     return e;
00199   }
00200   forceinline Archive&
00201   operator <<(Archive& e, int i) {
00202     e.put(static_cast<unsigned int>(i));
00203     return e;
00204   }
00205   forceinline Archive&
00206   operator <<(Archive& e, unsigned short i) {
00207     e.put(i);
00208     return e;
00209   }
00210   forceinline Archive&
00211   operator <<(Archive& e, short i) {
00212     e.put(static_cast<unsigned int>(i));
00213     return e;
00214   }
00215   forceinline Archive&
00216   operator <<(Archive& e, unsigned char i) {
00217     e.put(i);
00218     return e;
00219   }
00220   forceinline Archive&
00221   operator <<(Archive& e, char i) {
00222     e.put(static_cast<unsigned int>(i));
00223     return e;
00224   }
00225   forceinline Archive&
00226   operator <<(Archive& e, bool i) {
00227     e.put(static_cast<unsigned int>(i));
00228     return e;
00229   }
00230   forceinline Archive&
00231   operator <<(Archive& e, float d) {
00232     for (size_t i=0; i<sizeof(float); i++)
00233       e.put(static_cast<unsigned int>(reinterpret_cast<char*>(&d)[i]));
00234     return e;
00235   }
00236   forceinline Archive&
00237   operator <<(Archive& e, double d) {
00238     for (size_t i=0; i<sizeof(double); i++)
00239       e.put(static_cast<unsigned int>(reinterpret_cast<char*>(&d)[i]));
00240     return e;
00241   }
00242 
00243   forceinline Archive&
00244   operator >>(Archive& e, unsigned int& i) {
00245     i = e.get();
00246     return e;
00247   }
00248   forceinline Archive&
00249   operator >>(Archive& e, int& i) {
00250     i = static_cast<int>(e.get());
00251     return e;
00252   }
00253   forceinline Archive&
00254   operator >>(Archive& e, unsigned short& i) {
00255     i = static_cast<unsigned short>(e.get());
00256     return e;
00257   }
00258   forceinline Archive&
00259   operator >>(Archive& e, short& i) {
00260     i = static_cast<short>(e.get());
00261     return e;
00262   }
00263   forceinline Archive&
00264   operator >>(Archive& e, unsigned char& i) {
00265     i = static_cast<unsigned char>(e.get());
00266     return e;
00267   }
00268   forceinline Archive&
00269   operator >>(Archive& e, char& i) {
00270     i = static_cast<char>(e.get());
00271     return e;
00272   }
00273   forceinline Archive&
00274   operator >>(Archive& e, bool& i) {
00275     i = (e.get() != 0);
00276     return e;
00277   }
00278   forceinline Archive&
00279   operator >>(Archive& e, float& d) {
00280     char* cd = reinterpret_cast<char*>(&d);
00281     for (size_t i=0; i<sizeof(float); i++)
00282       cd[i] = static_cast<char>(e.get());
00283     return e;
00284   }
00285   forceinline Archive&
00286   operator >>(Archive& e, double& d) {
00287     char* cd = reinterpret_cast<char*>(&d);
00288     for (size_t i=0; i<sizeof(double); i++)
00289       cd[i] = static_cast<char>(e.get());
00290     return e;
00291   }
00292 
00293 }
00294 
00295 // STATISTICS: kernel-branch