ProgressCombinedImpl.h revision 545a02e9eda0fc9d6be6f8513686228283f73627
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync/* $Id$ */
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync *
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync * VirtualBox COM class implementation
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync */
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync/*
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync * Copyright (C) 2006-2010 Sun Microsystems, Inc.
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync *
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync * available from http://www.virtualbox.org. This file is free software;
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * additional information or have any questions.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync */
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync#ifndef ____H_PROGRESSCOMBINEDIMPL
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync#define ____H_PROGRESSCOMBINEDIMPL
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync#include "ProgressImpl.h"
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync#include "AutoCaller.h"
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync#include <vector>
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync/**
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync * The CombinedProgress class allows to combine several progress objects to a
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync * single progress component. This single progress component will treat all
7d6e0aa02bf89fa403a13d5eac18a18b5a79893fvboxsync * operations of individual progress objects as a single sequence of operations
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync * that follow each other in the same order as progress objects are passed to
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync * the #init() method.
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync *
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync * Individual progress objects are sequentially combined so that this progress
6473a6585394a0255de9936152f2fd35d068b347vboxsync * object:
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync *
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync * - is cancelable only if all progresses are cancelable.
7d6e0aa02bf89fa403a13d5eac18a18b5a79893fvboxsync * - is canceled once a progress that follows next to successfully completed
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync * ones reports it was canceled.
f4db180328f833f9fc9cb07a1a4a0bc948a47afevboxsync * - is completed successfully only after all progresses are completed
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync * successfully.
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync * - is completed unsuccessfully once a progress that follows next to
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync * successfully completed ones reports it was completed unsuccessfully;
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync * the result code and error info of the unsuccessful progress
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync * will be reported as the result code and error info of this progress.
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * - returns N as the operation number, where N equals to the number of
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * operations in all successfully completed progresses starting from the
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * first one plus the operation number of the next (not yet complete)
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * progress; the operation description of the latter one is reported as
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * the operation description of this progress object.
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * - returns P as the percent value, where P equals to the sum of percents
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * of all successfully completed progresses starting from the
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * first one plus the percent value of the next (not yet complete)
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * progress, normalized to 100%.
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync *
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * @note It's the respoisibility of the combined progress object creator to
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * complete individual progresses in the right order: if, let's say, the
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * last progress is completed before all previous ones,
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * #WaitForCompletion(-1) will most likely give 100% CPU load because it
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * will be in a loop calling a method that returns immediately.
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync */
7995839c0b791ae2334df998d34dbccac12b3b41vboxsyncclass ATL_NO_VTABLE CombinedProgress :
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync public com::SupportErrorInfoDerived<ProgressBase, CombinedProgress, IProgress>,
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync public VirtualBoxSupportTranslation<CombinedProgress>
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync{
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsyncpublic:
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync
3a109c62545ec1a0ebca5c8c3e5e9b88d3c7b672vboxsync VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE (CombinedProgress)
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync DECLARE_NOT_AGGREGATABLE (CombinedProgress)
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync DECLARE_PROTECT_FINAL_CONSTRUCT()
3a109c62545ec1a0ebca5c8c3e5e9b88d3c7b672vboxsync
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync BEGIN_COM_MAP (CombinedProgress)
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync COM_INTERFACE_ENTRY (ISupportErrorInfo)
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync COM_INTERFACE_ENTRY (IProgress)
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync COM_INTERFACE_ENTRY2 (IDispatch, IProgress)
3a109c62545ec1a0ebca5c8c3e5e9b88d3c7b672vboxsync END_COM_MAP()
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync HRESULT FinalConstruct();
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync void FinalRelease();
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync
3a109c62545ec1a0ebca5c8c3e5e9b88d3c7b672vboxsync // public initializer/uninitializer for internal purposes only
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync HRESULT init (
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync#if !defined (VBOX_COM_INPROC)
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync VirtualBox *aParent,
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync#endif
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync IUnknown *aInitiator,
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync CBSTR aDescription,
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync IProgress *aProgress1, IProgress *aProgress2,
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync OUT_GUID aId = NULL);
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync /**
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * Initializes the combined progress object given the first and the last
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * normal progress object from the list.
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync *
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync * @param aParent See ProgressBase::init().
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync * @param aInitiator See ProgressBase::init().
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync * @param aDescription See ProgressBase::init().
da41353d6837bbc94e31da46f0698be45c106305vboxsync * @param aFirstProgress Iterator of the first normal progress object.
da41353d6837bbc94e31da46f0698be45c106305vboxsync * @param aSecondProgress Iterator of the last normal progress object.
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync * @param aId See ProgressBase::init().
da41353d6837bbc94e31da46f0698be45c106305vboxsync */
e1cda3b0b3faa83df2fe0bf69e6938a6e7c4cb20vboxsync template <typename InputIterator>
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync HRESULT init (
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync#if !defined (VBOX_COM_INPROC)
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync VirtualBox *aParent,
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync#endif
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync IUnknown *aInitiator,
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync CBSTR aDescription,
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync InputIterator aFirstProgress, InputIterator aLastProgress,
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync OUT_GUID aId = NULL)
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync {
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync /* Enclose the state transition NotReady->InInit->Ready */
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync AutoInitSpan autoInitSpan (this);
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync AssertReturn (autoInitSpan.isOk(), E_FAIL);
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync mProgresses = ProgressVector (aFirstProgress, aLastProgress);
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync HRESULT rc = protectedInit (autoInitSpan,
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync#if !defined (VBOX_COM_INPROC)
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync aParent,
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync#endif
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync aInitiator, aDescription, aId);
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync /* Confirm a successful initialization when it's the case */
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync if (SUCCEEDED (rc))
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync autoInitSpan.setSucceeded();
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync return rc;
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync }
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync
7995839c0b791ae2334df998d34dbccac12b3b41vboxsyncprotected:
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync HRESULT protectedInit (AutoInitSpan &aAutoInitSpan,
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync#if !defined (VBOX_COM_INPROC)
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync VirtualBox *aParent,
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync#endif
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync IUnknown *aInitiator,
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync CBSTR aDescription, OUT_GUID aId);
1fb61f986d9f400eb3cc440422475f0fee5f96advboxsync
7995839c0b791ae2334df998d34dbccac12b3b41vboxsyncpublic:
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync void uninit();
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync // IProgress properties
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync STDMETHOD(COMGETTER(Percent)) (ULONG *aPercent);
7995839c0b791ae2334df998d34dbccac12b3b41vboxsync STDMETHOD(COMGETTER(Completed)) (BOOL *aCompleted);
STDMETHOD(COMGETTER(Canceled)) (BOOL *aCanceled);
STDMETHOD(COMGETTER(ResultCode)) (LONG *aResultCode);
STDMETHOD(COMGETTER(ErrorInfo)) (IVirtualBoxErrorInfo **aErrorInfo);
STDMETHOD(COMGETTER(Operation)) (ULONG *aCount);
STDMETHOD(COMGETTER(OperationDescription)) (BSTR *aOperationDescription);
STDMETHOD(COMGETTER(OperationPercent)) (ULONG *aOperationPercent);
STDMETHOD(COMSETTER(Timeout)) (ULONG aTimeout);
STDMETHOD(COMGETTER(Timeout)) (ULONG *aTimeout);
// IProgress methods
STDMETHOD(WaitForCompletion) (LONG aTimeout);
STDMETHOD(WaitForOperationCompletion) (ULONG aOperation, LONG aTimeout);
STDMETHOD(Cancel)();
STDMETHOD(SetCurrentOperationProgress)(ULONG aPercent)
{
NOREF(aPercent);
return E_NOTIMPL;
}
STDMETHOD(SetNextOperation)(IN_BSTR bstrNextOperationDescription, ULONG ulNextOperationsWeight)
{
NOREF(bstrNextOperationDescription); NOREF(ulNextOperationsWeight);
return E_NOTIMPL;
}
// public methods only for internal purposes
/** For com::SupportErrorInfoImpl. */
static const char *ComponentName() { return "CombinedProgress"; }
private:
HRESULT checkProgress();
typedef std::vector <ComPtr<IProgress> > ProgressVector;
ProgressVector mProgresses;
size_t mProgress;
ULONG mCompletedOperations;
};
#endif /* ____H_PROGRESSCOMBINEDIMPL */