File: Message.hh

package info (click to toggle)
topcom 1.2.0~beta%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 148,596 kB
  • sloc: cpp: 40,956; sh: 4,663; makefile: 679; ansic: 55
file content (146 lines) | stat: -rw-r--r-- 4,320 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
////////////////////////////////////////////////////////////////////////////////
// 
// Message.hh 
//
//    produced: 2024-05-04 jr
//
////////////////////////////////////////////////////////////////////////////////
#ifndef MESSAGE_HH
#define MESSAGE_HH

#include <iostream>
#include <mutex>

#include "ContainerIO.hh"
#include "config.h"

namespace topcom {

  class IO_sync {
  public:
    static std::mutex mutex;
  };
  
  class Message {
  private:
    std::ostream*                       _ostptr;
  public:
    inline Message() : _ostptr(nullptr) {}
    inline Message(std::ostream& ost, bool activate = true) : _ostptr(activate ? &ost : nullptr) {}
    inline Message(std::ostream* ostptr, bool activate = true) : _ostptr(activate ? ostptr : nullptr) {}
    inline Message& activate(std::ostream& ost) { _ostptr = &ost; return *this; }
    inline Message& deactivate() { _ostptr = nullptr; return *this; }
    inline bool is_active() const { return (_ostptr != nullptr); }

    inline void lock() { IO_sync::mutex.lock(); }
    inline void unlock() { IO_sync::mutex.unlock(); }

    // short-cut functions for constant strings:
    inline Message& print_topcomheader() {
      *this << std::endl;
      *this << "==================================================================\n";
      *this << "--------------------- " << PACKAGE << " version " << VERSION << " -----------------------\n";
      *this << "   Triangulations of Point Configurations and Oriented Matroids   \n";
      *this << "------------------------ by Joerg Rambau -------------------------\n";
      *this << "==================================================================\n";
      *this << std::endl;
#ifdef DEBUG
#ifdef TOPCOM_CONTAINERS
      *this << "       -- using STL containers for hash tables --" << std::endl;
#endif
#ifdef TOPCOM_SYMMETRIES
      *this << "       -- using TOPCOM containers for symmetries  --" << std::endl;
#endif
#ifdef TOPCOM_FLIPS
      *this << "       --    using TOPCOM containers for flips    --" << std::endl;
#endif
#ifdef TOPCOM_CHIROTOPE
      *this << "       -- using TOPCOM containers for chirotopes  --" << std::endl;
#endif
      *this << std::endl;
#endif
      return *this;
    }
    
    inline Message& print_hseparator() {
      *this << "---------------------------------"
	    << "---------------------------------"
	    << "---------------------------------"
	    << "---------------------------------"
	    << "---------------------------------"
	    << "-------------------" << std::endl;
      return *this;
    }

    inline Message& print_dumpseparator() {
      *this << "#################################"
	    << "#################################"
	    << "#################################"
	    << "#################################"
	    << "#################################"
	    << "###################" << std::endl;
      return *this;
    }
    
    // friends for stream output:
    template <class T>
    friend Message& operator<<(Message&, const T&);
    friend Message& operator<<(Message&, std::ostream&(*)(std::ostream&));
  };

  template <class T>
  inline Message& operator<<(Message& msg, const T& output) {
    if (msg.is_active()) {
      *(msg._ostptr) << output;
    }
    return msg;
  }

  inline Message& operator<<(Message& msg, std::ostream&(*fp)(std::ostream&)) {
    if (msg.is_active()) {
      (*fp)(*msg._ostptr);
    }
    return msg;
  }

  class MessageStreams {
  private:
    static Message _result;
    static Message _forced;
    static Message _verbose;
    static Message _debug;
  public:
    static Message& result () { return _result;  }
    static Message& forced () { return _forced;  }
    static Message& verbose() { return _verbose; }
    static Message& debug  () { return _debug;   }
  };
  
  namespace message {

    // the following function define stream manipulators for messages:
    inline std::ostream& lock(std::ostream& ost) {
      ost.flush();
      IO_sync::mutex.lock();
      return ost;
    }

    inline std::ostream& unlock(std::ostream& ost) {
      ost.flush();
      IO_sync::mutex.unlock();
      return ost;
    }
    
    inline std::ostream& tab(std::ostream& ost) {
      ost << '\t';
      return ost;
    }
  }; // namespace message
  
}; // namespace topcom



#endif

// eof Message.hh