summaryrefslogtreecommitdiff
path: root/library/globalFunctions.h
blob: 689fa5f135086b6d57c903e62c41b234154dbb87 (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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifndef GLOBALFUNCTIONS_H_INCLUDED
#define GLOBALFUNCTIONS_H_INCLUDED

#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <wx/string.h>
#include <wx/longlong.h>
#include <memory>
#include <sstream>

class wxInputStream;
class wxOutputStream;
class wxStopWatch;


namespace globalFunctions
{
    inline
    int round(double d) //little rounding function
    {
        return static_cast<int>(d < 0 ? d - .5 : d + .5);
    }

    template <class T>
    inline
    T abs(const T& d) //absolute value
    {
        return(d < 0 ? -d : d);
    }

    template <class T>
    inline std::string numberToString(const T& number) //convert number to string the C++ way
    {
        std::stringstream ss;
        ss << number;
        return ss.str();
    }

    wxString numberToWxString(const unsigned int number); //convert number to wxString
    wxString numberToWxString(const int number);          //convert number to wxString
    wxString numberToWxString(const float number);        //convert number to wxString

    int    stringToInt(   const std::string& number); //convert String to number
    long   stringToLong(  const std::string& number); //convert String to number
    double stringToDouble(const std::string& number); //convert String to number

    int    wxStringToInt(   const wxString& number); //convert wxString to number
    double wxStringToDouble(const wxString& number); //convert wxString to number

    wxString includeNumberSeparator(const wxString& number);

    int readInt(std::ifstream& stream);  //read int from file stream
    void writeInt(std::ofstream& stream, const int number);  //write int to filestream

    int readInt(wxInputStream& stream);  //read int from file stream
    void writeInt(wxOutputStream& stream, const int number);  //write int to filestream

    inline
    wxLongLong convertToSigned(const wxULongLong number)
    {
        return wxLongLong(number.GetHi(), number.GetLo());
    }


    //Note: the following lines are a performance optimization for deleting elements from a vector. It is incredibly faster to create a new
//vector and leave specific elements out than to delete row by row and force recopying of most elements for each single deletion (linear vs quadratic runtime)
    template <class T>
    void removeRowsFromVector(const std::set<int>& rowsToRemove, std::vector<T>& grid)
    {
        if (rowsToRemove.size() > 0)
        {
            std::vector<T> temp;

            std::set<int>::const_iterator rowToSkipIndex = rowsToRemove.begin();
            int rowToSkip = *rowToSkipIndex;

            for (int i = 0; i < int(grid.size()); ++i)
            {
                if (i != rowToSkip)
                    temp.push_back(grid[i]);
                else
                {
                    ++rowToSkipIndex;
                    if (rowToSkipIndex != rowsToRemove.end())
                        rowToSkip = *rowToSkipIndex;
                }
            }
            grid.swap(temp);
        }
    }


    template <class T>
    inline
    void mergeVectors(const std::vector<T>& input, std::vector<T>& changing)
    {
        for (typename std::vector<T>::const_iterator i = input.begin(); i != input.end(); ++i) //nested dependent typename: see Meyers effective C++
            changing.push_back(*i);
    }
}


//############################################################################
class Performance
{
public:
    wxDEPRECATED(Performance()); //generate compiler warnings as a reminder to remove code after measurements
    ~Performance();
    void showResult();

private:
    bool resultWasShown;
    std::auto_ptr<wxStopWatch> timer;
};

//two macros for quick performance measurements
#define PERF_START Performance a;
#define PERF_STOP  a.showResult();


//############################################################################
class wxFile;
class DebugLog
{
public:
    wxDEPRECATED(DebugLog());
    ~DebugLog();
    void write(const wxString& logText);

private:
    wxString assembleFileName();
    wxString logfileName;
    int lineCount;
    wxFile* logFile; //logFile.close(); <- not needed
};
extern DebugLog logDebugInfo;
wxString getCodeLocation(const wxString file, const int line);

//small macro for writing debug information into a logfile
#define WRITE_DEBUG_LOG(x) logDebugInfo.write(getCodeLocation(__TFILE__, __LINE__) + x);
//speed alternative: wxLogDebug(wxT("text")) + DebugView


//############################################################################
class RuntimeException //Exception class used to notify of general runtime exceptions
{
public:
    RuntimeException(const wxString& txt) : errorMessage(txt) {}

    wxString show() const
    {
        return errorMessage;
    }

private:
    wxString errorMessage;
};


#endif // GLOBALFUNCTIONS_H_INCLUDED
bgstack15