ProgressResource.h

Go to the documentation of this file.
00001 /*
00002  * The information in this file is
00003  * Copyright(c) 2007 Ball Aerospace & Technologies Corporation
00004  * and is subject to the terms and conditions of the
00005  * GNU Lesser General Public License Version 2.1
00006  * The license text is available from   
00007  * http://www.gnu.org/licenses/lgpl.html
00008  */
00009 
00010 #ifndef PROGRESSRESOURCE_H__
00011 #define PROGRESSRESOURCE_H__
00012 
00013 #include "ApplicationServices.h"
00014 #include "Progress.h"
00015 #include "Resource.h"
00016 #include "DesktopServices.h"
00017 #include "UtilityServices.h"
00018 
00019 #include <string>
00020 
00021 /**
00022  * The ProgressObject is a trait object for use with the Resource template. 
00023  *
00024  * @see ProgressResource
00025  */
00026 class ProgressObject
00027 {
00028 public:
00029    /**
00030    * This is an implementation detail of the ProgressObject class. 
00031    */
00032    struct Args
00033    {
00034       bool mThreadSafe;
00035       bool mCreateDialog;
00036       std::string mCaption;
00037       Progress *mpProgress;
00038       Args(bool threadSafe, bool createDialog, const std::string &caption) :
00039          mThreadSafe(threadSafe), mCreateDialog(createDialog), mCaption(caption), mpProgress(NULL) {}
00040       Args(Progress *pProgress, bool createDialog, const std::string &caption) :
00041          mThreadSafe(false), mCreateDialog(createDialog), mCaption(caption), mpProgress(pProgress) {}
00042    };
00043 
00044    Progress *obtainResource(const Args &args) const 
00045    {
00046       Progress *pProgress = args.mpProgress;
00047       if (pProgress == NULL)
00048       {
00049          pProgress = Service<UtilityServices>()->getProgress(args.mThreadSafe);
00050       }
00051 
00052       if (pProgress != NULL && args.mCreateDialog == true && Service<ApplicationServices>()->isInteractive() == true)
00053       {
00054          if (Service<DesktopServices>()->createProgressDialog(args.mCaption, pProgress) == false)
00055          {
00056             releaseResource(args, pProgress);
00057             pProgress = NULL;
00058          }
00059       }
00060 
00061       return pProgress;
00062    }
00063 
00064    void releaseResource(const Args &args, Progress *pProgress) const 
00065    {
00066       Service<UtilityServices>()->destroyProgress(pProgress);
00067    }
00068 };
00069 
00070 /**
00071  *  This is a Resource class that manages Progress objects.
00072  *
00073  *  This resource creates a Progress objects and destroys it
00074  *  when going out of scope. Optionally, a ProgressDialog will
00075  *  be created for the Progress object. The second form of this
00076  *  resource takes ownership of an existing Progress object.
00077 */
00078 class ProgressResource : public Resource<Progress,ProgressObject>
00079 {
00080 public:
00081    /**
00082     *  Constructs a Resource object that manages a Progress object.
00083     *
00084     *  This creates a new Progress object and optionally associates
00085     *  a ProgressDialog with it.
00086     *
00087     *  @param   caption
00088     *           If this is not empty, a ProgressDialog will be
00089     *           created with this caption.
00090     *  @param   threadSafe
00091     *           If true, a thread-safe Progress object is created.
00092     */
00093    explicit ProgressResource(const std::string &caption = std::string(), bool threadSafe = false) :
00094          Resource<Progress,ProgressObject>(ProgressObject::Args(threadSafe,
00095             !caption.empty() && Service<ApplicationServices>()->isInteractive(), caption)) {}
00096    /**
00097     *  Constructs a Resource object that manages a Progress object.
00098     *
00099     *  This takes ownership of an existing Progress object and optionally associates
00100     *  a ProgressDialog with it.
00101     *
00102     *  @param   progress
00103     *           The exisiting Progress object.
00104     *  @param   caption
00105     *           If this is not empty, a ProgressDialog will be
00106     *           created with this caption.
00107     */
00108    explicit ProgressResource(Progress &progress, const std::string &caption = std::string()) :
00109          Resource<Progress,ProgressObject>(ProgressObject::Args(&progress,
00110             !caption.empty() && Service<ApplicationServices>()->isInteractive(), caption)) {}
00111 };
00112 
00113 #endif

Software Development Kit - Opticks 4.9.0 Build 16218