00001 /* 00002 * Copyright (c) 2000, 2020, Oracle and/or its affiliates. 00003 * 00004 * Licensed under the Universal Permissive License v 1.0 as shown at 00005 * http://oss.oracle.com/licenses/upl. 00006 */ 00007 #ifndef COH_BOXING_MAP_HPP 00008 #define COH_BOXING_MAP_HPP 00009 00010 #include "coherence/lang.ns" 00011 00012 #include "coherence/stl/adapter_map.hpp" 00013 00014 00015 00016 COH_OPEN_NAMESPACE2(coherence,stl) 00017 00018 00019 /** 00020 * boxing_map provides an STL-like map adapter around a coherence::util::map, 00021 * and additionally auto-boxes the keys and values to their corresponding 00022 * non-managed types. The key and value template parameters specify the managed 00023 * class which will be stored in the Map. Other then the definition, the access 00024 * to the keys and values may use either the managed view type or the 00025 * corresponding unmanaged type. This also allows for easy copying of mapped 00026 * entries from a std::map to coherence maps and caches. 00027 * 00028 * @code 00029 * boxing_map<Managed<Foo>, Managed<Bar> > mycache 00030 * (CacheFactory::getCache("mycache")); // declare boxing adapter 00031 * 00032 * Foo foo(...); // initialize non-managed object Foo 00033 * Bar bar(...); // initialize non-managed object Bar 00034 * 00035 * mycache[foo] = bar; // insert directly into cache via auto-boxing 00036 * Bar barRead = mycache[foo]; // read from cache using auto-boxing 00037 * 00038 * std::map<Foo, Bar> mymap; 00039 * mymap.insert(mycache.begin(), mycache.end()); // copy from coherence to STL 00040 * mycache.insert(mymap.begin(), mymap.end()); // copy from STL to coherence 00041 * @endcode 00042 * 00043 * @see coherence::util::TypedCollections::TypedMap for an Object based equivalent 00044 * 00045 * @author mf 2008.06.02 00046 */ 00047 template<class K, class V> 00048 class boxing_map 00049 : public adapter_map<BoxHandle<const K, typename K::BoxedType, false>, 00050 BoxHandle<const V, typename V::BoxedType, false> > 00051 { 00052 // ----- typedefs ------------------------------------------------------- 00053 00054 public: 00055 /** 00056 * Super type 00057 */ 00058 typedef adapter_map<BoxHandle<const K, typename K::BoxedType, false>, 00059 BoxHandle<const V, typename V::BoxedType, false> > 00060 super; 00061 00062 00063 // ----- constructor ---------------------------------------------------- 00064 00065 public: 00066 /** 00067 * Create new boxing_map from the given Coherence Map. 00068 * 00069 * @param ohMap the Map to be delegated by the boxing_map. 00070 */ 00071 boxing_map(Map::Holder ohMap = NULL) 00072 : super(ohMap) 00073 { 00074 } 00075 00076 /** 00077 * Create a boxing_map copy. The new boxing_map will reference the 00078 * same Map, as the original boxing_map. 00079 */ 00080 boxing_map(const boxing_map& that) 00081 : super(that) 00082 { 00083 } 00084 00085 00086 // ----- operators ------------------------------------------------------ 00087 00088 public: 00089 /** 00090 * Reassign this boxing_map to reference the same Map as another 00091 * boxing_map. 00092 */ 00093 boxing_map& operator=(const boxing_map& that) 00094 { 00095 super::operator=(that); 00096 return *this; 00097 } 00098 00099 /** 00100 * Reassign this boxing_map to reference another Map. 00101 */ 00102 boxing_map& operator=(Map::Holder ohMap) 00103 { 00104 super::operator=(ohMap); 00105 return *this; 00106 } 00107 }; 00108 00109 COH_CLOSE_NAMESPACE2 00110 00111 #endif // COH_BOXING_MAP_HPP