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.2KB

  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. #include "util/debug.h"
  29. typedef void (progress_func)( int, void * );
  30. class Log_Entry;
  31. class Loggable;
  32. typedef Loggable *(create_func)(Log_Entry &);
  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 ) \
  40. { \
  41. class *r = new class; \
  42. r->set( e ); \
  43. return (Loggable *)r; \
  44. } \
  45. LOG_NAME_FUNC( class ); \
  46. #define LOG_NOT_LOGGABLE_FUNC( class ) \
  47. virtual const char *class_name ( void ) const { return #class ; } \
  48. class Logger;
  49. class Loggable
  50. {
  51. static FILE *_fp;
  52. static int _log_id;
  53. static int _level;
  54. static off_t _undo_offset;
  55. static size_t _loggables_size;
  56. static Loggable ** _loggables;
  57. static std::map <std::string, create_func*> _class_map;
  58. static std::queue <char *> _transaction;
  59. static progress_func *_progress_callback;
  60. static void *_progress_callback_arg;
  61. private:
  62. int _id;
  63. Log_Entry *_old_state;
  64. int _nest;
  65. static void ensure_size ( size_t n );
  66. void log_print( const Log_Entry *o, const Log_Entry *n ) const;
  67. static void log ( const char *fmt, ... );
  68. static void flush ( void );
  69. static bool snapshot( FILE * fp );
  70. static bool snapshot( const char *name );
  71. static bool replay ( FILE *fp );
  72. void init ( bool loggable=true )
  73. {
  74. // _new_state
  75. _old_state = NULL;
  76. _nest = 0;
  77. if ( loggable )
  78. {
  79. _id = ++_log_id;
  80. ensure_size( _id );
  81. _loggables[ _id - 1 ] = this;
  82. }
  83. else
  84. _id = 0;
  85. }
  86. /* not implemented */
  87. const Loggable & operator= ( const Loggable &rhs );
  88. static void compact_ids ( void );
  89. public:
  90. static void progress_callback ( progress_func *p, void *arg ) { _progress_callback = p; _progress_callback_arg = arg;}
  91. static const char *escape ( const char *s );
  92. int id ( void ) const { return _id; }
  93. static bool open ( const char *filename );
  94. static bool close ( void );
  95. static void undo ( void );
  96. static void compact ( void );
  97. static
  98. void
  99. block_start ( void )
  100. {
  101. ++Loggable::_level;
  102. }
  103. static
  104. void
  105. block_end ( void )
  106. {
  107. --Loggable::_level;
  108. ASSERT( Loggable::_level >= 0, "Programming error" );
  109. if ( Loggable::_level == 0 )
  110. flush();
  111. }
  112. static
  113. Loggable *
  114. find ( int id )
  115. {
  116. if ( id > _log_id )
  117. return NULL;
  118. return _loggables[ id - 1 ];
  119. }
  120. Loggable ( bool loggable=true )
  121. {
  122. init( loggable );
  123. }
  124. void update_id ( int id );
  125. virtual ~Loggable ( )
  126. {
  127. _loggables[ _id - 1 ] = NULL;
  128. }
  129. static
  130. void
  131. register_create ( const char *name, create_func *func )
  132. {
  133. _class_map[ std::string( name ) ] = func;
  134. }
  135. /* log messages for journal */
  136. virtual void get ( Log_Entry &e ) const = 0;
  137. virtual void set ( Log_Entry &e ) = 0;
  138. virtual const char *class_name ( void ) const = 0;
  139. static bool do_this ( const char *s, bool reverse );
  140. protected:
  141. void log_start ( void );
  142. void log_end ( void );
  143. void log_create ( void ) const;
  144. void log_destroy ( void ) const;
  145. /* leaf subclasses *must* call log_create() at the end of their copy contructors */
  146. Loggable ( const Loggable & )
  147. {
  148. init( true );
  149. }
  150. public:
  151. friend class Logger;
  152. };
  153. class Logger
  154. {
  155. Loggable *_this;
  156. Logger ( ) {}
  157. /* not permitted */
  158. Logger ( const Logger &rhs );
  159. const Logger & operator= ( const Logger &rhs );
  160. public:
  161. Logger ( Loggable *l ) : _this( l )
  162. {
  163. _this->log_start();
  164. }
  165. ~Logger ( )
  166. {
  167. _this->log_end();
  168. }
  169. void hold ( void )
  170. {
  171. _this->_nest++;
  172. }
  173. void release ( void )
  174. {
  175. _this->_nest--;
  176. assert( _this->_nest );
  177. }
  178. };
  179. #include "Log_Entry.H"