1 /*  -*- c++ -*-  */
2 #ifndef SYSTEMUTILITIES_H
3 #define SYSTEMUTILITIES_H
4 
5 #include <string>
6 #include <algorithm>
7 
8 #include "Report.h"
9 #include "Real.h"
10 
11 namespace ProtoMol {
12 
13   /// Changes to the actual directory of the file name
14   bool changeDirectory(const std::string& fileName);
15 
16   /// Test if the file is accessible
17   bool isAccessible(const std::string& fileName);
18 
19   /// Does an abort, calling the adequate abort system function
20   void protomolAbort();
21 
22   /// Sets function to be called when calling protomolAbort()
23   void setProtomolAbort(void (*abortFunction)());
24 
25   /// Does an exit, calling the adequate exit system function
26   void protomolExit();
27 
28   /// Sets function to be called when calling protomolxit()
29   void setProtomolExit(void (*exitFunction)());
30 
31   /// Initiates the serialization block, e.g., used by Report
32   void protomolStartSerial(bool exludeMaster);
33 
34   /// Sets function to be called to start serialization
35   void setProtomolStartSerial(void (*startSerialFunction)(bool));
36 
37   /// Finializes the serialization block
38   void protomolEndSerial(bool exludeMaster);
39 
40   /// Sets function to be called to end serialization
41   void setProtomolEndSerial(void (*endSerialFunction)(bool));
42 
43   /// Returns the username or user id
44   std::string getUserName();
45 
46   /// Swap function to change endianess
47   template <typename T> inline
swapBytes(T & t)48   void swapBytes(T& t){
49     if (sizeof(T) % 2 != 0)
50       Report::report << Report::error << "Cannot swap types of uneven size."<<Report::endr;
51     char* res = reinterpret_cast<char*>(&t);
52     std::reverse(res, res + sizeof(T));
53   }
54 
55   /// Shift left of four Real's
shift(Real & a,Real & b,Real & c,const Real d)56   inline void shift(Real &a, Real &b, Real &c, const Real d) {
57     a=b;
58     b=c;
59     c=d;
60   }
61 
62   /// bool constant if the machine is littleEndian or not
63   extern const bool ISLITTLEENDIAN;
64 
65   /// Clears a container explicitly
66   template <typename T> inline
realclear(T & t)67   void realclear(T& t){
68     T tmp;
69     t.swap(tmp);
70   }
71 
72   /// Shrinks the capacity of a container explicitly
73   template <typename T> inline
shrink(T & t)74   void shrink(T& t){
75     T tmp(t);
76     t.swap(tmp);
77   }
78 
79 }
80 #endif /* SYSTEMUTILITIES_H */
81