File: sharedwriter.hpp

package info (click to toggle)
mothur 1.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,684 kB
  • sloc: cpp: 161,854; makefile: 122; sh: 31
file content (40 lines) | stat: -rwxr-xr-x 1,256 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
//  SharedWriter.hpp
//  Mothur
//
//  Created by Sarah Westcott on 12/7/17.
//  Copyright © 2017 Schloss Lab. All rights reserved.
//

#ifndef SharedWriter_hpp
#define SharedWriter_hpp

#include "mothurout.h"
#include "utils.hpp"

/***********************************************************************/
class SynchronizedOutputFile {
public:
    SynchronizedOutputFile (const string& p)                : path(p) { util.openOutputFile(p, out);        }
    SynchronizedOutputFile (const string& p, bool append)   : path(p) { util.openOutputFileAppend(p, out);  }
    ~SynchronizedOutputFile() { if (out.is_open()) { out.close(); } } //if we forgot to close()
    
    void write (const string& dataToWrite) {
        std::lock_guard<std::mutex> lock((writerMutex)); // Ensure that only one thread can execute at a time
        out << dataToWrite;
    }
    void close() { if (out.is_open()) { out.close(); } }
    
    void setFixedShowPoint()    {  out.setf(ios::fixed, ios::showpoint);    }
    void setPrecision(int p)    {  out << setprecision(p);                  }
    
private:
    string path;
    std::mutex writerMutex;
    Utils util;
    ofstream out;
};

/***********************************************************************/

#endif