PLearn 0.1
TemporaryFileVMatrix.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // TemporaryFileVMatrix.cc
00004 //
00005 // Copyright (C) 2005 Olivier Delalleau
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 //
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 //
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 //
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************
00036    * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $
00037    ******************************************************* */
00038 
00039 // Authors: Olivier Delalleau
00040 
00044 #include "TemporaryFileVMatrix.h"
00045 #include <plearn/io/fileutils.h>    
00046 
00047 namespace PLearn {
00048 using namespace std;
00049 
00050 PLEARN_IMPLEMENT_OBJECT(
00051     TemporaryFileVMatrix,
00052     "FileVMatrix whose data file is deleted with the object.",
00053     "The '.pmat' file is actually only deleted when it is believed that no\n"
00054     "other FileVMatrix is using it. This is necessary for instance when\n"
00055     "a deep-copy of a FileVMatrix is performed (e.g. for cross-validation).\n"
00056     "Note that the metadatadir (file.pmat.metadata) will also be deleted at\n"
00057     "the same time."
00058 );
00059 
00061 // TemporaryFileVMatrix //
00063 TemporaryFileVMatrix::TemporaryFileVMatrix()
00064 {}
00065 
00066 TemporaryFileVMatrix::TemporaryFileVMatrix(const PPath& filename,
00067                                            bool writable):
00068     inherited(filename, writable)
00069 {
00070     build_();
00071 }
00072 
00073 TemporaryFileVMatrix::TemporaryFileVMatrix(const PPath& filename,
00074                                            int the_length, int the_width):
00075     inherited(filename, the_length, the_width)
00076 {
00077     build_();
00078 }
00079 
00081 // declareOptions //
00083 void TemporaryFileVMatrix::declareOptions(OptionList& ol)
00084 {
00085     // ### Declare all of this object's options here
00086     // ### For the "flags" of each option, you should typically specify
00087     // ### one of OptionBase::buildoption, OptionBase::learntoption or
00088     // ### OptionBase::tuningoption. Another possible flag to be combined with
00089     // ### is OptionBase::nosave
00090 
00091     // ### ex:
00092     // declareOption(ol, "myoption", &TemporaryFileVMatrix::myoption, OptionBase::buildoption,
00093     //               "Help text describing this option");
00094     // ...
00095 
00096     // Now call the parent class' declareOptions
00097     inherited::declareOptions(ol);
00098 }
00099 
00101 // build //
00103 void TemporaryFileVMatrix::build()
00104 {
00105     // ### Nothing to add here, simply calls build_
00106     inherited::build();
00107     build_();
00108 }
00109 
00111 // build_ //
00113 void TemporaryFileVMatrix::build_()
00114 {
00115     if (f) {
00116         // File has been opened successfully.
00117         addReferenceToFile(filename_);
00118         last_filename = filename_;
00119     } else
00120         last_filename = "";
00121     updateMtime(filename_);
00122 }
00123 
00125 // closeCurrentFile //
00127 void TemporaryFileVMatrix::closeCurrentFile()
00128 {
00129     if (f)
00130         removeReferenceToFile(last_filename);
00131     inherited::closeCurrentFile();
00132 }
00133 
00135 // makeDeepCopyFromShallowCopy //
00137 void TemporaryFileVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00138 {
00139     // Clearing 'last_filename' is important: since at this point it is a
00140     // shallow copy, the file pointed by 'last_filename' has not actually
00141     // been opened by this object, thus there is no need to decrease its
00142     // number of references when build() is called in the parent
00143     // makeDeepCopyFromShallowCopy(..) method.
00144     last_filename = "";
00145     inherited::makeDeepCopyFromShallowCopy(copies);
00146 }
00147 
00149 // ~TemporaryFileVMatrix //
00151 TemporaryFileVMatrix::~TemporaryFileVMatrix()
00152 {
00153     TemporaryFileVMatrix::closeCurrentFile();
00154     if (noReferenceToFile(filename_)) {
00155         rm(filename_);
00156         if (hasMetaDataDir()) {
00157             force_rmdir(getMetaDataDir());
00158             // Clear the metadatadir so that the parent class does not try to
00159             // do anything with it.
00160             metadatadir = "";
00161         }
00162     }
00163 }
00164 
00165 } // end of namespace PLearn
00166 
00167 
00168 /*
00169   Local Variables:
00170   mode:c++
00171   c-basic-offset:4
00172   c-file-style:"stroustrup"
00173   c-file-offsets:((innamespace . 0)(inline-open . 0))
00174   indent-tabs-mode:nil
00175   fill-column:79
00176   End:
00177 */
00178 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines