From: cherierm Date: Thu, 3 Jul 2008 16:58:25 +0000 (+0000) Subject: Some new classes of CPP version of Msg X-Git-Tag: v3.3~253 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/2371d0c1060589bd7b8757f47974ceaa906e0dba Some new classes of CPP version of Msg git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5854 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/src/cxx/ClassNotFoundException.cxx b/src/cxx/ClassNotFoundException.cxx new file mode 100644 index 0000000000..e61bf539e1 --- /dev/null +++ b/src/cxx/ClassNotFoundException.cxx @@ -0,0 +1,60 @@ +#include + +#include +#include +#include + +namespace SimGrid +{ + namespace Msg + { + + ClassNotFoundException::ClassNotFoundException() + { + this->reason = (char*) calloc(strlen("Class not found : unknown") + 1, sizeof(char)); + strcpy(this->reason, "Class not found : unknown"); + } + + + ClassNotFoundException::ClassNotFoundException(const ClassNotFoundException& rClassNotFoundException) + { + const char* reason = rClassNotFoundException.toString(); + this->reason = (char*) calloc(strlen(reason) + 1, sizeof(char)); + strcpy(this->reason, reason); + + } + + + ClassNotFoundException::ClassNotFoundException(const char* name) + { + this->reason = (char*) calloc(strlen("Class not found : ") + strlen(name) + 1, sizeof(char)); + sprintf(this->reason, "Class not found : %s", name); + } + + + ClassNotFoundException::~ClassNotFoundException() + { + if(this->reason) + free(this->reason); + } + + const char* ClassNotFoundException::toString(void) const + { + return (const char*)(this->reason); + } + + + const ClassNotFoundException& ClassNotFoundException::operator = (const ClassNotFoundException& rClassNotFoundException) + { + const char* reason = rClassNotFoundException.toString(); + this->reason = (char*) calloc(strlen(reason) + 1, sizeof(char)); + + return *this; + } + + } // namespace Msg + +}// namespace SimGrid + + + diff --git a/src/cxx/ClassNotFoundException.hpp b/src/cxx/ClassNotFoundException.hpp new file mode 100644 index 0000000000..84b4317b09 --- /dev/null +++ b/src/cxx/ClassNotFoundException.hpp @@ -0,0 +1,64 @@ +/* + * ClassNotFoundException.hpp + * + * Copyright 2006,2007 Martin Quinson, Malek Cherier + * All right reserved. + * + * This program is free software; you can redistribute + * it and/or modify it under the terms of the license + *(GNU LGPL) which comes with this package. + * + */ + +#ifndef MSG_CLASSNOTFOUNDEXCEPTION_HPP +#define MSG_CLASSNOTFOUNDEXCEPTION_HPP + +#include + +namespace SimGrid +{ + namespace Msg + { + + class SIMGRIDX_EXPORT ClassNotFoundException : public Exception + { + public: + + // Default constructor. + ClassNotFoundException(); + + // Copy constructor. + ClassNotFoundException(const ClassNotFoundException& rClassNotFoundException); + + // This constructor takes the name of the class not found. + ClassNotFoundException(const char* name); + + // Destructor. + virtual ~ClassNotFoundException(); + + // Operations. + + // Returns the reason of the exception : + // the message "Host not found `host name'" + const char* toString(void) const; + + // Operators. + + // Assignement. + const ClassNotFoundException& operator = (const ClassNotFoundException& rClassNotFoundException); + + private : + + // Attributes. + + // A buffer used to build the message returned by the methode toString(). + char* reason; + }; + + + } // namespace Msg + +}// namespace SimGrid + + +#endif // !MSG_CLASSNOTFOUNDEXCEPTION_HPP diff --git a/src/cxx/Config.hpp b/src/cxx/Config.hpp new file mode 100644 index 0000000000..837946e59d --- /dev/null +++ b/src/cxx/Config.hpp @@ -0,0 +1,20 @@ +#ifndef MSG_CONFIG_HPP +#define MSG_CONFIG_HPP + +namespace SimGrid +{ + namespace Msg + { + #if defined(WIN32) + #if defined(SIMGRIDX_EXPORTS) + #define SIMGRIDX_EXPORT __declspec(dllexport) + #else + #define SIMGRIDX_EXPORT + #endif + #else + #define SIMGRIDX_EXPORT + #endif + } // namespace Msg +} // namespace SimGrid + +#endif // MSG_CONFIG_HPP \ No newline at end of file diff --git a/src/cxx/NullPointerException.cxx b/src/cxx/NullPointerException.cxx new file mode 100644 index 0000000000..e672c66914 --- /dev/null +++ b/src/cxx/NullPointerException.cxx @@ -0,0 +1,60 @@ +#include + +#include +#include +#include + +namespace SimGrid +{ + namespace Msg + { + + NullPointerException::NullPointerException() + { + this->reason = (char*) calloc(strlen("Null pointer : unknown") + 1, sizeof(char)); + strcpy(this->reason, "Null pointer : unknown"); + } + + + NullPointerException::NullPointerException(const NullPointerException& rNullPointerException) + { + const char* reason = rNullPointerException.toString(); + this->reason = (char*) calloc(strlen(reason) + 1, sizeof(char)); + strcpy(this->reason, reason); + + } + + + NullPointerException::NullPointerException(const char* name) + { + this->reason = (char*) calloc(strlen("Null pointer : ") + strlen(name) + 1, sizeof(char)); + sprintf(this->reason, "Null pointer : %s", name); + } + + + NullPointerException::~NullPointerException() + { + if(this->reason) + free(this->reason); + } + + const char* NullPointerException::toString(void) const + { + return (const char*)(this->reason); + } + + + const NullPointerException& NullPointerException::operator = (const NullPointerException& rNullPointerException) + { + const char* reason = rNullPointerException.toString(); + this->reason = (char*) calloc(strlen(reason) + 1, sizeof(char)); + + return *this; + } + + } // namespace Msg + +}// namespace SimGrid + + + diff --git a/src/cxx/NullPointerException.hpp b/src/cxx/NullPointerException.hpp new file mode 100644 index 0000000000..a7ce0ea023 --- /dev/null +++ b/src/cxx/NullPointerException.hpp @@ -0,0 +1,64 @@ +/* + * NullPointerException.hpp + * + * Copyright 2006,2007 Martin Quinson, Malek Cherier + * All right reserved. + * + * This program is free software; you can redistribute + * it and/or modify it under the terms of the license + *(GNU LGPL) which comes with this package. + * + */ + +#ifndef MSG_NULLPOINTEREXCEPTION_HPP +#define MSG_NULLPOINTEREXCEPTION_HPP + +#include + +namespace SimGrid +{ + namespace Msg + { + + class SIMGRIDX_EXPORT NullPointerException : public Exception + { + public: + + // Default constructor. + NullPointerException(); + + // Copy constructor. + NullPointerException(const NullPointerException& rNullPointerException); + + // This constructor takes the name of the invalid argument. + NullPointerException(const char* name); + + // Destructor. + virtual ~NullPointerException(); + + // Operations. + + // Returns the reason of the exception : + // the message "Null pointer `pointer name'" + const char* toString(void) const; + + // Operators. + + // Assignement. + const NullPointerException& operator = (const NullPointerException& rNullPointerException); + + private : + + // Attributes. + + // A buffer used to build the message returned by the methode toString(). + char* reason; + }; + + + } // namespace Msg + +}// namespace SimGrid + + +#endif // !MSG_NULLPOINTEREXCEPTION_HPP diff --git a/src/cxx/Object.hpp b/src/cxx/Object.hpp new file mode 100644 index 0000000000..660f4eb0fc --- /dev/null +++ b/src/cxx/Object.hpp @@ -0,0 +1,285 @@ + +#ifndef MSG_OBJECT_H +#define MSG_OBJECT_H + +// Compilation C++ recquise +#ifndef __cplusplus + #error Object.hpp requires C++ compilation (use a .cxx suffix) +#endif + +#include + +#include + +namespace SimGrid +{ + namespace Msg + { + ////////////////////////////////////////////////////////////////////////////// + // Macros + + // Returns the runtime class of the class_name object. + #define MSG_GET_CLASS(class_name) \ + ((Class*)(&class_name::class##class_name)) + + // Declare the class class_name as dynamic + #define MSG_DECLARE_DYNAMIC(class_name) \ + public: \ + static Class class##class_name; \ + virtual Class* getClass() const; \ + static Object* createObject(); \ + + // The runtime class implementation. + #define MSG_IMPLEMENT_CLASS(class_name, base_class_name, pfn,class_init) \ + Class class_name::class##class_name = { \ + #class_name, sizeof(class class_name),pfn, \ + MSG_GET_CLASS(base_class_name), NULL,class_init}; \ + Class* class_name::getClass() const \ + { return MSG_GET_CLASS(class_name); } \ + + // CreateObject implementation. + #define MSG_IMPLEMENT_DYNAMIC(class_name, base_class_name) \ + Object* class_name::createObject() \ + { return (Object*)(new class_name); } \ + DeclaringClass _declaringClass_##class_name(MSG_GET_CLASS(class_name)); \ + MSG_IMPLEMENT_CLASS(class_name, base_class_name, \ + class_name::createObject, &_declaringClass_##class_name) \ + + ////////////////////////////////////////////////////////////////////////////// + // Classes declared in this file. + + class Object; // The msg object. + + ////////////////////////////////////////////////////////////////////////////// + // Structures declared in this files. + + struct Class; // used during the rtti operations + struct DeclaringClass; // used during the instances registration. + + + class DeclaringClasses; + + ////////////////////////////////////////////////////////////////////////////// + // Global functions + + // Used during the registration. + void DeclareClass(Class* c); + + ////////////////////////////////////////////////////////////////////////////// + // DeclaringClass + + struct SIMGRIDX_EXPORT DeclaringClass + { + // Constructor : add the runtime classe in the list. + DeclaringClass(Class* c); + + // Destructor + virtual ~DeclaringClass(void); + + // Attributes : + // the list of runtime classes. + static DeclaringClasses* declaringClasses; + }; + + #define MSG_DELCARING_CLASSES (*(DeclaringClass::declaringClasses)) + + + struct SIMGRIDX_EXPORT Class + { + + // Attributes + + const char* name; // class name. + size_t typeSize; // type size. + Object* (*createObjectFn)(void); // pointer to the create object function. + Class* baseClass; // the runtime class of the runtime class. + Class* next; // the next runtime class in the list. + const DeclaringClass* declaringClass; // used during the registration of the class. + + // Operations + + // Create the runtime class from its name. + static Class* fromName(const char* name) + throw (ClassNotFoundException); + + // Create an object from the name of the its class. + static Object* createObject(const char* name); + + // Create an instance of the class. + Object* createObject(void); + + // Return true is the class is dervived from the base class baseClass. + bool isDerivedFrom(const Class* baseClass) const; + + }; + + // Create an instance of the class. + inline Object* Class::createObject(void) + { + return (*createObjectFn)(); + } + + + class SIMGRIDX_EXPORT Object + { + public: + + // Default constructor. + Object(){} + + // Destructor. + virtual ~Object(){} + + // Operations. + + // Get the runtime class. + virtual Class* getClass(void) const; + + // Returns true if the class is derived from the class baseClass. Otherwise + // the method returns false. + bool isDerivedFrom(const Class* baseClass) const; + + // Returns true if the object is valid. Otherwise the method returns false. + virtual bool isValid(void) const; + + // Returns true is the object is an instance of the class specified as parameter. + bool isInstanceOf(const char* className); + + // Operators. + + // Attributes. + + // The runtime class. + static const Class classObject; + }; + + // inline member functions of the class Object. + + // Returns the runtime class of the object. + inline Class* Object::getClass(void) const + { + return MSG_GET_CLASS(Object); + } + + // Returns true if the class is derived from the class pBaseClass. Otherwise + // the method returns false. + inline bool Object::isDerivedFrom(const Class* baseClass) const + { + return (getClass()->isDerivedFrom(baseClass)); + } + + // Returns true if the object is valid. Otherwise the method returns false. + inline bool Object::isValid(void) const + { + // returns always true. + return true; + } + + + class DeclaringClasses + { + public: + + // Constructor. + DeclaringClasses(); + + // Destructor. + virtual ~DeclaringClasses(){} + + // Operations. + + // Add the class at the head of the list. + void addHead(Class* c); + + // Get the runtime class of the head of the list. + Class* getHead(void) const ; + + // Remove the class from the list (don't destroy it). + bool remove(Class* c); + + // Remove the head of the list. + Class* removeHead(void); + + // Return true if the list is empty. + + bool isEmpty(void) const; + + // Remove of the elements of the list. + void removeAll(void); + + // Get the number of classes in the list. + unsigned int getCount(void); + + void lock(void){} + + void unlock(void){} + + //Attributes + + // The head of the list. + Class* head; + + private: + + // Attributes + + // The number of elements of the list. + unsigned int count; + }; + + + // Constructor (Add the class in the list). + inline DeclaringClass::DeclaringClass(Class* c) + { + if(!declaringClasses) + declaringClasses = new DeclaringClasses(); + + DeclareClass(c); + } + + // Destructor. + inline DeclaringClass::~DeclaringClass() + { + /*if(NULL != declaringClasses) + delete declaringClasses; + + declaringClasses=NULL;*/ + + } + + // Returns the number of elements of the list. + inline unsigned int DeclaringClasses::getCount() + { + return count; + } + + // Returns the head of the list. + inline Class* DeclaringClasses::getHead() const + { + return head; + } + + // Returns true if the list is empty. Otherwise this function + // returns false. + inline bool DeclaringClasses::isEmpty() const + { + return(!head); + } + + // Removes all the elements of the list. + inline void DeclaringClasses::removeAll() + { + head = 0; + count=0; + } + + } // namespace Msg +} // namespace SimGrid + + +using namespace SimGrid::Msg; + +#define instanceOf(class_name) reinterpret_cast(Class::createObject(#class_name)) + +#endif // !MSG_OBJECT_H + diff --git a/src/cxx/ProcessNotFoundException.cxx b/src/cxx/ProcessNotFoundException.cxx new file mode 100644 index 0000000000..bae42a3956 --- /dev/null +++ b/src/cxx/ProcessNotFoundException.cxx @@ -0,0 +1,62 @@ +#include + +#include +#include +#include + +namespace SimGrid +{ + namespace Msg + { + + ProcessNotFoundException::ProcessNotFoundException() + { + this->reason = (char*) calloc(strlen("Host not found : unknown") + 1, sizeof(char)); + strcpy(this->reason, "Host not found : unknown"); + } + + + ProcessNotFoundException::ProcessNotFoundException(const ProcessNotFoundException& rProcessNotFoundException) + { + const char* reason = rProcessNotFoundException.toString(); + this->reason = (char*) calloc(strlen(reason) + 1, sizeof(char)); + strcpy(this->reason, reason); + + } + + + ProcessNotFoundException::ProcessNotFoundException(int PID) + { + char buff[7] = {0}; + _itoa(PID, buff, 10); + this->reason = (char*) calloc(strlen("Process not found : ") + strlen(buff) + 1, sizeof(char)); + sprintf(this->reason, "Host not found : %s", buff); + } + + + ProcessNotFoundException::~ProcessNotFoundException() + { + if(this->reason) + free(this->reason); + } + + const char* ProcessNotFoundException::toString(void) const + { + return (const char*)(this->reason); + } + + + const ProcessNotFoundException& ProcessNotFoundException::operator = (const ProcessNotFoundException& rProcessNotFoundException) + { + const char* reason = rProcessNotFoundException.toString(); + this->reason = (char*) calloc(strlen(reason) + 1, sizeof(char)); + + return *this; + } + + } // namespace Msg + +}// namespace SimGrid + + + diff --git a/src/cxx/ProcessNotFoundException.hpp b/src/cxx/ProcessNotFoundException.hpp new file mode 100644 index 0000000000..6e8bc6efdd --- /dev/null +++ b/src/cxx/ProcessNotFoundException.hpp @@ -0,0 +1,64 @@ +/* + * ProcessNotFoundException.hpp + * + * Copyright 2006,2007 Martin Quinson, Malek Cherier + * All right reserved. + * + * This program is free software; you can redistribute + * it and/or modify it under the terms of the license + *(GNU LGPL) which comes with this package. + * + */ + +#ifndef MSG_PROCESSNOTFOUNDEXCEPTION_HPP +#define MSG_PROCESSNOTFOUNDEXCEPTION_HPP + +#include + +namespace SimGrid +{ + namespace Msg + { + + class SIMGRIDX_EXPORT ProcessNotFoundException : public Exception + { + public: + + // Default constructor. + ProcessNotFoundException(); + + // Copy constructor. + ProcessNotFoundException(const ProcessNotFoundException& rProcessNotFoundException); + + // This constructor takes PID of the process. + ProcessNotFoundException(int PID); + + // Destructor. + virtual ~ProcessNotFoundException(); + + // Operations. + + // Returns the reason of the exception : + // the message "Process not found `PID'" + const char* toString(void) const; + + // Operators. + + // Assignement. + const ProcessNotFoundException& operator = (const ProcessNotFoundException& rProcessNotFoundException); + + private : + + // Attributes. + + // A buffer used to build the message returned by the methode toString(). + char* reason; + }; + + + } // namespace Msg + +}// namespace SimGrid + + +#endif // !MSG_PROCESSNOTFOUNDEXCEPTION_HPP