blob: 5babcad2ba86af47ef0b8d3fce4668dab069a2ca [file]
/*
* 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.
*/
/*
* The PdxInstance QuickStart Example.
* This example takes the following steps:
*
* This example shows PdxInstanceFactory and PdxInstance usage.
*
* 1. Create a Geode Cache.
* 2. Creates the PdxInstanceFactory for Person class.
* 3. Then creates instance of PdxInstance
* 4. It does put.
* 5. Then it does get and access it fields.
* 6. Close the Cache.
*
*/
// Include the Geode library.
#include <gfcpp/GeodeCppCache.hpp>
// Use the "geode" namespace.
using namespace apache::geode::client;
class Person {
private:
char* m_name;
int m_id;
int m_age;
public:
Person() {}
Person(char* name, int id, int age) {
m_name = name;
m_id = id;
m_age = age;
}
char* getName() const { return m_name; }
int getID() { return m_id; }
int getAge() { return m_age; }
};
// The PdxInstance QuickStart example.
int main(int argc, char** argv) {
try {
// Create a Geode Cache.
CacheFactoryPtr cacheFactory = CacheFactory::createCacheFactory();
CachePtr cachePtr =
cacheFactory->set("cache-xml-file", "XMLs/clientPdxInstance.xml")
->create();
LOGINFO("Created the Geode Cache");
// Get the example Region from the Cache which is declared in the Cache XML
// file.
RegionPtr regionPtr = cachePtr->getRegion("Person");
LOGINFO("Obtained the Region from the Cache.");
Person* p = new Person("Jack", 7, 21);
// PdxInstanceFactory for Person class
PdxInstanceFactoryPtr pif = cachePtr->createPdxInstanceFactory("Person");
LOGINFO("Created PdxInstanceFactory for Person class");
pif->writeString("m_name", p->getName());
pif->writeInt("m_id", p->getID());
pif->markIdentityField("m_id");
pif->writeInt("m_age", p->getAge());
PdxInstancePtr pdxInstance = pif->create();
LOGINFO("Created PdxInstance for Person class");
regionPtr->put("Key1", pdxInstance);
LOGINFO("Populated PdxInstance Object");
PdxInstancePtr retPdxInstance = regionPtr->get("Key1");
LOGINFO("Got PdxInstance Object");
int id = 0;
retPdxInstance->getField("m_id", id);
int age = 0;
retPdxInstance->getField("m_age", age);
char* name = NULL;
retPdxInstance->getField("m_name", &name);
if (id == p->getID() && age == p->getAge() &&
strcmp(name, p->getName()) == 0 &&
retPdxInstance->isIdentityField("m_id")) {
LOGINFO("PdxInstance returns all fields value expected");
} else {
LOGINFO("PdxInstance doesn't returns all fields value expected");
}
delete p;
// Close the Geode Cache.
cachePtr->close();
LOGINFO("Closed the Geode Cache");
return 0;
}
// An exception should not occur
catch (const Exception& geodeExcp) {
LOGERROR("PdxInstance Geode Exception: %s", geodeExcp.getMessage());
return 1;
}
}