runtime_history.cpp

Go to the documentation of this file.
00001 #ifndef RUNTIME_HISTORY_IMPLEMENTATION_FILE
00002 #define RUNTIME_HISTORY_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : runtime_history                                                   *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1990-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "occurrence.h"
00019 #include "runtime_history.h"
00020 
00021 #include <basis/mutex.h>
00022 #include <data_struct/static_memory_gremlin.h>
00023 
00024 #undef AUTO_LOCK
00025 #define AUTO_LOCK \
00026   auto_synchronizer l(*_lock)
00027 
00028 class occurrence_list : public array<occurrence>
00029 {
00030 public:
00031   occurrence_list() : array<occurrence>() {}
00032 };
00033 
00035 
00036 runtime_history::runtime_history(int maximum_length)
00037 : _bound(maximum_length),
00038   _lock(new mutex),
00039   _occurrences(new occurrence_list)
00040 {
00041   if (_bound < 0) _bound = DEFAULT_RUNTIME_HISTORY;
00042 }
00043 
00044 runtime_history::~runtime_history()
00045 {
00046   WHACK(_occurrences);
00047   WHACK(_lock);
00048 }
00049 
00050 void runtime_history::lock() { _lock->lock(); }
00051 
00052 void runtime_history::unlock() { _lock->unlock(); }
00053 
00054 int runtime_history::max_items() const { AUTO_LOCK; return _bound; }
00055 
00056 void runtime_history::set_max_items(int new_max)
00057 {
00058   AUTO_LOCK;
00059   if (new_max < 0) _bound = DEFAULT_RUNTIME_HISTORY;
00060   _bound = new_max;
00061 }
00062 
00063 int runtime_history::elements() const
00064 { AUTO_LOCK; return _occurrences->length(); }
00065 
00066 void runtime_history::pack(byte_array &packed_form) const
00067 { AUTO_LOCK; basis::pack(packed_form, *_occurrences); }
00068 
00069 bool runtime_history::unpack(byte_array &packed_form)
00070 { AUTO_LOCK; return basis::unpack(packed_form, *_occurrences); }
00071 
00072 void runtime_history::add_outcome(const occurrence &happening)
00073 {
00074   AUTO_LOCK;
00075   if (!happening.valid()) return;  // to hell with recording that garbage.
00076   if (_occurrences->length() >= _bound)
00077     _occurrences->zap(0, 0);
00078   *_occurrences += happening;
00079 }
00080 
00081 SAFE_STATIC_CONST(occurrence, runtime_history::empty_junk_occurrence, )
00082   // a blank occurrence.
00083 
00084 const occurrence &runtime_history::get(int index)
00085 {
00086   AUTO_LOCK;
00087   bounds_return(index, 0, _occurrences->length() - 1, empty_junk_occurrence());
00088   return _occurrences->get(index);
00089 }
00090 
00091 
00092 #endif //RUNTIME_HISTORY_IMPLEMENTATION_FILE
00093 

Generated on Fri Nov 21 04:29:55 2008 for HOOPLE Libraries by  doxygen 1.5.1