Annotation of parser3/src/include/pa_stylesheet_connection.h, revision 1.45

1.1       parser      1: /** @file
1.7       parser      2:        Parser: Stylesheet connection decl.
1.1       parser      3: 
1.44      moko        4:        Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)
1.26      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       parser      6: */
                      7: 
                      8: #ifndef PA_STYLESHEET_CONNECTION_H
                      9: #define PA_STYLESHEET_CONNECTION_H
1.29      paf        10: 
1.45    ! moko       11: #define IDENT_PA_STYLESHEET_CONNECTION_H "$Id: pa_stylesheet_connection.h,v 1.44 2017/02/07 22:00:37 moko Exp $"
1.1       parser     12: 
1.22      paf        13: #include "libxslt/xslt.h"
1.18      paf        14: #include "libxslt/xsltInternals.h"
                     15: 
1.33      paf        16: 
1.34      paf        17: #include "pa_xml_exception.h"
1.1       parser     18: #include "pa_common.h"
1.33      paf        19: #include "pa_globals.h"
1.39      paf        20: #include "pa_xml_io.h"
1.9       parser     21: 
1.1       parser     22: // defines
                     23: 
                     24: /**    Connection with stylesheet: 
                     25:        remembers time and can figure out that it needs recompilation
                     26: */
1.33      paf        27: class Stylesheet_connection: public PA_Object {
1.1       parser     28: 
1.19      paf        29:        friend class Stylesheet_connection_ptr;
                     30: 
1.33      paf        31: private:
                     32: 
1.37      paf        33:        String::Body ffile_spec;
1.33      paf        34:        xsltStylesheet *fstylesheet;
1.39      paf        35:        HashStringBool* dependencies;
1.33      paf        36:        time_t time_used;
                     37:        time_t prev_disk_time;
                     38: 
1.1       parser     39: public:
                     40: 
1.37      paf        41:        Stylesheet_connection(String::Body afile_spec):
1.1       parser     42:                ffile_spec(afile_spec),
1.36      paf        43:                fstylesheet(0),
1.39      paf        44:                dependencies(0),
1.36      paf        45:                time_used(0),
1.1       parser     46:                prev_disk_time(0),
1.37      paf        47:                used(0)  
1.39      paf        48:        {}
1.1       parser     49:        
1.37      paf        50:        String::Body file_spec() { return ffile_spec; }
1.1       parser     51: 
1.39      paf        52:        bool uncachable() {
                     53:                return !dependencies/*means they were external*/;
                     54:        }
                     55: 
1.1       parser     56:        bool expired(time_t older_dies) {
1.42      moko       57:                return uncachable() || (!used && time_used<older_dies);
1.1       parser     58:        }
1.15      paf        59:        time_t get_time_used() { return time_used; }
1.1       parser     60: 
                     61:        void disconnect() { 
1.18      paf        62:                xsltFreeStylesheet(fstylesheet);  fstylesheet=0; 
1.1       parser     63:        }
                     64: 
                     65:        bool connected() { return fstylesheet!=0; }
                     66: 
1.37      paf        67:        xsltStylesheet *stylesheet() { 
                     68:                time_t new_disk_time=0;
1.39      paf        69:                if(uncachable() || (new_disk_time=get_new_disk_time()))
1.1       parser     70:                        load(new_disk_time);
1.18      paf        71:                return fstylesheet; 
1.1       parser     72:        }
                     73: 
                     74: private:
                     75: 
1.19      paf        76:        /// return to cache
1.33      paf        77:        void close();
1.19      paf        78: 
                     79: private:
                     80: 
1.1       parser     81:        time_t get_new_disk_time() {
                     82:                time_t now_disk_time=get_disk_time();
                     83:                return now_disk_time>prev_disk_time?now_disk_time:0;
                     84:        }
                     85: 
1.39      paf        86:        void load(time_t new_disk_time);
                     87:        time_t get_disk_time();
1.1       parser     88: 
1.19      paf        89: private: // connection usage methods
                     90: 
1.33      paf        91:        int used;
1.19      paf        92:        void use() {
                     93:                time_used=time(0); // they started to use at this time
                     94:                used++;
                     95:        }
                     96:        void unuse() {
                     97:                used--;
                     98:                if(!used)
                     99:                        close();
                    100:        }
                    101: 
                    102: };
                    103: 
                    104: /// Auto-object used to track Stylesheet_connection usage
                    105: class Stylesheet_connection_ptr {
                    106:        Stylesheet_connection *fconnection;
                    107: public:
                    108:        explicit Stylesheet_connection_ptr(Stylesheet_connection *aconnection) : 
                    109:                fconnection(aconnection) {
                    110:                fconnection->use();
                    111:        }
                    112:        ~Stylesheet_connection_ptr() {
                    113:                fconnection->unuse();
                    114:        }
                    115:        Stylesheet_connection* operator->() {
                    116:                return fconnection;
                    117:        }
                    118: 
                    119:        // copying
                    120:        Stylesheet_connection_ptr(const Stylesheet_connection_ptr& src) : fconnection(src.fconnection) {
                    121:                fconnection->use();
                    122:        }
1.45    ! moko      123: 
1.19      paf       124:        Stylesheet_connection_ptr& operator =(const Stylesheet_connection_ptr& src) {
                    125:                // may do without this=src check
                    126:                fconnection->unuse();
                    127:                fconnection=src.fconnection;
                    128:                fconnection->use();
                    129: 
                    130:                return *this;
                    131:        }
1.1       parser    132: };
                    133: 
                    134: #endif

E-mail: