From 579d0272037617a64ab8c4b94d69522eb0ced122 Mon Sep 17 00:00:00 2001 From: DarkWyrm Date: Thu, 23 Jan 2003 01:06:45 +0000 Subject: [PATCH] Added TokenHandler class and associated documentation git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2534 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/servers/app/server/Jamfile | 2 + src/servers/app/server/TokenHandler.cpp | 125 ++++++++++++++++++++++++ src/servers/app/server/TokenHandler.h | 53 ++++++++++ 3 files changed, 180 insertions(+) create mode 100644 src/servers/app/server/TokenHandler.cpp create mode 100644 src/servers/app/server/TokenHandler.h diff --git a/src/servers/app/server/Jamfile b/src/servers/app/server/Jamfile index 2e1e53d0d9..4bf11a8676 100644 --- a/src/servers/app/server/Jamfile +++ b/src/servers/app/server/Jamfile @@ -7,6 +7,8 @@ UseFreeTypeHeaders ; Server app_server : # Misc. Sources Angle.cpp + TokenHandler.cpp + # ColorUtils will disappear as soon as I can figure out how to link the server # against libopenbeos ColorUtils.cc diff --git a/src/servers/app/server/TokenHandler.cpp b/src/servers/app/server/TokenHandler.cpp new file mode 100644 index 0000000000..e1b0d66018 --- /dev/null +++ b/src/servers/app/server/TokenHandler.cpp @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2001-2002, OpenBeOS +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// +// File Name: TokenHandler.cpp +// Author: DarkWyrm +// Description: Class to provide tokens with excluded values possible. +// +//------------------------------------------------------------------------------ +#include "TokenHandler.h" + +//! Does setup +TokenHandler::TokenHandler(void) +{ + _index=-1; + _lock=create_sem(1,"tokenhandler_sem"); + + _excludes=new BList(0); +} + +//! Undoes the setup from the constructor +TokenHandler::~TokenHandler(void) +{ + ResetExcludes(); + delete_sem(_lock); + delete _excludes; +} + +/*! + \brief Returns a unique token + \return A unique token + + If a value is excluded the next higher non-excluded value is returned. +*/ +int32 TokenHandler::GetToken(void) +{ + int32 value; + + acquire_sem(_lock); + + _index++; + while(IsExclude(_index)) + _index++; + value=_index; + release_sem(_lock); + return value; +} + +/*! + \brief Excludes a value from being returned as a token. + + This is quite useful for excluding values like B_ERROR and such. +*/ +void TokenHandler::ExcludeValue(int32 value) +{ + acquire_sem(_lock); + + if(!IsExclude(value)) + _excludes->AddItem(new int32(value)); + release_sem(_lock); +} + +//! Resets the token index +void TokenHandler::Reset(void) +{ + acquire_sem(_lock); + _index=-1; + release_sem(_lock); +} + +//! Empties object of all assigned excluded values +void TokenHandler::ResetExcludes(void) +{ + acquire_sem(_lock); + int32 *temp; + for(int32 i=0; i<_excludes->CountItems();i++) + { + temp=(int32*)_excludes->ItemAt(i); + if(temp) + delete temp; + } + _excludes->MakeEmpty(); + release_sem(_lock); +} + +/*! + \brief Determines whether a value is excluded + \param value The value to be checked + \return True if the value is excluded from being a token, false if not. +*/ +bool TokenHandler::IsExclude(int32 value) +{ + bool match=false; + int32 *temp; + + acquire_sem(_lock); + for(int32 i=0;i<_excludes->CountItems();i++) + { + temp=(int32*)_excludes->ItemAt(i); + if(temp && *temp==value) + { + match=true; + break; + } + } + delete_sem(_lock); + return match; +} diff --git a/src/servers/app/server/TokenHandler.h b/src/servers/app/server/TokenHandler.h new file mode 100644 index 0000000000..b0bc2eac4b --- /dev/null +++ b/src/servers/app/server/TokenHandler.h @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2001-2002, OpenBeOS +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// +// File Name: TokenHandler.h +// Author: DarkWyrm +// Description: Class to provide tokens with excluded values possible. +// +//------------------------------------------------------------------------------ +#ifndef TOKENHANDLER_H_ +#define TOKENHANDLER_H_ + +#include +#include + +/*! + \class TokenHandler TokenHandler.h + \brief Class to provide tokens with excluded values possible. +*/ +class TokenHandler +{ +public: + TokenHandler(void); + ~TokenHandler(void); + int32 GetToken(void); + void ExcludeValue(int32 value); + void Reset(void); + void ResetExcludes(void); + bool IsExclude(int32 value); +private: + int32 _index; + sem_id _lock; + BList *_excludes; +}; + +#endif \ No newline at end of file