• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

InkPool.h

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 #ifndef __INK_POOL_H_INCLUDED__
00025 #define __INK_POOL_H_INCLUDED__
00026 
00027 //
00028 // Template of a static size pool of objects.
00029 //
00030 //
00031 template<class C> class InkStaticPool {
00032 public:
00033 
00034   InkStaticPool(int size):sz1(size + 1), head(0), tail(0)
00035   {
00036     pool = new C *[sz1];
00037   }
00038 
00039   virtual ~ InkStaticPool() {
00040     cleanUp();
00041     delete[]pool;
00042   }
00043 
00044   C *get();
00045   bool put(C * newObj);
00046   void put_or_delete(C * newObj)
00047   {
00048     if (!put(newObj))
00049       delete newObj;
00050   }
00051 
00052 protected:
00053   void cleanUp(void);
00054 
00055 private:
00056   const int sz1;
00057   int head;
00058   int tail;
00059   C **pool;
00060 };
00061 
00062 template<class C> inline C * InkStaticPool<C>::get()
00063 {
00064   if (head != tail) {
00065     C *res = pool[head++];
00066     head %= sz1;
00067     return (res);
00068   }
00069   return (NULL);
00070 }
00071 
00072 template<class C> inline bool InkStaticPool<C>::put(C * newObj)
00073 {
00074   if (newObj == NULL)
00075     return (false);             // cannot put NULL pointer
00076 
00077   int newTail = (tail + 1) % sz1;
00078   bool res = (newTail != head);
00079   if (res) {
00080     pool[tail] = newObj;
00081     tail = newTail;
00082   }
00083   return (res);
00084 }
00085 
00086 template<class C> inline void InkStaticPool<C>::cleanUp(void)
00087 {
00088   while (true) {
00089     C *tp = get();
00090     if (tp == NULL)
00091       break;
00092     delete tp;
00093   }
00094 }
00095 
00096 #endif // #ifndef __INK_POOL_H_INCLUDED__

Generated by  doxygen 1.7.1