home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / listpm7.zip / ltper.hpp < prev    next >
C/C++ Source or Header  |  1999-07-14  |  5KB  |  145 lines

  1. /*
  2.     listPM list files under Presentation Manager. Uses Open Class Libarary.
  3.     Copyright (C) 1998, 1999  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.     PMB # 181
  21.     11900 Metric Blvd Ste. J
  22.     Austin Tx 78758-3117
  23.     pelliott@io.com
  24. */
  25. #ifndef LTPER
  26. #define LTPER
  27. // This Abstract Base Class define a mechinism to
  28. // move raw memory to and from some sort of abstract
  29. // external memory store.
  30.  
  31. class ToStorage
  32. {
  33.   public:
  34.      virtual void read ( char * buf,int count) = 0;
  35.      virtual void write (const char * buf,int count) = 0;
  36.      virtual Boolean isInStorage(void) = 0;
  37.      virtual ~ToStorage() {};
  38. };
  39.  
  40. // In order for an "object" to be light weight persistant,
  41. // it must be mapable to "essance" of just plain data. That is something
  42. // that can be copied bytewise (without loosing underlying meaning
  43. // if any) and having no pointers (including vtbl pointers from
  44. // virtual members) or other connection to the world outside.
  45.  
  46.  
  47. // In what follows, it must be possible to construct "object"
  48. // from its essance (Just Plain Data). Also, "object" must
  49. // return a reference to its essance. Or construct an essance
  50. // from an from an object
  51.  
  52. // Aessance is a type that must be constructable from an object
  53. // by conversion operator or constructable it must be possible
  54. // to assign a const essance& to an Aessance
  55. // I have used Aessance = const essance&
  56. // for cases where an object will return a reference to an essance
  57. // and used Aessance = essance in cases where essance is constructable
  58. // from an object.
  59.  
  60. template<class object,class essance, typename Aessance=const essance& >
  61. class ExternalExistance
  62. {
  63.      private:
  64.         // reference to raw storage interface.
  65.         ToStorage& storage;
  66.  
  67.         // returns a essance read from external.
  68.         operator essance()
  69.         {
  70.             essance essance_instance;
  71.  
  72.             // cast the essance to raw characters & read it in.
  73.             char * str = (char*) &essance_instance;
  74.             storage.read( str, sizeof(essance_instance) );
  75.             return essance_instance;
  76.         };
  77.  
  78.      public:
  79.         // true if indicated object exists externally.
  80.         // use storage to determine if exists.
  81.         Boolean ExistsExternally()
  82.         {
  83.             return storage.isInStorage();
  84.         };
  85.  
  86.         // construct self from a ToStorage
  87.         // save the passed in storage which will really a derived object.
  88.         ExternalExistance(ToStorage& stor) : storage(stor)
  89.         {};
  90.  
  91.         // destructor.
  92.         virtual ~ExternalExistance() {};
  93.  
  94.         // write the indicated object to external storage.
  95.         ExternalExistance<object,essance,Aessance> &
  96.                      operator<<(const object& obj_instance)
  97.         {
  98.             // get the essance of the object. (As a reference to a constant essance)
  99.             // choose Aessance so that this statement works!
  100.             const essance& essance_instance = ( Aessance )obj_instance;
  101.  
  102.             // convert the essance to bytes.
  103.             const char * str = (const char*) &essance_instance;
  104.  
  105.             // write the essance bytes to external.
  106.             storage.write(str,sizeof(essance) );
  107.  
  108.             // done.
  109.             return *this;
  110.         };
  111.  
  112.         // The class Created is identical to object except it can be
  113.         // created from external storage. Works because object
  114.         // can be created from essance.
  115.         class Created : public object
  116.         {
  117.           public:
  118.             // constructor. Create essance from external store.
  119.             // create object=Created from essance.
  120.             Created(ExternalExistance<object,essance,Aessance> & exist)  :
  121.                object( essance(exist) )
  122.             {};
  123.         };
  124.         // Created constructor can use ExternalStorage to construct
  125.         // an essance.
  126.         friend Created::Created(ExternalExistance<object,essance,Aessance> & exist);
  127. };
  128.  
  129.  
  130. // In the above template object must be a class.
  131. // otherwise compile error. To get around this for builtin types
  132. // such as int or char, we provide the following template.
  133. // which creates a class=struct really which embodies a builtin
  134. // type for the purpose of ExternalExistance.
  135. template <class X>
  136. struct BuiltIn
  137. {
  138.       X   built_in;
  139.       operator X() const { return built_in; };
  140.       BuiltIn(const X& built_in) : built_in(built_in) {};
  141. };
  142.  
  143.  
  144. #endif // LTPER
  145.