Wrappers.h

Go to the documentation of this file.
00001 /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 5 -*- */
00002 
00011 /*
00012  * Created by Daniel Aarno on Sun Oct Oct, 2004.
00013  * Copyright (c) 2004 by Daniel Aarno - <macbishop@users.sf.net>
00014  * M. Sc. in Electrical Engineering * http://www.e.kth.se/~e98_aar
00015  * 
00016  * All Rights Reserved
00017  * ATTRIBUTION ASSURANCE LICENSE (adapted from the original BSD license)
00018  * Redistribution and use in source and binary forms, with or without
00019  * modification, are permitted provided that the conditions below are met.
00020  * These conditions require a modest attribution to  (the
00021  * "Author"), who hopes that its promotional value may help justify the
00022  * thousands of dollars in otherwise billable time invested in writing
00023  * this and other freely available, open-source software.
00024  * 
00025  * 1. Redistributions of source code, in whole or part and with or without
00026  * modification (the "Code"), must prominently display this GPG-signed
00027  * text in verifiable form.
00028  * 2. Redistributions of the Code in binary form must be accompanied by
00029  * this GPG-signed text in any documentation and, each time the resulting
00030  * executable program or a program dependent thereon is launched, a
00031  * prominent display (e.g., splash screen or banner text) of the Author's
00032  * attribution information, which includes:
00033  * (a) Name ("Daniel Aarno"),
00034  * (b) Professional identification ("M. Sc. in Electrical Engineering"), and
00035  * (c) URL ("http://www.e.kth.se/~e98_aar").
00036  * 3. Neither the name nor any trademark of the Author may be used to
00037  * endorse or promote products derived from this software without specific
00038  * prior written permission.
00039  * 4. Users are entirely responsible, to the exclusion of the Author and
00040  * any other persons, for compliance with (1) regulations set by owners or
00041  * administrators of employed equipment, (2) licensing terms of any other
00042  * software, and (3) local regulations regarding use, including those
00043  * regarding import, export, and use of encryption software.
00044  * 
00045  * THIS FREE SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND
00046  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00047  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00048  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
00049  * EVENT SHALL THE AUTHOR OR ANY CONTRIBUTOR BE LIABLE FOR
00050  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00051  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00052  * EFFECTS OF UNAUTHORIZED OR MALICIOUS NETWORK ACCESS;
00053  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00054  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00055  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00056  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00057  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00058  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00059  */
00060 
00061 #ifndef _WRAPPERS_
00062 #define _WRAPPERS_
00063 
00064 #include <stdio.h>
00065 
00070 namespace WRAPPERS {
00078      class File {
00079           FILE* m_fp;
00080           
00081           void close(FILE* fp) const { if(fp) fclose(fp); }
00082      public:
00092           File(FILE* fp = 0) : m_fp(fp) {}
00093 
00100           File(const char* path, const char* mode) {
00101                m_fp = fopen(path, mode);
00102           }
00103 
00111           File(File &o) : m_fp(o.m_fp) { o.m_fp = 0; }
00112           
00114           ~File() { fclose(m_fp); m_fp = 0; }
00115           
00117           operator const FILE*() const { return m_fp; }
00118 
00131           FILE* get() { return m_fp; }
00132           
00141           const File& operator=(File &rhs) { 
00142                if(&rhs == this)
00143                     return *this;
00144                close(m_fp); 
00145                m_fp = rhs.m_fp; 
00146                rhs.m_fp = 0;  
00147                return *this;
00148           }
00149      };//File
00150 
00158      template<typename T> class auto_ary {
00159           T* m_ptr;
00160 
00161      public:
00168           explicit auto_ary(T* ptr=0) : m_ptr(ptr) {}
00169 
00177           auto_ary(auto_ary<T> &o) : m_ptr(o.m_ptr) { o.m_ptr = 0; }
00178 
00184           ~auto_ary() { delete[] m_ptr; m_ptr = 0; }
00185           
00194           auto_ary<T>& operator=(auto_ary<T> &rhs) {
00195                if(this == &rhs)
00196                     return *this;
00197                
00198                delete[] m_ptr;
00199                m_ptr = rhs.m_ptr;
00200                rhs.m_ptr = 0;
00201           }
00202           
00209           const T& operator[](int i) const { return m_ptr[i]; }
00210 
00217           T& operator[](int i) { return m_ptr[i]; }
00218           
00229           T* get() const { return m_ptr; }
00230      }; //auto_ary
00231 } //namespace
00232 
00233 #endif

Generated on Sun Mar 11 15:33:24 2007 for MetalWarriors by  doxygen 1.4.7