file_cast.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2009 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8// psrchive/Util/units/file_cast.h
9
10#ifndef __UTILS_UNITS_FILE_CAST_H
11#define __UTILS_UNITS_FILE_CAST_H
12
13#include "FilePtr.h"
14#include <memory>
15
16template<typename T>
17T* hard_const_cast (const T* x)
18{
19 return const_cast<T*> (x);
20}
21
22template<typename To, typename From>
23To* file_cast (const From* from)
24{
25 FilePtr temp = tmpfile();
26 if (!temp)
27 throw Error (FailedSys, "file_cast", "tmpfile");
28
29 from->unload (temp);
30 rewind (temp);
31
32 std::unique_ptr<To> to (new To);
33
34 hard_const_cast(to.get())->load (temp);
35
36 return to.release();
37}
38
39template<typename To, typename From>
40To* dynamic_file_cast (From* from)
41{
42 To* to = dynamic_cast<To*>( from );
43 if (to)
44 return to;
45
46 return file_cast<To>(from);
47}
48
49#endif
A convenient exception handling class.
Definition Error.h:54
Closes a FILE* when it goes out of scope.
Definition FilePtr.h:19

Generated using doxygen 1.14.0