git-svn-id: https://svn.microneil.com/svn/CodeDweller/branches/adeniz_1@92 d34b734f-a00e-4b39-a726-e4eeb87269abadeniz_1
| #include <iostream> | #include <iostream> | ||||
| #include <string> | #include <string> | ||||
| using namespace std; | |||||
| namespace CodeDweller { | |||||
| const int DefaultExitCode = EXIT_FAILURE; // Use this when no code is provided. | const int DefaultExitCode = EXIT_FAILURE; // Use this when no code is provided. | ||||
| private: | private: | ||||
| const string myDescription; // This is what I have to say. | |||||
| const std::string myDescription; // This is what I have to say. | |||||
| public: | public: | ||||
| AbortCheck(const string& Text) : myDescription(Text) {} // I am constructed with a description | |||||
| AbortCheck(const std::string& Text) : myDescription(Text) {} // I am constructed with a description | |||||
| void operator()(bool X) const { // Apply me like assert(exp) | void operator()(bool X) const { // Apply me like assert(exp) | ||||
| if(false == X) { // If the expression is false then we | if(false == X) { // If the expression is false then we | ||||
| cerr << myDescription << endl; // failed the check so we display our | |||||
| std::cerr << myDescription << std::endl; // failed the check so we display our | |||||
| abort(); // description and abort. | abort(); // description and abort. | ||||
| } | } | ||||
| } | } | ||||
| const string Description() { return myDescription; } // You can ask for my Description. | |||||
| const std::string Description() { return myDescription; } // You can ask for my Description. | |||||
| }; | }; | ||||
| class AbortFault { // If this fault occurs we will abort. | class AbortFault { // If this fault occurs we will abort. | ||||
| private: | private: | ||||
| const string myDescription; // This is what I have to say. | |||||
| const std::string myDescription; // This is what I have to say. | |||||
| public: | public: | ||||
| AbortFault(const string& Text) : myDescription(Text) {} // I am constructed with a description | |||||
| AbortFault(const std::string& Text) : myDescription(Text) {} // I am constructed with a description | |||||
| void operator()(bool X) const { // Apply me like assert(! exp) | void operator()(bool X) const { // Apply me like assert(! exp) | ||||
| if(true == X) { // If the expression is true then we | if(true == X) { // If the expression is true then we | ||||
| cerr << myDescription << endl; // have a fault so we display our fault | |||||
| std::cerr << myDescription << std::endl; // have a fault so we display our fault | |||||
| abort(); // description and abort. | abort(); // description and abort. | ||||
| } | } | ||||
| } | } | ||||
| const string Description() const { return myDescription; } // You can ask for my Description. | |||||
| const std::string Description() const { return myDescription; } // You can ask for my Description. | |||||
| }; | }; | ||||
| class ExitCheck { // If this check is false we will exit. | class ExitCheck { // If this check is false we will exit. | ||||
| private: | private: | ||||
| const string myDescription; // This is what I have to say. | |||||
| const std::string myDescription; // This is what I have to say. | |||||
| const int myExitCode; // This is what I send to exit(). | const int myExitCode; // This is what I send to exit(). | ||||
| public: | public: | ||||
| ExitCheck(const string& Text, int Code=DefaultExitCode) : // I am constructed with a description | |||||
| ExitCheck(const std::string& Text, int Code=DefaultExitCode) : // I am constructed with a description | |||||
| myDescription(Text), myExitCode(Code) {} // and (optionlly) an exit code. | myDescription(Text), myExitCode(Code) {} // and (optionlly) an exit code. | ||||
| void operator()(bool X) const { // Apply me like assert(exp) | void operator()(bool X) const { // Apply me like assert(exp) | ||||
| if(false == X) { // If the expression is false then we | if(false == X) { // If the expression is false then we | ||||
| cerr << myDescription << endl; // failed the check so we display our | |||||
| std::cerr << myDescription << std::endl; // failed the check so we display our | |||||
| exit(myExitCode); // description and exit with our code. | exit(myExitCode); // description and exit with our code. | ||||
| } | } | ||||
| } | } | ||||
| const string Description() { return myDescription; } // You can ask for my Description. | |||||
| const std::string Description() { return myDescription; } // You can ask for my Description. | |||||
| const int ExitCode() { return myExitCode; } // You can ask for my ExitCode. | const int ExitCode() { return myExitCode; } // You can ask for my ExitCode. | ||||
| }; | }; | ||||
| private: | private: | ||||
| const string myDescription; // This is what I have to say. | |||||
| const std::string myDescription; // This is what I have to say. | |||||
| const int myExitCode; // This is what I send to exit(). | const int myExitCode; // This is what I send to exit(). | ||||
| public: | public: | ||||
| ExitFault(const string& Text, int Code=DefaultExitCode) : // I am constructed with a description | |||||
| ExitFault(const std::string& Text, int Code=DefaultExitCode) : // I am constructed with a description | |||||
| myDescription(Text), myExitCode(Code) {} // and (optionlly) an exit code. | myDescription(Text), myExitCode(Code) {} // and (optionlly) an exit code. | ||||
| void operator()(bool X) const { // Apply me like assert(! exp) | void operator()(bool X) const { // Apply me like assert(! exp) | ||||
| if(true == X) { // If the expression is true then we | if(true == X) { // If the expression is true then we | ||||
| cerr << myDescription << endl; // have a fault so we display our fault | |||||
| std::cerr << myDescription << std::endl; // have a fault so we display our fault | |||||
| exit(myExitCode); // description and exit with our code. | exit(myExitCode); // description and exit with our code. | ||||
| } | } | ||||
| } | } | ||||
| const string Description() const { return myDescription; } // You can ask for my Description. | |||||
| const std::string Description() const { return myDescription; } // You can ask for my Description. | |||||
| const int ExitCode() const { return myExitCode; } // You can ask for my ExitCode. | const int ExitCode() const { return myExitCode; } // You can ask for my ExitCode. | ||||
| }; | }; | ||||
| class RuntimeCheck : public runtime_error { // Throw if this check fails. | |||||
| class RuntimeCheck : public std::runtime_error { // Throw if this check fails. | |||||
| public: | public: | ||||
| RuntimeCheck(const string& Text) : runtime_error(Text) {} // Construct me with a description. | |||||
| RuntimeCheck(const std::string& Text) : std::runtime_error(Text) {} // Construct me with a description. | |||||
| void operator()(bool X) const { // Apply me like assert(exp) | void operator()(bool X) const { // Apply me like assert(exp) | ||||
| if(false == X) { // If the expression is false then we | if(false == X) { // If the expression is false then we | ||||
| } | } | ||||
| }; | }; | ||||
| class RuntimeFault : public runtime_error { // Throw if we find this fault. | |||||
| class RuntimeFault : public std::runtime_error { // Throw if we find this fault. | |||||
| public: | public: | ||||
| RuntimeFault(const string& Text) : runtime_error(Text) {} // Construct me with a description. | |||||
| RuntimeFault(const std::string& Text) : std::runtime_error(Text) {} // Construct me with a description. | |||||
| void operator()(bool X) const { // Apply me like assert(exp) | void operator()(bool X) const { // Apply me like assert(exp) | ||||
| if(true == X) { // If the expression is true then we | if(true == X) { // If the expression is true then we | ||||
| } | } | ||||
| }; | }; | ||||
| class LogicCheck : public logic_error { // Throw if this check fails. | |||||
| class LogicCheck : public std::logic_error { // Throw if this check fails. | |||||
| public: | public: | ||||
| LogicCheck(const string& Text) : logic_error(Text) {} // Construct me with a description. | |||||
| LogicCheck(const std::string& Text) : std::logic_error(Text) {} // Construct me with a description. | |||||
| void operator()(bool X) const { // Apply me like assert(exp) | void operator()(bool X) const { // Apply me like assert(exp) | ||||
| if(false == X) { // If the expression is false then we | if(false == X) { // If the expression is false then we | ||||
| } | } | ||||
| }; | }; | ||||
| class LogicFault : public logic_error { // Throw if we find this fault. | |||||
| class LogicFault : public std::logic_error { // Throw if we find this fault. | |||||
| public: | public: | ||||
| LogicFault(const string& Text) : logic_error(Text) {} // Construct me with a description. | |||||
| LogicFault(const std::string& Text) : std::logic_error(Text) {} // Construct me with a description. | |||||
| void operator()(bool X) const { // Apply me like assert(exp) | void operator()(bool X) const { // Apply me like assert(exp) | ||||
| if(true == X) { // If the expression is true then we | if(true == X) { // If the expression is true then we | ||||
| } | } | ||||
| }; | }; | ||||
| } // namespace CodeDweller. | |||||
| #endif | #endif | ||||
| // End Of Include MNR_faults Once Only ========================================= | // End Of Include MNR_faults Once Only ========================================= |
| #include <set> | #include <set> | ||||
| using namespace std; | |||||
| /** The Histogram class is managed set of HistogramRecords. | /** The Histogram class is managed set of HistogramRecords. | ||||
| *** We play some naughty tricks with pointers to break the rules and | *** We play some naughty tricks with pointers to break the rules and | ||||
| *** directly manipulate the counts of HistogramRecords stored in the | *** directly manipulate the counts of HistogramRecords stored in the | ||||
| *** ordered by key. | *** ordered by key. | ||||
| **/ | **/ | ||||
| namespace CodeDweller { | |||||
| class HistogramRecord { // A record to assocate a key and count. | class HistogramRecord { // A record to assocate a key and count. | ||||
| public: | public: | ||||
| int Key; // Here is the key. | int Key; // Here is the key. | ||||
| } | } | ||||
| }; | }; | ||||
| class Histogram : public set<HistogramRecord> { // A Histogram is a set of HistogramRecords | |||||
| class Histogram : public std::set<HistogramRecord> { // A Histogram is a set of HistogramRecords | |||||
| private: // and a private hit counter... | private: // and a private hit counter... | ||||
| int HitCount; | int HitCount; | ||||
| public: | public: | ||||
| int hit(const int EventKey, const int Adjustment = 1) { // hit() method increments a specific count. | int hit(const int EventKey, const int Adjustment = 1) { // hit() method increments a specific count. | ||||
| HistogramRecord E(EventKey); // First, make a record for the event key. | HistogramRecord E(EventKey); // First, make a record for the event key. | ||||
| insert(E); // Insert the new record (if it's not there). | insert(E); // Insert the new record (if it's not there). | ||||
| set<HistogramRecord>::iterator iE = // Find either the pre-existing or the new | |||||
| std::set<HistogramRecord>::iterator iE = // Find either the pre-existing or the new | |||||
| find(E); // record for this key. | find(E); // record for this key. | ||||
| int* C; // Play naughty pointer games to access | int* C; // Play naughty pointer games to access | ||||
| C = const_cast<int*>(&((*iE).Count)); // the Count for this record inside the | C = const_cast<int*>(&((*iE).Count)); // the Count for this record inside the | ||||
| } | } | ||||
| }; | }; | ||||
| } // namespace CodeDweller. | |||||
| #endif | #endif | ||||
| // Using new optimized chaos driver for uniformity experiments. | // Using new optimized chaos driver for uniformity experiments. | ||||
| // Important in this experiment is proof of highest possible entropy. | // Important in this experiment is proof of highest possible entropy. | ||||
| #include "mangler.hpp" | |||||
| #include "CodeDweller/mangler.hpp" | |||||
| namespace CodeDweller { | |||||
| unsigned char MANGLER::ChaosDriver(void) { // Return the current | unsigned char MANGLER::ChaosDriver(void) { // Return the current | ||||
| return Fill[Fill[Position]^Fill[Position^0xff]]; // chaos engine output | return Fill[Fill[Position]^Fill[Position^0xff]]; // chaos engine output | ||||
| Fill[c]=(unsigned char) c; // value and Position to 0. | Fill[c]=(unsigned char) c; // value and Position to 0. | ||||
| } | } | ||||
| } |
| #ifndef _MANGLER_ | #ifndef _MANGLER_ | ||||
| #define _MANGLER_ | #define _MANGLER_ | ||||
| namespace CodeDweller { | |||||
| class MANGLER { | class MANGLER { | ||||
| private: | private: | ||||
| MANGLER(void); // Default. | MANGLER(void); // Default. | ||||
| }; | }; | ||||
| } | |||||
| #endif | #endif | ||||
| #include "onetimepad.hpp" | #include "onetimepad.hpp" | ||||
| #include "timing.hpp" | #include "timing.hpp" | ||||
| namespace CodeDweller { | |||||
| /* | /* | ||||
| class OneTimePad { // One Time Pad generator. | class OneTimePad { // One Time Pad generator. | ||||
| private: | private: | ||||
| } // initial Mangler state. | } // initial Mangler state. | ||||
| } // The OneTimePad object is ready. | } // The OneTimePad object is ready. | ||||
| } |
| #include <vector> | #include <vector> | ||||
| #include "mangler.hpp" | #include "mangler.hpp" | ||||
| using namespace std; | |||||
| namespace CodeDweller { | |||||
| typedef vector<unsigned char> PadBuffer; | |||||
| typedef std::vector<unsigned char> PadBuffer; | |||||
| class OneTimePad { // One Time Pad generator. | class OneTimePad { // One Time Pad generator. | ||||
| private: | private: | ||||
| }; | }; | ||||
| } | |||||
| #endif | #endif |
| class ThreadState { // Thread State Object. | class ThreadState { // Thread State Object. | ||||
| public: | public: | ||||
| const string Name; // Text name of thread descriptor. | |||||
| ThreadState(string N) : Name(N) {} // Constructor requires text name. | |||||
| const std::string Name; // Text name of thread descriptor. | |||||
| ThreadState(std::string N) : Name(N) {} // Constructor requires text name. | |||||
| }; | }; | ||||
| // ThreadType objects are constant static objects defined for each Thread class | // ThreadType objects are constant static objects defined for each Thread class | ||||
| class ThreadType { | class ThreadType { | ||||
| public: | public: | ||||
| const string Name; | |||||
| ThreadType(string N) : Name(N) {} | |||||
| const std::string Name; | |||||
| ThreadType(std::string N) : Name(N) {} | |||||
| }; | }; | ||||
| class Thread; // There is such thing as a Thread. | class Thread; // There is such thing as a Thread. | ||||
| Thread* Pointer; // A pointer to the thread. | Thread* Pointer; // A pointer to the thread. | ||||
| ThreadType* Type; // A descriptor of it's type. | ThreadType* Type; // A descriptor of it's type. | ||||
| ThreadState* State; // A descriptor of it's state. | ThreadState* State; // A descriptor of it's state. | ||||
| string Name; // Name of the thread if any. | |||||
| std::string Name; // Name of the thread if any. | |||||
| bool isRunning; // True if the thread is running. | bool isRunning; // True if the thread is running. | ||||
| bool isBad; // True if the thread is bad. | bool isBad; // True if the thread is bad. | ||||
| string Fault; // Bad Thread's Fault if any. | |||||
| std::string Fault; // Bad Thread's Fault if any. | |||||
| public: | public: | ||||
| ThreadStatusRecord( // Initialize all items. | ThreadStatusRecord( // Initialize all items. | ||||
| ThreadState& S, | ThreadState& S, | ||||
| bool R, | bool R, | ||||
| bool B, | bool B, | ||||
| string F, | |||||
| string N | |||||
| std::string F, | |||||
| std::string N | |||||
| ) : | ) : | ||||
| Pointer(P), | Pointer(P), | ||||
| Type(&T), | Type(&T), | ||||
| const ThreadState& getState() { return *State; } | const ThreadState& getState() { return *State; } | ||||
| bool getRunning() { return isRunning; } | bool getRunning() { return isRunning; } | ||||
| bool getBad() { return isBad; } | bool getBad() { return isBad; } | ||||
| string getFault() { return Fault; } | |||||
| string getName() { return Name; } | |||||
| std::string getFault() { return Fault; } | |||||
| std::string getName() { return Name; } | |||||
| }; | }; | ||||
| typedef vector<ThreadStatusRecord> ThreadStatusReport; // Status report type. | |||||
| typedef std::vector<ThreadStatusRecord> ThreadStatusReport; // Status report type. | |||||
| // End ThreadDescriptor | // End ThreadDescriptor | ||||
| //////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////// | ||||
| protected: | protected: | ||||
| const ThreadType& MyThreadType; // Identify thread type. | const ThreadType& MyThreadType; // Identify thread type. | ||||
| const string MyThreadName; // Name string of this instance. | |||||
| const std::string MyThreadName; // Name string of this instance. | |||||
| thread_primative MyThread; // Abstracted thread. | thread_primative MyThread; // Abstracted thread. | ||||
| bool RunningFlag; // True when thread is in myTask() | bool RunningFlag; // True when thread is in myTask() | ||||
| bool BadFlag; // True when myTask() throws! | bool BadFlag; // True when myTask() throws! | ||||
| string BadWhat; // Bad exception what() if any. | |||||
| std::string BadWhat; // Bad exception what() if any. | |||||
| void CurrentThreadState(const ThreadState& TS); // Set thread state. | void CurrentThreadState(const ThreadState& TS); // Set thread state. | ||||
| public: | public: | ||||
| Thread(); // Constructor (just in case) | Thread(); // Constructor (just in case) | ||||
| Thread(const ThreadType& T, string N); // Construct with specific Type/Name | |||||
| Thread(const ThreadType& T, std::string N); // Construct with specific Type/Name | |||||
| virtual ~Thread(); // Destructor (just in case) | virtual ~Thread(); // Destructor (just in case) | ||||
| void run(); // Method to launch this thread. | void run(); // Method to launch this thread. | ||||
| bool isRunning(); // Return the Running flag state. | bool isRunning(); // Return the Running flag state. | ||||
| bool isBad(); // Return the Bad flag state. | bool isBad(); // Return the Bad flag state. | ||||
| const string MyFault(); // Return exception Bad fault if any. | |||||
| const std::string MyFault(); // Return exception Bad fault if any. | |||||
| const string MyName() const; // The thread's name. | |||||
| const std::string MyName() const; // The thread's name. | |||||
| const ThreadType& MyType(); // Thread type for this thread. | const ThreadType& MyType(); // Thread type for this thread. | ||||
| const ThreadState& MyState(); // Returns the current thread state. | const ThreadState& MyState(); // Returns the current thread state. | ||||
| const ThreadState& CurrentThreadState(); // Returns the current thread state. | const ThreadState& CurrentThreadState(); // Returns the current thread state. | ||||
| private: | private: | ||||
| Mutex MyMutex; // Protect our data with this. | Mutex MyMutex; // Protect our data with this. | ||||
| set<Thread*> KnownThreads; // Keep track of all threads. | |||||
| std::set<Thread*> KnownThreads; // Keep track of all threads. | |||||
| void rememberThread(Thread* T); // Threads register themselves. | void rememberThread(Thread* T); // Threads register themselves. | ||||
| void forgetThread(Thread* T); // Threads remove themselves. | void forgetThread(Thread* T); // Threads remove themselves. | ||||
| Mutex myMutex; // Contains a mutex and | Mutex myMutex; // Contains a mutex and | ||||
| volatile unsigned int LatestSize; // a volatile (blinking light) size | volatile unsigned int LatestSize; // a volatile (blinking light) size | ||||
| ProductionGateway myGateway; // integrated with a production | ProductionGateway myGateway; // integrated with a production | ||||
| queue<T> myQueue; // gateway and a queue. | |||||
| std::queue<T> myQueue; // gateway and a queue. | |||||
| public: | public: | ||||
| ProductionQueue() : LatestSize(0) {} // The size always starts at zero. | ProductionQueue() : LatestSize(0) {} // The size always starts at zero. |