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.

281 lines
7.4KB

  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. typedef void (dirty_func)( int, void * );
  31. class Log_Entry;
  32. class Loggable;
  33. typedef Loggable *(create_func)(Log_Entry &, unsigned int id);
  34. #define LOG_REGISTER_CREATE( class ) \
  35. Loggable::register_create( #class, & class ::create )
  36. #define LOG_NAME_FUNC( class ) \
  37. virtual const char *class_name ( void ) const { return #class ; }
  38. #define LOG_CREATE_FUNC( class ) \
  39. static Loggable * \
  40. create ( Log_Entry &e, unsigned int id ) \
  41. { \
  42. class *r = new class; \
  43. r->update_id( id ); \
  44. r->set( e ); \
  45. return (Loggable *)r; \
  46. } \
  47. LOG_NAME_FUNC( class )
  48. #define LOG_NOT_LOGGABLE_FUNC( class ) \
  49. virtual const char *class_name ( void ) const { return #class ; }
  50. class Logger;
  51. class Loggable
  52. {
  53. struct log_pair {
  54. Loggable * loggable;
  55. Log_Entry * unjournaled_state;
  56. };
  57. static bool _readonly;
  58. static FILE *_fp;
  59. static unsigned int _log_id;
  60. static int _level;
  61. static off_t _undo_offset;
  62. static std::map <unsigned int, Loggable::log_pair > _loggables;
  63. static std::map <std::string, create_func*> _class_map;
  64. static std::queue <char *> _transaction;
  65. static progress_func *_progress_callback;
  66. static void *_progress_callback_arg;
  67. static snapshot_func *_snapshot_callback;
  68. static void *_snapshot_callback_arg;
  69. static dirty_func *_dirty_callback;
  70. static void *_dirty_callback_arg;
  71. private:
  72. static unsigned int _relative_id;
  73. #ifndef NDEBUG
  74. static bool _snapshotting;
  75. static int _snapshot_count;
  76. mutable int _num_log_creates;
  77. mutable int _num_snapshot;
  78. mutable int _num_snapshot_creates;
  79. #endif
  80. unsigned int _id;
  81. Log_Entry *_old_state;
  82. int _nest;
  83. static int _dirty; /* count of changes */
  84. static void ensure_size ( size_t n );
  85. void log_print ( const Log_Entry *o, const Log_Entry *n ) const;
  86. static void log ( const char *fmt, ... );
  87. static void flush ( void );
  88. void init ( bool loggable=true )
  89. {
  90. // _new_state
  91. #ifndef NDEBUG
  92. _num_log_creates = 0;
  93. _num_snapshot = 0;
  94. _num_snapshot_creates = 0;
  95. #endif
  96. _old_state = NULL;
  97. _nest = 0;
  98. if ( loggable )
  99. {
  100. _id = ++_log_id;
  101. _loggables[ _id ].loggable = this;
  102. }
  103. else
  104. _id = 0;
  105. }
  106. /* not implemented */
  107. const Loggable & operator= ( const Loggable &rhs );
  108. void record_unjournaled ( void ) const;
  109. static bool load_unjournaled_state ( void );
  110. static bool replay ( FILE *fp );
  111. static void signal_dirty ( int v ) { if ( _dirty_callback ) _dirty_callback( v, _dirty_callback_arg ); }
  112. static void set_dirty ( void ) { signal_dirty( ++_dirty ); }
  113. static void clear_dirty ( void ) { signal_dirty( _dirty = 0 ); }
  114. public:
  115. static bool readonly ( void ) { return _readonly; }
  116. static bool replay ( const char *name );
  117. static bool snapshot( FILE * fp );
  118. static bool snapshot( const char *name );
  119. static void snapshot_callback ( snapshot_func *p, void *arg ) { _snapshot_callback = p; _snapshot_callback_arg = arg; }
  120. static void progress_callback ( progress_func *p, void *arg ) { _progress_callback = p; _progress_callback_arg = arg;}
  121. static void dirty_callback ( dirty_func *p, void *arg ) { _dirty_callback = p; _dirty_callback_arg = arg;}
  122. static const char *escape ( const char *s );
  123. unsigned int id ( void ) const { return _id; }
  124. static bool save_unjournaled_state ( void );
  125. static bool open ( const char *filename );
  126. static bool close ( void );
  127. static void undo ( void );
  128. static void compact ( void );
  129. static void block_start ( void );
  130. static void block_end ( void );
  131. static Loggable * find ( unsigned int id );
  132. Loggable ( bool loggable=true )
  133. {
  134. init( loggable );
  135. }
  136. void update_id ( unsigned int id );
  137. virtual ~Loggable ( );
  138. static
  139. void
  140. register_create ( const char *name, create_func *func )
  141. {
  142. _class_map[ std::string( name ) ] = func;
  143. }
  144. /* log messages for journal */
  145. virtual void get ( Log_Entry &e ) const = 0;
  146. virtual void get_unjournaled ( Log_Entry & ) const
  147. {
  148. /* implementation optional */
  149. }
  150. virtual void set ( Log_Entry &e ) = 0;
  151. virtual const char *class_name ( void ) const = 0;
  152. virtual void log_children ( void ) const { return; }
  153. static void begin_relative_id_mode ( void );
  154. static void end_relative_id_mode ( void );
  155. static bool do_this ( const char *s, bool reverse );
  156. static int dirty ( void ) { return _dirty; }
  157. void log_create ( void ) const;
  158. protected:
  159. void log_start ( void );
  160. void log_end ( void );
  161. void log_destroy ( void ) const;
  162. /* leaf subclasses *must* call log_create() at the end of their copy contructors */
  163. Loggable ( const Loggable & )
  164. {
  165. init( true );
  166. }
  167. public:
  168. friend class Logger;
  169. };
  170. class Logger
  171. {
  172. Loggable *_this;
  173. Logger ( ) {}
  174. /* not permitted */
  175. Logger ( const Logger &rhs );
  176. const Logger & operator= ( const Logger &rhs );
  177. public:
  178. Logger ( Loggable *l ) : _this( l )
  179. {
  180. _this->log_start();
  181. }
  182. ~Logger ( )
  183. {
  184. _this->log_end();
  185. }
  186. void hold ( void )
  187. {
  188. _this->_nest++;
  189. }
  190. void release ( void )
  191. {
  192. _this->_nest--;
  193. assert( _this->_nest );
  194. }
  195. };
  196. #include "Log_Entry.H"