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.

380 lines
9.0KB

  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. #include <map>
  26. #include <string>
  27. #include <queue>
  28. using namespace std;
  29. #include "types.h"
  30. #include "debug.h"
  31. /* welcome to C++ */
  32. /* This class is just a dummy to allow base classes with null ids but
  33. * whose children are really loggable. */
  34. /* class Loggable_ID */
  35. /* { */
  36. /* public: */
  37. /* Loggable_ID ( ) { } */
  38. /* virtual ~Loggable_ID ( ) { } */
  39. /* virtual int id ( void ) const = 0; */
  40. /* virtual const char *class_name ( void ) const = 0; */
  41. /* }; */
  42. class Log_Entry;
  43. class Loggable;
  44. typedef Loggable *(create_func)(Log_Entry &);
  45. #define LOG_REGISTER_CREATE( class ) \
  46. Loggable::register_create( #class, & class ::create );
  47. #define LOG_NAME_FUNC( class ) \
  48. virtual const char *class_name ( void ) const { return #class ; }
  49. #define LOG_CREATE_FUNC( class ) \
  50. static Loggable * \
  51. create ( Log_Entry &e ) \
  52. { \
  53. class *r = new class; \
  54. r->set( e ); \
  55. return (Loggable *)r; \
  56. } \
  57. LOG_NAME_FUNC( class ); \
  58. #define LOG_NOT_LOGGABLE_FUNC( class ) \
  59. virtual const char *class_name ( void ) const { return #class ; } \
  60. class Logger;
  61. class Loggable
  62. {
  63. static FILE *_fp;
  64. static int _log_id;
  65. static int _level;
  66. static int _undo_index;
  67. static vector <Loggable *> _loggables;
  68. static map <string, create_func*> _class_map;
  69. static queue <char *> _transaction;
  70. private:
  71. int _id;
  72. char **_old_state;
  73. char **_new_state;
  74. int _nest;
  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. public:
  87. int id ( void ) const { return _id; }
  88. static bool open ( const char *filename );
  89. static bool close ( void );
  90. static void undo ( void );
  91. static int undo_index ( void ) { return _undo_index; }
  92. static void compact ( void );
  93. static
  94. void
  95. block_start ( void )
  96. {
  97. ++Loggable::_level;
  98. }
  99. static
  100. void
  101. block_end ( void )
  102. {
  103. assert( --Loggable::_level >= 0 );
  104. if ( Loggable::_level == 0 )
  105. flush();
  106. }
  107. static
  108. Loggable *
  109. find ( int id )
  110. {
  111. if ( id > _log_id )
  112. return NULL;
  113. return _loggables[ id - 1 ];
  114. }
  115. Loggable ( bool loggable=true )
  116. {
  117. if ( loggable )
  118. {
  119. _id = ++_log_id;
  120. _old_state = NULL;
  121. _nest = 0;
  122. _loggables.push_back( this );
  123. }
  124. else
  125. _id = 0;
  126. }
  127. /** must be called after construction in create() methods */
  128. void
  129. update_id ( int id )
  130. {
  131. assert( _id == _log_id );
  132. assert( _loggables[ _id - 1 ] == this );
  133. _loggables[ _id - 1 ] = NULL;
  134. // --_log_id;
  135. _id = id;
  136. /* make sure it'll fit */
  137. _loggables.reserve( _id );
  138. // assert( ! _loggables[ _id - 1 ] );
  139. ASSERT( ! _loggables[ _id - 1 ], "Attempt to create object with an ID (0x%X) that already exists. The existing object is of type \"%s\", the new one is \"%s\". Corrupt journal?", _id, _loggables[ _id -1 ]->class_name(), class_name() );
  140. _loggables[ _id - 1 ] = this;
  141. }
  142. virtual ~Loggable ( )
  143. {
  144. _loggables[ _id - 1 ] = NULL;
  145. }
  146. static
  147. void
  148. register_create ( const char *name, create_func *func )
  149. {
  150. // printf( "registering %s to %p\n", name, func );
  151. _class_map[ string( name ) ] = func;
  152. }
  153. /* log messages for journal */
  154. virtual void get ( Log_Entry &e ) const = 0;
  155. virtual void set ( Log_Entry &e ) = 0;
  156. virtual const char *class_name ( void ) const = 0;
  157. static bool do_this ( const char *s, bool reverse );
  158. protected:
  159. void log_start ( void );
  160. void log_end ( void );
  161. void log_create ( void ) const;
  162. void log_destroy ( void ) const;
  163. public:
  164. // virtual const char *class_name ( void ) const = 0;
  165. friend class Logger;
  166. };
  167. class Logger
  168. {
  169. Loggable *_this;
  170. Logger ( ) {}
  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. class Log_Entry
  193. {
  194. // vector <Pair> _sa;
  195. char **_sa;
  196. int _i;
  197. public:
  198. struct Pair
  199. {
  200. const char *name;
  201. const char *value;
  202. };
  203. Log_Entry ( )
  204. {
  205. _sa = (char**)malloc( sizeof( char * ) );
  206. *_sa = NULL;
  207. _i = 0;
  208. }
  209. Log_Entry ( char **sa )
  210. {
  211. _sa = sa;
  212. _i = 0;
  213. if ( _sa )
  214. while ( _sa[ _i ] ) ++_i;
  215. }
  216. ~Log_Entry ( )
  217. {
  218. if ( ! _sa )
  219. return;
  220. for ( _i = 0; _sa[ _i ]; ++_i )
  221. {
  222. free( _sa[ _i ] );
  223. }
  224. free( _sa );
  225. }
  226. /****************/
  227. /* Construction */
  228. /****************/
  229. void grow ( )
  230. {
  231. _sa = (char**)realloc( _sa, sizeof( char * ) * (_i + 2) );
  232. _sa[ _i + 1 ] = NULL;
  233. }
  234. #define ADD( type, format, exp ) \
  235. void add ( const char *name, type v ) \
  236. { \
  237. grow(); \
  238. asprintf( &_sa[ _i ], "%s " format, name, (exp) ); \
  239. strtok( _sa[ _i++ ], " " ); \
  240. } \
  241. /***************/
  242. /* Examination */
  243. /***************/
  244. int size ( void ) const
  245. {
  246. return _i;
  247. }
  248. void get ( int n, const char **name, const char **value )
  249. {
  250. *name = _sa[ n ];
  251. *value = *name + strlen( *name ) + 1;
  252. }
  253. char **sa ( void )
  254. {
  255. char **sa = _sa;
  256. _sa = NULL;
  257. return sa;
  258. }
  259. /* #define ADD ( type, format, exp ) \ */
  260. /* void add ( const char *name, type v ) \ */
  261. /* { \ */
  262. /* char pat[ 256 ]; \ */
  263. /* Pair p; \ */
  264. /* p.name = strdup( name ); \ */
  265. /* snprintf( pat, sizeof( pat ), format, exp ); \ */
  266. /* p.value = strdup( pat ); \ */
  267. /* _sa.push( p ); \ */
  268. /* } \ */
  269. ADD( int, "%d", v );
  270. ADD( nframes_t, "%lu", (unsigned long)v );
  271. ADD( unsigned long, "%lu", v );
  272. ADD( const char *, "\"%s\"", v ? v : "" );
  273. ADD( Loggable * , "0x%X", v ? v->id() : 0 );
  274. ADD( float, "%f", v );
  275. ADD( double, "%f", v );
  276. #undef ADD
  277. };