cid#1607171 Data race condition
[LibreOffice.git] / sd / source / ui / inc / tools / AsynchronousCall.hxx
blobfa4c180207aa618d33d82db694887840e7ec2c4c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include <vcl/timer.hxx>
24 #include <memory>
25 #include <functional>
27 namespace sd::tools
29 /** Store a function object and execute it asynchronous.
31 The features of this class are:
32 a) It provides a wrapper around a VCL Timer so that generic function
33 objects can be used.
34 b) When more than one function objects are posted to be executed later
35 then the pending ones are erased and only the last one will actually be
36 executed.
38 Use this class like this:
39 aInstanceOfAsynchronousCall.Post(
40 ::std::bind(
41 ::std::mem_fun(&DrawViewShell::SwitchPage),
42 pDrawViewShell,
43 11));
45 class AsynchronousCall
47 public:
48 /** Create a new asynchronous call. Each object of this class processes
49 one (semantical) type of call.
51 AsynchronousCall();
53 ~AsynchronousCall();
55 /** Post a function object that is to be executed asynchronously. When
56 this method is called while the current function object has not been
57 executed then the latter is destroyed and only the given function
58 object will be executed.
59 @param rFunction
60 The function object that may be called asynchronously in the
61 near future.
63 typedef ::std::function<void()> AsynchronousFunction;
64 void Post(const AsynchronousFunction& rFunction);
66 private:
67 Timer maTimer;
68 /** The function object that will be executed when the TimerCallback
69 function is called the next time. This pointer may be NULL.
71 ::std::unique_ptr<AsynchronousFunction> mpFunction;
72 DECL_LINK(TimerCallback, Timer*, void);
75 } // end of namespace ::sd::tools
77 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */