MADNESS  version 0.9
vecar.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33 */
34 
35 
36 #ifndef MADNESS_WORLD_VECAR_H__INCLUDED
37 #define MADNESS_WORLD_VECAR_H__INCLUDED
38 
41 
42 #include <vector>
43 #include <cstring>
44 #include <madness/world/archive.h>
45 
46 
47 namespace madness {
48  namespace archive {
49 
50 // With a bit of thought this could be generalized to several STL containers
51 
52 
55  mutable std::vector<unsigned char>* v;
56  public:
57  VectorOutputArchive(std::vector<unsigned char>& v, std::size_t hint=262144) : v(&v) {
58  open(hint);
59  };
60 
61  template <class T>
62  inline
64  store(const T* t, long n) const {
65  const unsigned char* ptr = (unsigned char*) t;
66  v->insert(v->end(),ptr,ptr+n*sizeof(T));
67  }
68 
69  void open(std::size_t hint=262144) {
70  v->clear();
71  v->reserve(hint);
72  };
73 
74  void close() {};
75 
76  void flush() {};
77  };
78 
79 
82  mutable std::vector<unsigned char>* v;
83  mutable std::size_t i;
84  public:
85  VectorInputArchive(std::vector<unsigned char>& v) : v(&v) , i(0) {}
86 
87  template <class T>
88  inline
90  load(T* t, long n) const {
91  std::size_t m = n*sizeof(T);
92  if (m+i > v->size()) MADNESS_EXCEPTION("VectorInputArchive: reading past end", m+1);
93  memcpy((unsigned char*) t, &((*v)[i]), m);
94  i += m;
95  }
96 
97  void open() {};
98 
99  void rewind() const {
100  i=0;
101  };
102 
103  std::size_t nbyte_avail() const {
104  return v->size()-i;
105  };
106 
107  void close() {}
108  };
109  }
110 }
111 #endif // MADNESS_WORLD_VECAR_H__INCLUDED
void close()
Definition: vecar.h:107
Interface templates for the archives (serialization)
void open()
Definition: vecar.h:97
VectorInputArchive(std::vector< unsigned char > &v)
Definition: vecar.h:85
std::size_t nbyte_avail() const
Definition: vecar.h:103
Wraps an archive around an STL vector for output.
Definition: vecar.h:54
Wraps an archive around an STL vector for input.
Definition: vecar.h:81
void open(std::size_t hint=262144)
Definition: vecar.h:69
madness::enable_if< madness::is_serializable< T >, void >::type store(const T *t, long n) const
Definition: vecar.h:64
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
Base class for output archives classes.
Definition: archive.h:583
void rewind() const
Definition: vecar.h:99
Base class for input archives classes.
Definition: archive.h:576
void flush()
Definition: vecar.h:76
void close()
Definition: vecar.h:74
const double m
Definition: gfit.cc:199
madness::enable_if< madness::is_serializable< T >, void >::type load(T *t, long n) const
Definition: vecar.h:90
VectorOutputArchive(std::vector< unsigned char > &v, std::size_t hint=262144)
Definition: vecar.h:57
#define MADNESS_EXCEPTION(msg, value)
Definition: worldexc.h:88
enable_if from Boost for conditionally instantiating templates based on type
Definition: enable_if.h:60
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45