SessionMap.h revision fdd65a0fc7df2c878cc601e4c0f4021cb264f051
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef __SESSIONMAP_H__ 18#define __SESSIONMAP_H__ 19 20#include <utils/KeyedVector.h> 21 22namespace android { 23 24/** 25 * A wrapper template class for handling DRM Engine sessions. 26 */ 27template <typename NODE> 28class SessionMap { 29 30public: 31 KeyedVector<int, NODE> map; 32 33 SessionMap() {} 34 35 virtual ~SessionMap() { 36 destroyMap(); 37 } 38 39/** 40 * Adds a new value in the session map table. It expects memory to be allocated already 41 * for the session object 42 * 43 * @param key - key or Session ID 44 * @param value - session object to add 45 * 46 * @return boolean result of adding value. returns false if key is already exist. 47 */ 48bool addValue(int key, NODE value) { 49 bool result = false; 50 51 if (!isCreated(key)) { 52 map.add(key, value); 53 result = true; 54 } 55 56 return result; 57} 58 59 60/** 61 * returns the session object by the key 62 * 63 * @param key - key or Session ID 64 * 65 * @return session object as per the key 66 */ 67NODE getValue(int key) { 68 NODE value = NULL; 69 70 if (isCreated(key)) { 71 value = (NODE) map.valueFor(key); 72 } 73 74 return value; 75} 76 77/** 78 * returns the number of objects in the session map table 79 * 80 * @return count of number of session objects. 81 */ 82int getSize() { 83 return map.size(); 84} 85 86/** 87 * returns the session object by the index in the session map table 88 * 89 * @param index - index of the value required 90 * 91 * @return session object as per the index 92 */ 93NODE getValueAt(unsigned int index) { 94 NODE value = NULL; 95 96 if (map.size() > index) { 97 value = map.valueAt(index); 98 } 99 100 return value; 101} 102 103/** 104 * deletes the object from session map. It also frees up memory for the session object. 105 * 106 * @param key - key of the value to be deleted 107 * 108 */ 109void removeValue(int key) { 110 deleteValue(getValue(key)); 111 map.removeItem(key); 112} 113 114/** 115 * decides if session is already created. 116 * 117 * @param key - key of the value for the session 118 * 119 * @return boolean result of whether session is created 120 */ 121bool isCreated(int key) { 122 return (0 <= map.indexOfKey(key)); 123} 124 125/** 126 * empty the entire session table. It releases all the memory for session objects. 127 */ 128void destroyMap() { 129 int size = map.size(); 130 int i = 0; 131 132 for (i = 0; i < size; i++) { 133 deleteValue(map.valueAt(i)); 134 } 135 136 map.clear(); 137} 138 139/** 140 * free up the memory for the session object. 141 * Make sure if any reference to the session object anywhere, otherwise it will be a 142 * dangle pointer after this call. 143 * 144 * @param value - session object to free 145 * 146 */ 147void deleteValue(NODE value) { 148 delete value; 149} 150 151}; 152 153}; 154 155#endif /* __SESSIONMAP_H__ */ 156