| --- |
| title: Using C++ Enum Type with PDX Serialization |
| --- |
| |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| Because there is no "object" base type in C++, enums cannot be directly passed as parameters to the `writeObject` and `readObject` API. |
| |
| To use the C++ enum type with PDX serialization, you have to wrap the `enum` in the `CacheableEnum` class type by specifying classname, enumname and ordinal. |
| |
| ``` pre |
| enum enumQuerytest { id1, id2, id3 }; |
| class TESTOBJECT_EXPORT PdxEnumTestClass :public PdxSerializable |
| { |
| private: |
| int m_id; |
| CacheableEnumPtr m_enumid; |
| |
| public: |
| int getID(){ |
| return m_id; |
| } |
| |
| CacheableEnumPtr getEnumID() { |
| return m_enumid; |
| } |
| |
| PdxEnumTestClass(int id) |
| { |
| m_id = id; |
| switch (m_id) { |
| case 0: |
| m_enumid = CacheableEnum::create("enumQuerytest", "id1", id1); |
| break; |
| case 1: |
| m_enumid = CacheableEnum::create("enumQuerytest", "id2", id2); |
| break; |
| case 2: |
| m_enumid = CacheableEnum::create("enumQuerytest", "id3", id3); |
| break; |
| default: |
| m_enumid = CacheableEnum::create("enumQuerytest", "id1", id1); |
| break; |
| } |
| } |
| |
| PdxEnumTestClass() { } |
| |
| void toData(PdxWriterPtr pw) { |
| pw->writeInt("m_id", m_id); |
| pw->writeObject("m_enumid", m_enumid); |
| } |
| |
| void fromData(PdxReaderPtr pr) { |
| m_id = pr->readInt("m_id"); |
| m_enumid = pr->readObject("m_enumid"); |
| } |
| |
| CacheableStringPtr toString() const { |
| return CacheableString::create("PdxEnumTestClass"); |
| } |
| |
| char* GetClassName() const { |
| return "com.example.PdxEnumTestClass"; |
| } |
| |
| static PdxSerializable* createDeserializable() { |
| return new PdxEnumTestClass(); |
| } |
| }; |
| ``` |
| |
| ## <a id="concept_F38FDBC327204B4EB1E0BC74B4C95409__section_3491F76DB8C0464D89418B89372BBAEA" class="no-quick-link"></a>How Puts and Queries Work on Enums |
| |
| The following code sample demonstrates how put and query operations work when using the C++ enum Type with PDX serialization: |
| |
| ``` pre |
| //Creating objects of type PdxEnumTestClass |
| PdxEnumTestClassPtr pdxobj1(new PdxEnumTestClass(0)); |
| PdxEnumTestClassPtr pdxobj2(new PdxEnumTestClass(1)); |
| PdxEnumTestClassPtr pdxobj3(new PdxEnumTestClass(2)); |
| |
| RegionPtr rptr = getHelper()->getRegion( "DistRegionAck" ); |
| |
| //PUT Operations |
| rptr->put( CacheableInt32::create(0), pdxobj1 ); |
| LOG( "pdxPut 1 completed " ); |
| |
| rptr->put( CacheableInt32::create(1), pdxobj2 ); |
| LOG( "pdxPut 2 completed " ); |
| |
| rptr->put( CacheableInt32::create(2), pdxobj3 ); |
| LOG( "pdxPut 3 completed " ); |
| |
| |
| //Query |
| try { |
| Serializable::registerPdxType(PdxEnumTestClass::createDeserializable); |
| LOG("PdxEnumTestClass Registered Successfully...."); |
| } catch (geode::IllegalStateException&/* ex*/) { |
| LOG("PdxEnumTestClass IllegalStateException"); |
| } |
| |
| RegionPtr rptr = getHelper()->getRegion( "DistRegionAck" ); |
| SelectResultsPtr results = rptr->query("m_enumid.name = 'id2'"); |
| ASSERT(results->size()== 1 , "query result should have one item"); |
| ResultSetPtr rsptr = dynCast<ResultSetPtr>(results); |
| SelectResultsIterator iter = rsptr->getIterator(); |
| while (iter.moveNext()) { |
| PdxEnumTestClassPtr re = dynCast<PdxEnumTestClassPtr>(iter.current()); |
| ASSERT(re->getID()== 1 , "query should have returned id 1"); |
| } |
| ``` |
| |
| |