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.

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