Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
6.5KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. /* Master class for journaling. */
  19. #pragma once
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <map>
  25. #include <string>
  26. #include <queue>
  27. // #include "types.h"
  28. typedef void (progress_func)( int, void * );
  29. typedef void (snapshot_func)( void * );
  30. class Log_Entry;
  31. class Loggable;
  32. typedef Loggable *(create_func)(Log_Entry &, unsigned int id);
  33. #define LOG_REGISTER_CREATE( class ) \
  34. Loggable::register_create( #class, & class ::create );
  35. #define LOG_NAME_FUNC( class ) \
  36. virtual const char *class_name ( void ) const { return #class ; }
  37. #define LOG_CREATE_FUNC( class ) \
  38. static Loggable * \
  39. create ( Log_Entry &e, unsigned int id ) \
  40. { \
  41. class *r = new class; \
  42. r->update_id( id ); \
  43. r->set( e ); \
  44. return (Loggable *)r; \
  45. } \
  46. LOG_NAME_FUNC( class );
  47. #define LOG_NOT_LOGGABLE_FUNC( class ) \
  48. virtual const char *class_name ( void ) const { return #class ; }
  49. class Logger;
  50. class Loggable
  51. {
  52. struct log_pair {
  53. Loggable * loggable;
  54. Log_Entry * unjournaled_state;
  55. };
  56. static FILE *_fp;
  57. static unsigned int _log_id;
  58. static int _level;
  59. static off_t _undo_offset;
  60. static std::map <unsigned int, Loggable::log_pair > _loggables;
  61. static std::map <std::string, create_func*> _class_map;
  62. static std::queue <char *> _transaction;
  63. static progress_func *_progress_callback;
  64. static void *_progress_callback_arg;
  65. static snapshot_func *_snapshot_callback;
  66. static void *_snapshot_callback_arg;
  67. private:
  68. unsigned int _id;
  69. Log_Entry *_old_state;
  70. int _nest;
  71. static int _dirty; /* count of changes */
  72. static void ensure_size ( size_t n );
  73. void log_print ( const Log_Entry *o, const Log_Entry *n ) const;
  74. static void log ( const char *fmt, ... );
  75. static void flush ( void );
  76. void init ( bool loggable=true )
  77. {
  78. // _new_state
  79. _old_state = NULL;
  80. _nest = 0;
  81. if ( loggable )
  82. {
  83. _id = ++_log_id;
  84. _loggables[ _id ].loggable = this;
  85. }
  86. else
  87. _id = 0;
  88. }
  89. /* not implemented */
  90. const Loggable & operator= ( const Loggable &rhs );
  91. void record_unjournaled ( void ) const;
  92. static bool load_unjournaled_state ( void );
  93. static bool replay ( FILE *fp );
  94. public:
  95. static bool replay ( const char *name );
  96. static bool snapshot( FILE * fp );
  97. static bool snapshot( const char *name );
  98. static void snapshot_callback ( snapshot_func *p, void *arg ) { _snapshot_callback = p; _snapshot_callback_arg = arg; }
  99. static void progress_callback ( progress_func *p, void *arg ) { _progress_callback = p; _progress_callback_arg = arg;}
  100. static const char *escape ( const char *s );
  101. unsigned int id ( void ) const { return _id; }
  102. static bool save_unjournaled_state ( void );
  103. static bool open ( const char *filename );
  104. static bool close ( void );
  105. static void undo ( void );
  106. static void compact ( void );
  107. static void block_start ( void );
  108. static void block_end ( void );
  109. static Loggable * find ( unsigned int id );
  110. Loggable ( bool loggable=true )
  111. {
  112. init( loggable );
  113. }
  114. void update_id ( unsigned int id );
  115. virtual ~Loggable ( );
  116. static
  117. void
  118. register_create ( const char *name, create_func *func )
  119. {
  120. _class_map[ std::string( name ) ] = func;
  121. }
  122. /* log messages for journal */
  123. virtual void get ( Log_Entry &e ) const = 0;
  124. virtual void get_unjournaled ( Log_Entry & ) const
  125. {
  126. /* implementation optional */
  127. }
  128. virtual void set ( Log_Entry &e ) = 0;
  129. virtual const char *class_name ( void ) const = 0;
  130. virtual void log_children ( void ) const { return; }
  131. static bool do_this ( const char *s, bool reverse );
  132. static int dirty ( void ) { return _dirty; }
  133. static void clear_dirty ( void ) { _dirty = 0; }
  134. void log_create ( void ) const;
  135. protected:
  136. void log_start ( void );
  137. void log_end ( void );
  138. void log_destroy ( void ) const;
  139. /* leaf subclasses *must* call log_create() at the end of their copy contructors */
  140. Loggable ( const Loggable & )
  141. {
  142. init( true );
  143. }
  144. public:
  145. friend class Logger;
  146. };
  147. class Logger
  148. {
  149. Loggable *_this;
  150. Logger ( ) {}
  151. /* not permitted */
  152. Logger ( const Logger &rhs );
  153. const Logger & operator= ( const Logger &rhs );
  154. public:
  155. Logger ( Loggable *l ) : _this( l )
  156. {
  157. _this->log_start();
  158. }
  159. ~Logger ( )
  160. {
  161. _this->log_end();
  162. }
  163. void hold ( void )
  164. {
  165. _this->_nest++;
  166. }
  167. void release ( void )
  168. {
  169. _this->_nest--;
  170. assert( _this->_nest );
  171. }
  172. };
  173. #include "Log_Entry.H"