netgen/libsrc/general/archive_base.hpp

145 lines
3.5 KiB
C++
Raw Normal View History

2014-02-24 01:31:40 +06:00
#ifndef NGS_ARCHIVE_BASE
#define NGS_ARCHIVE_BASE
2015-03-23 18:49:31 +05:00
// copied from netgen
2018-04-28 01:34:25 +05:00
#include <vector>
#include <map>
2014-02-24 01:31:40 +06:00
namespace ngstd
{
class Archive
{
2015-07-23 17:10:50 +05:00
bool is_output;
2014-02-24 01:31:40 +06:00
public:
2015-07-23 17:10:50 +05:00
Archive (bool ais_output) : is_output(ais_output) { ; }
2018-03-15 23:00:50 +05:00
virtual ~Archive() { ; }
2015-07-23 17:10:50 +05:00
bool Output () { return is_output; }
bool Input () { return !is_output; }
2014-02-24 01:31:40 +06:00
virtual Archive & operator & (double & d) = 0;
virtual Archive & operator & (int & i) = 0;
2015-01-08 17:47:27 +05:00
virtual Archive & operator & (long & i) = 0;
virtual Archive & operator & (size_t & i) = 0;
2014-02-24 01:31:40 +06:00
virtual Archive & operator & (short & i) = 0;
virtual Archive & operator & (unsigned char & i) = 0;
virtual Archive & operator & (bool & b) = 0;
virtual Archive & operator & (string & str) = 0;
virtual Archive & operator & (char *& str) = 0;
2015-03-23 18:49:31 +05:00
template <typename T>
Archive & Do (T * data, size_t n)
{ for (size_t j = 0; j < n; j++) { (*this) & data[j]; }; return *this; };
virtual Archive & Do (double * d, size_t n)
{ for (size_t j = 0; j < n; j++) { (*this) & d[j]; }; return *this; };
virtual Archive & Do (int * i, size_t n)
{ for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; };
virtual Archive & Do (long * i, size_t n)
{ for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; };
virtual Archive & Do (size_t * i, size_t n)
{ for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; };
virtual Archive & Do (short * i, size_t n)
{ for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; };
virtual Archive & Do (unsigned char * i, size_t n)
{ for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; };
virtual Archive & Do (bool * b, size_t n)
{ for (size_t j = 0; j < n; j++) { (*this) & b[j]; }; return *this; };
// nvirtual Archive & Do (string * str, size_t n)
// { for (size_t j = 0; j < n; j++) { (*this) & str[j]; }; return *this; };
// virtual Archive & operator & (char *& str) = 0;
2018-04-28 01:34:25 +05:00
// archive a pointer ...
int cnt = 0;
std::map<void*,int> ptr2nr;
std::vector<void*> nr2ptr;
2018-04-28 14:53:24 +05:00
2018-04-28 15:07:57 +05:00
/*
2018-04-28 14:53:24 +05:00
// necessary for msvc ???
Archive & operator& (string* & ps)
{
return operator&<string> (ps);
}
2018-04-28 15:07:57 +05:00
*/
2018-04-28 01:34:25 +05:00
template <typename T>
Archive & operator& (T *& p)
{
if (Output())
{
if (!p)
{
int m2 = -2;
(*this) & m2;
return *this;
}
auto pos = ptr2nr.find( (void*) p);
if (pos == ptr2nr.end())
{
ptr2nr[p] = cnt;
int m1 = -1;
(*this) & m1;
cnt++;
(*this) & (*p);
}
else
{
(*this) & pos->second;
}
}
else
{
int nr;
(*this) & nr;
// cout << "in, got nr " << nr << endl;
if (nr == -2)
{
p = nullptr;
}
else if (nr == -1)
{
p = new T;
// cout << "create new ptr, p = " << p << endl;
(*this) & *p;
nr2ptr.push_back(p);
}
else
{
p = (T*)nr2ptr[nr];
// cout << "reuse ptr " << nr << ": " << p << endl;
}
}
return *this;
}
2014-02-24 01:31:40 +06:00
template <typename T>
Archive & operator << (const T & t)
{
T ht(t);
(*this) & ht;
return *this;
}
};
}
#endif