summaryrefslogtreecommitdiff
path: root/source/core/slang-std-writers.h
blob: 84447186a1b02404772a4a313b12a771c4738062 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef SLANG_CORE_STD_WRITERS_H
#define SLANG_CORE_STD_WRITERS_H

#include "slang-writer.h"
#include "../../slang-com-ptr.h"

namespace Slang
{

/* Holds standard writers for the channels */
class StdWriters: public RefObject
{
public:

    ISlangWriter* getWriter(SlangWriterChannel chan) const { return m_writers[chan]; }
    void setWriter(SlangWriterChannel chan, ISlangWriter* writer) { m_writers[chan] = writer; }

        /// Flush all the set writers
    void flushWriters();

        /// Ctor
    StdWriters() {}

        /// Initialize a default context
    static RefPtr<StdWriters> createDefault();
    static RefPtr<StdWriters> initDefaultSingleton();

    static StdWriters* getSingleton() { return s_singleton; }
    static void setSingleton(StdWriters* context) { s_singleton = context;  }

    static WriterHelper getError() { return getSingleton()->getWriter(SLANG_WRITER_CHANNEL_STD_ERROR); }
    static WriterHelper getOut() { return getSingleton()->getWriter(SLANG_WRITER_CHANNEL_STD_OUTPUT); }
    static WriterHelper getDiagnostic() { return getSingleton()->getWriter(SLANG_WRITER_CHANNEL_DIAGNOSTIC); }

protected:

    ComPtr<ISlangWriter> m_writers[SLANG_WRITER_CHANNEL_COUNT_OF]; 
    
    static StdWriters* s_singleton;
};

// --------------------------------------------------------------------------
inline void StdWriters::flushWriters()
{
    for (Index i = 0; i < Count(SLANG_WRITER_CHANNEL_COUNT_OF); ++i)
    {
        auto writer = m_writers[i];
        if (writer)
        {
            writer->flush();
        }
    }
}

}

#endif