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.

228 lines
5.8KB

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