File: loadinglistener.hpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (61 lines) | stat: -rw-r--r-- 2,118 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
#ifndef COMPONENTS_LOADINGLISTENER_H
#define COMPONENTS_LOADINGLISTENER_H

#include <memory>
#include <string>

namespace Loading
{
    class Listener
    {
    public:
        /// Set a text label to show on the loading screen.
        /// @param label The label
        /// @param important Is the label considered important to show?
        /// @note "non-important" labels may not show on screen if the loading process went so fast
        /// that the implementation decided not to show a loading screen at all. "important" labels
        /// will show in a separate message-box if the loading screen was not shown.
        virtual void setLabel(const std::string& label, bool important = false) {}

        /// Start a loading sequence. Must call loadingOff() when done.
        /// @note To get the loading screen to actually update, you must call setProgress / increaseProgress
        /// periodically.
        /// @note It is best to use the ScopedLoad object instead of using loadingOn()/loadingOff() directly,
        ///  so that the loading is exception safe.
        virtual void loadingOn() {}
        virtual void loadingOff() {}

        /// Set the total range of progress (e.g. the number of objects to load).
        virtual void setProgressRange(size_t range) {}
        /// Set current progress. Valid range is [0, progressRange)
        virtual void setProgress(size_t value) {}
        /// Increase current progress, default by 1.
        virtual void increaseProgress(size_t increase = 1) {}

        virtual ~Listener() = default;
    };

    struct LoadingOff
    {
        void operator()(Listener* listener) const
        {
            if (listener != nullptr)
                listener->loadingOff();
        }
    };

    /// @brief Used for stopping a loading sequence when the object goes out of scope
    struct ScopedLoad
    {
        std::unique_ptr<Listener, LoadingOff> mListener;

        explicit ScopedLoad(Listener* listener)
            : mListener(listener)
        {
            if (mListener != nullptr)
                mListener->loadingOn();
        }
    };
}

#endif