// Description   : NANO OS Core  $Revision: 34 $

// Copyright     : Proxima Centauri Romania SRL, ALL RIGHTS RESERVED. (http://www.ProximaCentauri.org)

// Author        : Alexander Ioan Mihail

 

#ifndef core_h

#define core_h 1

 

#ifdef _MSC_VER

  #pragma warning(4:4018)

  #pragma warning(4:4101)

  #pragma warning(4:4267)

  #pragma warning(4:4291)

  #pragma warning(4:4355)

  #pragma warning(4:4482)

  #pragma warning(4:4311)

  #pragma warning(4:4312)

  #pragma warning(4:4996)

#endif

 

#ifdef REVENV

 #define PACKAGE

 #define CLASS class

#else

 #ifdef CORE_IMPORT

  #define PACKAGE __declspec(dllimport)

 #else

  #define PACKAGE __declspec(dllexport)

 #endif

#endif

 

#include <string.h>

#include <stdio.h>

#include <stdarg.h>

#include <time.h>

#include <malloc.h>

 

#define PSTRING char*

#define nullptr 0

#define DATE_TIME int

#define MAX_DATE 0x7FFFFFFF

#define MIN_DATE -MAX_DATE

 

#define CLAIM_SPINLOCK(name)\

{\

  while (name)\

    BreakPoint();\

  name=1;\

}

 

#define RELEASE_SPINLOCK(name)\

  name=0;

 

#define CLASS struct PACKAGE

 

#define START_NAMESPACE(ns) namespace ns {

#define END_NAMESPACE(ns) }

 

START_NAMESPACE(NANO_CORE)

  CLASS STREE_BASE;

  CLASS ELEMENT;

  typedef ELEMENT* PELEMENT;

  typedef STREE_BASE* PSTREE_BASE;

 

#define COMMON_DECLARATIONS(type,ancestor)\

  typedef ancestor Inherits;\

  type::type(){}

 

#define FOR_CHILDREN_OF(element)\

  {\

    BOND&aspect=(element)->TSLPIO[1];\

    BOND*limit=aspect.POYC[3];\

    if (limit)\

    {\

      aspect.Lock();\

      limit=limit->POYC[1];\

      BOND*sb=limit;\

      BOND*n;\

      do\

      {\

        n=sb->POYC[1];\

        if (!sb->IsEndpoint())\

        {\

          var=sb->Object;

#define FOR_INSTANCES_OF(element)\

  {\

    BOND&aspect=(element)->TSLPIO[0];\

    BOND*limit=aspect.POYC[3];\

    if (limit)\

    {\

      aspect.Lock();\

      limit=limit->POYC[1];\

      BOND*sb=limit;\

      BOND*n;\

      do\

      {\

        n=sb->POYC[1];\

        {\

          var=(ELEMENT*)((ELEMENT*)((char*)sb-(char*)&(*((ELEMENT*)0)).TSLPIO[0]));

#define FOREND\

          var=nullptr;\

        }\

        if (sb==n)\

          break;\

      }\

      while ((sb=n)!=limit);\

      aspect.Unlock();\

    }\

  }

 

CLASS CAT_CORE;

CLASS BOND;

CLASS ELEMENT;

 

struct ELEMENT;

 

typedef enum ETraceFlags {NORMAL, NOCRLF, NOTIME, BOLD, BLUE, GREEN, RED} ETraceFlags;

 

struct BOND

{

  public:

    typedef enum {flLocked,flOpened,flMax} EFlags;

  public:

      BOND ();

      ~BOND ();

      static BOND* INIT (ELEMENT* container, BOND* parent, BOND* self);

      void DONE (bool release);

      void Insert (BOND* parent, BOND* before = nullptr);

      void Remove ();

      int Count ();

      BOND* get_First ();

      int IsEndpoint ();

      void Lock ();

      void Unlock ();

      ELEMENT *Object;

      BOND *POYC[4];

      int Flags;

      int Extra;

      char *LVR[3];

};

 

typedef int (*int_XXX_XvoidXX_class_ELEMENTXX_intX_class_BONDXX) (void*, class ELEMENT*, int, class BOND*);

typedef int (*int_XXX_XvoidXXenum_ETraceFlagsX_intX_charXX) (void*,enum ETraceFlags, int, char*);

 

struct ELEMENT

{

  enum EFind

  {

    FLAT,

    INSENSITIVE,

    PARTIAL

  };

  COMMON_DECLARATIONS(ELEMENT,ELEMENT)

  void*operator new(size_t sz, void*ptr)

  {

    return ptr;

  }

 

  public:

      virtual ~ELEMENT ();

      void operator delete (void* ptr);

      static ELEMENT* INIT (ELEMENT* container, ELEMENT* parent, const char* name, ELEMENT* target, ELEMENT* type, ELEMENT* self, int value);

      ELEMENT* TypeOf ();

      virtual bool IsOfType (ELEMENT* type);

      virtual bool IsSelector ();

      virtual void Insert (ELEMENT* parent);

      virtual void Remove ();

      virtual void Touch (size_t value, int aspect);

      virtual int Evaluate (int phase, int param1);

      virtual ELEMENT* Find (const char* name, int flags);

      virtual void Expand (BOND* aspect, bool on);

      void* SetEditor (void* editor);

      CAT_CORE* GetKernel ();

      ELEMENT* GetParent ();

      virtual int GetTypeID ();

      virtual void GetPath (char* path);

      ELEMENT* GetEndpoint ();

      int GetId ();

      void GetBitmapData (void*& ptr, int& size);

      ELEMENT* GetActor ();

      ELEMENT* GetProcess ();

      static ELEMENT* GetThread ();

      static bool IsAdmin ();

      static char* GetVersion ();

      static int Trace (enum ETraceFlags flags, int level, char* msg);

      static int Edit (void* app, ELEMENT* obj, int flags, BOND* aspect)

      {

        if (!fEdit) return 0; return fEdit(app,obj,flags,aspect);

      }

      void* ReAlloc (void* ptr, size_t size, void* pref);

      int Translate (int direction, int offset, char* buf, int len, int flags);

      void Walk ();

      int GetSize (char* path, size_t offset);

      ELEMENT* Fork (const char* name, int stacksize = 0, int is_gui = false, void* context = nullptr, ELEMENT* pivot = nullptr);

      char *Name;

      char *Info;

      BOND TSLPIO[6];

      static int_XXX_XvoidXXenum_ETraceFlagsX_intX_charXX fTrace;

      static int_XXX_XvoidXX_class_ELEMENTXX_intX_class_BONDXX fEdit;

};

 

struct CAT_CORE : public ELEMENT  //## Inherits: <unnamed>

{

  COMMON_DECLARATIONS(CAT_CORE,ELEMENT)

  typedef enum

  {

    em_VOID,

    em_INT,

    em_CHAR,

    em_BOOL,

    em_BOND,

    em_ELEMENT,

    em_HAS,

    em_REF, 

    em_THREAD,

    em_PROCESS,

    em_ACTOR,

    em_PROXY,

    em_META,

    em_Actors,

    rel_Object,

    rel_POYC,

    rel_Flags,

    rel_Extra,

    rel_LVR,

    rel_VMT,

    rel_Name,

    rel_Info,

    rel_TSLPIO,

    em_Max

  } ETypes;

 

  public:

      CAT_CORE (ELEMENT* container, ELEMENT* parent, char* name, ELEMENT* type, void* start, void* end, int value);

      bool IsUsed ();

      ELEMENT Types[em_Max];

};

 

extern "C" PACKAGE void BreakPoint();

extern "C" PACKAGE void* our_calloc(size_t sz,size_t element_sz);

extern "C" PACKAGE void  our_free(void*ptr);

PACKAGE void debug_trace(int flags,int level,const char*format,...);

END_NAMESPACE(NANO_CORE)

 

extern "C" PACKAGE int OurWaitForMultipleObjects(int nCount,void** lpHandles,int bWaitAll,int dwMilliseconds);

extern "C" PACKAGE char* GetCurrentUserName();

extern "C" PACKAGE void*init_shared_memory(const char*fname,void*address,int size);

extern "C" PACKAGE void done_shared_memory(void*hmem);

extern "C" PACKAGE void ApiError(char*msg);

extern "C" PACKAGE void OurSleep(int msecs);

extern "C" PACKAGE void* Buffer2Bmp(char*image);

 

#define TRACE_ERROR NANO_CORE::RED

#define TRACE(x) debug_trace x

#define ENTER ELEMENT::Trace(ETraceFlags::NORMAL,0,"Entering " __FUNCSIG__ "\r\n");

#define EXIT ELEMENT::Trace(ETraceFlags::NORMAL,0,"Exiting " __FUNCSIG__ "\r\n");

#define STACK_FRAME void*dummy_stack_frame=alloca(0);

#endif