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.

254 lines
6.6KB

  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. static unsigned int _relative_id;
  69. unsigned int _id;
  70. Log_Entry *_old_state;
  71. int _nest;
  72. static int _dirty; /* count of changes */
  73. static void ensure_size ( size_t n );
  74. void log_print ( const Log_Entry *o, const Log_Entry *n ) const;
  75. static void log ( const char *fmt, ... );
  76. static void flush ( void );
  77. void init ( bool loggable=true )
  78. {
  79. // _new_state
  80. _old_state = NULL;
  81. _nest = 0;
  82. if ( loggable )
  83. {
  84. _id = ++_log_id;
  85. _loggables[ _id ].loggable = this;
  86. }
  87. else
  88. _id = 0;
  89. }
  90. /* not implemented */
  91. const Loggable & operator= ( const Loggable &rhs );
  92. void record_unjournaled ( void ) const;
  93. static bool load_unjournaled_state ( void );
  94. static bool replay ( FILE *fp );
  95. public:
  96. static bool replay ( const char *name );
  97. static bool snapshot( FILE * fp );
  98. static bool snapshot( const char *name );
  99. static void snapshot_callback ( snapshot_func *p, void *arg ) { _snapshot_callback = p; _snapshot_callback_arg = arg; }
  100. static void progress_callback ( progress_func *p, void *arg ) { _progress_callback = p; _progress_callback_arg = arg;}
  101. static const char *escape ( const char *s );
  102. unsigned int id ( void ) const { return _id; }
  103. static bool save_unjournaled_state ( void );
  104. static bool open ( const char *filename );
  105. static bool close ( void );
  106. static void undo ( void );
  107. static void compact ( void );
  108. static void block_start ( void );
  109. static void block_end ( void );
  110. static Loggable * find ( unsigned int id );
  111. Loggable ( bool loggable=true )
  112. {
  113. init( loggable );
  114. }
  115. void update_id ( unsigned int id );
  116. virtual ~Loggable ( );
  117. static
  118. void
  119. register_create ( const char *name, create_func *func )
  120. {
  121. _class_map[ std::string( name ) ] = func;
  122. }
  123. /* log messages for journal */
  124. virtual void get ( Log_Entry &e ) const = 0;
  125. virtual void get_unjournaled ( Log_Entry & ) const
  126. {
  127. /* implementation optional */
  128. }
  129. virtual void set ( Log_Entry &e ) = 0;
  130. virtual const char *class_name ( void ) const = 0;
  131. virtual void log_children ( void ) const { return; }
  132. static void begin_relative_id_mode ( void );
  133. static void end_relative_id_mode ( void );
  134. static bool do_this ( const char *s, bool reverse );
  135. static int dirty ( void ) { return _dirty; }
  136. static void clear_dirty ( void ) { _dirty = 0; }
  137. void log_create ( void ) const;
  138. protected:
  139. void log_start ( void );
  140. void log_end ( void );
  141. void log_destroy ( void ) const;
  142. /* leaf subclasses *must* call log_create() at the end of their copy contructors */
  143. Loggable ( const Loggable & )
  144. {
  145. init( true );
  146. }
  147. public:
  148. friend class Logger;
  149. };
  150. class Logger
  151. {
  152. Loggable *_this;
  153. Logger ( ) {}
  154. /* not permitted */
  155. Logger ( const Logger &rhs );
  156. const Logger & operator= ( const Logger &rhs );
  157. public:
  158. Logger ( Loggable *l ) : _this( l )
  159. {
  160. _this->log_start();
  161. }
  162. ~Logger ( )
  163. {
  164. _this->log_end();
  165. }
  166. void hold ( void )
  167. {
  168. _this->_nest++;
  169. }
  170. void release ( void )
  171. {
  172. _this->_nest--;
  173. assert( _this->_nest );
  174. }
  175. };
  176. #include "Log_Entry.H"