home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / listpm.zip / source / ltper.hpp < prev    next >
C/C++ Source or Header  |  1996-07-08  |  4KB  |  130 lines

  1. /*
  2.     listPM list files under Presentation Manager. Uses Open Class Libarary.
  3.     Copyright (C) 1996  Paul Elliott
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     Paul Elliott
  20.     3987 South Gessner #224
  21.     Houston Tx 77063
  22.     Paul.Elliott@Hrnowl.LoneStar.Org
  23. */
  24. #ifndef LTPER
  25. #define LTPER
  26. // This Abstract Base Class define a mechinism to
  27. // move raw memory to and from some sort of abstract
  28. // external memory store.
  29.  
  30. class ToStorage
  31. {
  32.   public:
  33.      virtual void read ( char * buf,int count) = 0;
  34.      virtual void write (const char * buf,int count) = 0;
  35.      virtual Boolean isInStorage(void) = 0;
  36.      virtual ~ToStorage() {};
  37. };
  38.  
  39. // In order for an "object" to be light weight persistant,
  40. // it must be mapable to "essance" of just plain data. That is something
  41. // that can be copied bytewise (without loosing underlying meaning
  42. // if any) and having no pointers (including vtbl pointers from
  43. // virtual members) or other connection to the world outside.
  44.  
  45.  
  46. // In what follows, it must be possible to construct "object"
  47. // from its essance (Just Plain Data). Also, "object" must
  48. // return a reference to its essance.
  49.  
  50. template<class object,class essance>
  51. class ExternalExistance
  52. {
  53.      private:
  54.         // reference to raw storage interface.
  55.         ToStorage& storage;
  56.  
  57.         // returns a essance read form external.
  58.         operator essance()
  59.         {
  60.             essance essance_instance;
  61.  
  62.             char * str = (char*) &essance_instance;
  63.             storage.read( str, sizeof(essance_instance) );
  64.             return essance_instance;
  65.         };
  66.  
  67.      public:
  68.         // true if indicated object exists externally.
  69.         Boolean ExistsExternally()
  70.         {
  71.             return storage.isInStorage();
  72.         };
  73.  
  74.         // construct self from a ToStorage
  75.         ExternalExistance(ToStorage& stor) : storage(stor)
  76.         {};
  77.  
  78.         // destructor.
  79.         virtual ~ExternalExistance() {};
  80.  
  81.         // write the indicated object to external storage.
  82.         ExternalExistance<object,essance> &
  83.                      operator<<(const object& obj_instance)
  84.         {
  85.             // get the essance of the object.
  86.             const essance& essance_instance = (const essance&)obj_instance;
  87.  
  88.             // convert the essance to bytes.
  89.             const char * str = (const char*) &essance_instance;
  90.  
  91.             // write the essance bytes to external.
  92.             storage.write(str,sizeof(essance) );
  93.  
  94.             // done.
  95.             return *this;
  96.         };
  97.  
  98.         // The class Created is identical to object except it can be
  99.         // created from external storage. Works because object
  100.         // can be created from essance.
  101.         class Created : public object
  102.         {
  103.           public:
  104.             // constructor. Create essance from external store.
  105.             // create object=Created from essance.
  106.             Created(ExternalExistance<object,essance> & exist)  :
  107.                object( essance(exist) )
  108.             {};
  109.         };
  110.         // Created constructor can use ExternalStorage to construct
  111.         // an essance.
  112.         friend Created::Created(ExternalExistance<object,essance> & exist);
  113. };
  114.  
  115. // In the above template object must be a class.
  116. // otherwise compile error. To get around this for builtin types
  117. // such as int or char, we provide the following template.
  118. // which creates a class=struct really which embodies a builtin
  119. // type for the purpose of ExternalExistance.
  120. template <class X>
  121. struct BuiltIn
  122. {
  123.       X   built_in;
  124.       operator X() const { return built_in; };
  125.       BuiltIn(const X& built_in) : built_in(built_in) {};
  126. };
  127.  
  128.  
  129. #endif // LTPER
  130.