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.

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