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.

275 lines
6.7KB

  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( char **o, char **n ) const;
  76. static void log ( const char *fmt, ... );
  77. static void flush ( void );
  78. static
  79. void indent ( void )
  80. {
  81. int n = Loggable::_level;
  82. while ( n-- )
  83. log( "\t" );
  84. }
  85. static bool snapshot( FILE * fp );
  86. void init ( bool loggable=true )
  87. {
  88. // _new_state
  89. _old_state = NULL;
  90. _nest = 0;
  91. if ( loggable )
  92. {
  93. _id = ++_log_id;
  94. ensure_size( _id );
  95. _loggables[ _id - 1 ] = this;
  96. }
  97. else
  98. _id = 0;
  99. }
  100. /* not implemented */
  101. const Loggable & operator= ( const Loggable &rhs );
  102. public:
  103. static const char *escape ( const char *s );
  104. int id ( void ) const { return _id; }
  105. static bool open ( const char *filename );
  106. static bool close ( void );
  107. static void undo ( void );
  108. static int undo_index ( void ) { return _undo_index; }
  109. static void compact ( void );
  110. static
  111. void
  112. block_start ( void )
  113. {
  114. ++Loggable::_level;
  115. }
  116. static
  117. void
  118. block_end ( void )
  119. {
  120. assert( --Loggable::_level >= 0 );
  121. if ( Loggable::_level == 0 )
  122. flush();
  123. }
  124. static
  125. Loggable *
  126. find ( int id )
  127. {
  128. if ( id > _log_id )
  129. return NULL;
  130. return _loggables[ id - 1 ];
  131. }
  132. Loggable ( bool loggable=true )
  133. {
  134. init( loggable );
  135. }
  136. void update_id ( int id );
  137. virtual ~Loggable ( )
  138. {
  139. _loggables[ _id - 1 ] = NULL;
  140. }
  141. static
  142. void
  143. register_create ( const char *name, create_func *func )
  144. {
  145. // printf( "registering %s to %p\n", name, func );
  146. _class_map[ std::string( name ) ] = func;
  147. }
  148. /* log messages for journal */
  149. virtual void get ( Log_Entry &e ) const = 0;
  150. virtual void set ( Log_Entry &e ) = 0;
  151. virtual const char *class_name ( void ) const = 0;
  152. static bool do_this ( const char *s, bool reverse );
  153. protected:
  154. void log_start ( void );
  155. void log_end ( void );
  156. void log_create ( void ) const;
  157. void log_destroy ( void ) const;
  158. /* leaf subclasses *must* call log_create() at the end of their copy contructors */
  159. Loggable ( const Loggable & )
  160. {
  161. init( true );
  162. /* FIXME: get a real id here!!! */
  163. // _id = 0;
  164. }
  165. public:
  166. // virtual const char *class_name ( void ) const = 0;
  167. friend class Logger;
  168. };
  169. class Logger
  170. {
  171. Loggable *_this;
  172. Logger ( ) {}
  173. /* not permitted */
  174. Logger ( const Logger &rhs );
  175. const Logger & operator= ( const Logger &rhs );
  176. public:
  177. Logger ( Loggable *l ) : _this( l )
  178. {
  179. _this->log_start();
  180. }
  181. ~Logger ( )
  182. {
  183. _this->log_end();
  184. }
  185. void hold ( void )
  186. {
  187. printf( "hold\n" );
  188. _this->_nest++;
  189. }
  190. void release ( void )
  191. {
  192. printf( "release\n" );
  193. _this->_nest--;
  194. assert( _this->_nest );
  195. }
  196. };
  197. #include "Log_Entry.H"