File: MapFileOperation.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (124 lines) | stat: -rw-r--r-- 2,755 bytes parent folder | download | duplicates (4)
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
#pragma once

#include <stdexcept>
#include "imessagebus.h"

namespace map
{

/**
 * Message sent when the import/export code is about to start 
 * working on a stream.
 * 
 * If any listener wishes to cancel the operation, the cancel() 
 * method is available, which will throw an internal exception.
 */
class FileOperation :
    public radiant::IMessage
{
public:
    enum class Type
    {
        Import,
        Export,
    };

    enum MessageType
    {
        Started,  // operation started, called once
        Progress, // operation in progress, called 0..N times
        Finished, // operation finished, called once
    };

    class OperationCancelled :
        public std::runtime_error
    {
    public:
        OperationCancelled() :
            runtime_error("")
        {}
    };

private:
    Type _type;
    MessageType _msgType;
    float _progressFraction;
    bool _canCalculateProgress;
    std::string _message;

public:
    FileOperation(Type operationType, MessageType msgType, bool canCalculateProgress) :
        FileOperation(operationType, msgType, canCalculateProgress, 0.0f)
    {}

    FileOperation(Type operationType, MessageType msgType, bool canCalculateProgress, float progressFraction) :
        _type(operationType),
        _msgType(msgType),
        _progressFraction(progressFraction),
        _canCalculateProgress(canCalculateProgress)
    {
        if (_progressFraction < 0)
        {
            _progressFraction = 0;
        }
        else if (_progressFraction > 1.0f)
        {
            _progressFraction = 1.0f;
        }

        if (_msgType == Started)
        {
            _progressFraction = 0;
        }
        else if (_msgType == Finished)
        {
            _progressFraction = 1;
        }
    }

    std::size_t getId() const override
    {
        return IMessage::Type::MapFileOperation;
    }

    const std::string& getText() const
    {
        return _message;
    }

    void setText(const std::string& message)
    {
        _message = message;
    }

    Type getOperationType() const
    {
        return _type;
    }

    MessageType getMessageType() const
    {
        return _msgType;
    }

    // If canCalculateProgress() is true, this indicates the progress in the range of [0..1]
    float getProgressFraction() const
    {
        return _progressFraction;
    }

    // Whether the information in this message can be used to show a meaningful progress meter
    bool canCalculateProgress() const
    {
        return _canCalculateProgress;
    }

    // Call this to cancel the ongoing export process
    // This will throw an exception, so control won't be returned to the caller
    void cancelOperation()
    {
        throw OperationCancelled();
    }
};

}