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.

212 lines
4.9KB

  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 <vector>
  25. using std::vector;
  26. #include <map>
  27. using std::map;
  28. #include <string>
  29. using std::string;
  30. class Loggable;
  31. typedef Loggable *(create_func)(char **);
  32. class Logger;
  33. class Loggable
  34. {
  35. static FILE *_fp;
  36. static int _log_id;
  37. static int _level;
  38. static int _undo_index;
  39. static vector <Loggable *> _loggables;
  40. static map <string, create_func*> _class_map;
  41. private:
  42. int _id;
  43. char **_old_state;
  44. char **_new_state;
  45. int _nest;
  46. void log_print( char **o, char **n );
  47. static void log ( const char *fmt, ... );
  48. static
  49. void indent ( void )
  50. {
  51. int n = Loggable::_level;
  52. while ( n-- )
  53. log( "\t" );
  54. }
  55. public:
  56. static bool open ( const char *filename );
  57. static void undo ( void );
  58. static int undo_index ( void ) { return _undo_index; }
  59. static bool snapshot( const char *file );
  60. static
  61. void
  62. block_start ( void )
  63. {
  64. indent();
  65. // log( "{\n" );
  66. ++Loggable::_level;
  67. }
  68. static
  69. void
  70. block_end ( void )
  71. {
  72. assert( --Loggable::_level >= 0 );
  73. indent();
  74. // log( "}\n" );
  75. }
  76. static
  77. Loggable *
  78. find ( int id )
  79. {
  80. if ( id > _log_id )
  81. return NULL;
  82. return _loggables[ id - 1 ];
  83. }
  84. Loggable ( )
  85. {
  86. _id = ++_log_id;
  87. _old_state = NULL;
  88. _nest = 0;
  89. _loggables.push_back( this );
  90. }
  91. /** must be called after construction in create() methods */
  92. void
  93. update_id ( int id )
  94. {
  95. assert( _id == _log_id );
  96. _loggables[ _id - 1 ] = NULL;
  97. --_log_id;
  98. _id = id;
  99. assert( ! _loggables[ _id - 1 ] );
  100. _loggables[ _id - 1 ] = this;
  101. }
  102. virtual ~Loggable ( )
  103. {
  104. _loggables[ _id - 1 ] = NULL;
  105. }
  106. static
  107. void
  108. register_create ( const char *name, create_func *func )
  109. {
  110. printf( "registering %s to %p\n", name, func );
  111. _class_map[ string( name ) ] = func;
  112. }
  113. /* log messages for journal */
  114. virtual const char *class_name ( void ) = 0;
  115. virtual char ** get ( void ) = 0;
  116. /* this method must parse an array of name/value pair strings and make the appropriate changes too
  117. the object state */
  118. virtual void set ( char **sa ) = 0;
  119. // void log ( const char *module, const char *action, const char *fmt, ... );
  120. protected:
  121. void log_start ( void );
  122. void log_end ( void );
  123. void log_create ( void );
  124. void log_destroy ( void );
  125. public:
  126. int id ( void ) { return _id; }
  127. friend class Logger;
  128. };
  129. class Logger
  130. {
  131. Loggable *_this;
  132. Logger ( ) {}
  133. public:
  134. Logger ( Loggable *l ) : _this( l )
  135. {
  136. _this->log_start();
  137. }
  138. ~Logger ( )
  139. {
  140. _this->log_end();
  141. }
  142. void hold ( void )
  143. {
  144. printf( "hold\n" );
  145. _this->_nest++;
  146. }
  147. void release ( void )
  148. {
  149. printf( "release\n" );
  150. _this->_nest--;
  151. assert( _this->_nest );
  152. }
  153. };
  154. /* #ifndef _LOGGABLE_C */
  155. /* #define log( act, fmt, args... ) log( __CLASS__, act, fmt, ## args ) */
  156. /* #endif */
  157. /* #define LOG_START Logger _logger( this ) */
  158. /* #define LOG_END _logger.print( this ) */