A device detection library that is used natively or by 51Degrees products

Hash/MetaData.cpp

Meta data example of using 51Degrees device detection.

Meta data example of using 51Degrees device detection.The example shows how to:

  1. Specify the name of the data file, properties the data set should be initialised with, and the configuration.
    using namespace FiftyoneDegrees;
    string fileName = "51Degrees-V4.1.hash";
    string propertiesString = "ScreenPixelsWidth,HardwareModel,IsMobile,BrowserName";
    new Common::RequiredPropertiesConfig(&propertiesString);
    DeviceDetection::Hash::DeviceDetection::Hash::ConfigHash *config =
    new DeviceDetection::Hash::DeviceDetection::Hash::ConfigHash();
    C++ class wrapper for the fiftyoneDegreesPropertiesRequired structure.
    Definition RequiredPropertiesConfig.hpp:59
    A C++ wrapper class for transform.h conversion routines.
    Definition ComponentMetaDataBuilderHash.hpp:33
  2. Construct a new engine from the specified data file with the required properties and the specified configuration.
    using namespace FiftyoneDegrees;
    dataFilePath,
    config,
    properties);
    Encapsulates the Hash engine class which implements EngineDeviceDetection.
    Definition EngineHash.hpp:96
  3. Fetch a collection containing the meta data of all the properties in the engine's data set.
    using namespace FiftyoneDegrees;
    engine->getMetaData()->getProperties();
    A group of items accessible by index or key.
    Definition Collection.hpp:79
    MetaData * getMetaData() const
    Return the a pointer to the meta data class which contains meta data for the properties,...
    virtual Collection< string, PropertyMetaData > * getProperties() const =0
    Get a new Collection instance of all property meta data keyed on the name of the property.
  4. Iterate over all the properties and print the name and description. Note that getting a property meta data instance will return a new instance which holds no reference to the engine nor any of its internals. Therefore it is the responsibility of the caller to delete it once it is no longer needed.
    using namespace FiftyoneDegrees;
    for (uint32_t i = 0; i < properties->getSize(); i++){
    Common::PropertyMetaData *property = properties->getByIndex(i);
    cout << property->getName() << " - " << property->getDescription() << "\n";
    delete property;
    }
    Meta data relating to a property populated by an engine implementation.
    Definition PropertyMetaData.hpp:38
  5. Release the memory used by the properties collection, and its reference to the engine's underlying data set.
    delete properties;
  6. Finally release the memory used by the engine.
    delete engine;
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
// Include ExmapleBase.h before others as it includes Windows 'crtdbg.h'
// which requires to be included before 'malloc.h'.
#include "../../C/Hash/ExampleBase.h"
#include "ExampleBase.hpp"
namespace FiftyoneDegrees {
namespace Examples {
namespace Hash {
class MetaDataExample : public ExampleBase {
public:
: ExampleBase(dataFilePath, config)
{};
void run() {
PropertyMetaData *property;
Collection<string, PropertyMetaData> *localProperties;
cout << "Starting MetaData Example.\n";
localProperties = engine->getMetaData()->getProperties();
for (uint32_t i = 0; i < localProperties->getSize(); i++){
property = localProperties->getByIndex(i);
cout << property->getName() << " - " << property->getDescription() << "\n";
delete property;
}
delete localProperties;
}
};
}
}
}
extern "C" void fiftyoneDegreesExampleCPPMetaDataRun(ExampleParameters *params) {
// Call the actual function.
try {
MetaDataExample *metaData = new MetaDataExample(params->dataFilePath, cppConfig);
metaData->run();
delete metaData;
}
catch (const StatusCodeException &e) {
ExampleBase::reportStatus(e.getCode(), params->dataFilePath);
exit(1);
}
}
#ifndef TEST
int main(int argc, char* argv[]) {
char dataFilePath[FIFTYONE_DEGREES_FILE_MAX_PATH];
if (argc > 1) {
strcpy(dataFilePath, argv[1]);
}
else {
dataDir,
dataFileName,
dataFilePath,
sizeof(dataFilePath));
}
ExampleBase::reportStatus(status, dataFileName);
fgetc(stdin);
return 1;
}
ExampleParameters params;
params.dataFilePath = dataFilePath;
// run the example
fiftyoneDegreesExampleMemCheck(
&params,
fiftyoneDegreesExampleCPPMetaDataRun);
// Wait for a character to be pressed.
fgetc(stdin);
return 0;
}
#endif
C++ class wrapper for the fiftyoneDegreesConfigHash configuration structure.
Definition ConfigHash.hpp:72
Base class extended by all Hash examples.
Definition ExampleBase.hpp:58
static fiftyoneDegreesStatusCode getFilePath(const char *dataFolderName, const char *fileName, char *destination, size_t size)
Finds a file in the data folder, and downloads it first when it is missing and the data repository li...
EngineHash * engine
Hash Engine used for the example.
Definition ExampleBase.hpp:244
ExampleBase(byte *data, long length, DeviceDetection::Hash::ConfigHash *config)
Construct a new instance of the example to be run using the data pointer provided.
static void reportStatus(fiftyoneDegreesStatusCode status, const char *fileName)
Reports the status of the data file initialization.
DeviceDetection::Hash::ConfigHash * config
Configuration for the Engine.
Definition ExampleBase.hpp:240
Hash Meta Data Example.
Definition MetaData.cpp:102
void run()
Run the example.
Definition MetaData.cpp:114
MetaDataExample(string dataFilePath, DeviceDetection::Hash::ConfigHash *config)
Construct a new instance of the example to be run using the data file provided.
Definition MetaData.cpp:107
#define FIFTYONE_DEGREES_FILE_MAX_PATH
Define the max path length on the target system.
Definition file.h:218
EXTERNAL_VAR fiftyoneDegreesConfigHash fiftyoneDegreesHashDefaultConfig
Default detection configuration.
Definition hash.h:436
fiftyoneDegreesStatusCode
Status returned from the initialisation of a resource.
Definition status.h:81
@ FIFTYONE_DEGREES_STATUS_SUCCESS
All okay.
Definition status.h:82
C++ Hash engine examples.
Definition ExampleBase.hpp:54
C++ Examples.
Definition dd/FiftyoneDegrees.hpp:39
Hash specific configuration structure.
Definition hash.h:187