213 lines
5.2 KiB
C++

// Copyright (C) 2004 Intel Corporation
#ifndef __AMT_MOF_OBJECT_H
#define __AMT_MOF_OBJECT_H
#include <memory>
#include "WsmanClient.h"
#include "EndpointReference.h"
using namespace std;
using namespace WsmanClientNamespace;
using namespace CimXMLUtilsNamespace;
using namespace CimXMLTypesNamespace;
using namespace WsmanExceptionNamespace;
namespace CimClassNamespace
{
template<class T>
class CimClassContainer;
// CIMClass Exception class
class CIMClassException : public GeneralWsmanException
{
public:
CIMClassException(const char* msg, unsigned int err = WSMAN_GENERAL_ERROR)
:GeneralWsmanException(msg, err){}
virtual ~CIMClassException() throw() {}
};
class CimClass : public Serializable, public DeSerializable
{
private:
bool isCreated;
string resourceUri;
ResourceCreated creationData;
protected:
static const WsmanClient* defaultProvider;
public:
CimClass(const string& objName,
const string& uri,
const string& ns,
const string& nsPrefix = "");
virtual ~CimClass();
string GetObjectName() const;
string GetResourceUri() const;
virtual NameValuePairs GetSelectorsList() const = 0;
EndpointReference ToEndpointReference() const;
void Get(const NameValuePairs *selectors = NULL, const WsmanClient *cl = defaultProvider);
void Get(const NameValuePairs *selectors, string & outputXML, const WsmanClient *cl = defaultProvider);
void Put(const WsmanClient *cl = defaultProvider);
void Put(string inputXML, const WsmanClient *cl = defaultProvider);
void Create(const WsmanClient *cl = defaultProvider);
void Delete(const WsmanClient *cl = defaultProvider);
void Invoke(const string& functionName,
const Serializable* input, DeSerializable* output,
const WsmanClient *cl = defaultProvider) const;
// Enumerates the CIM objects of type T. Subclasses will
// be enumerated but they will be returned as instances of
// T. (Subclass properties will not be recorded).
template<class T>
static void Enumerate(vector<T> &vec,
const NameValuePairs *selectors = NULL,
const WsmanClient *cl = defaultProvider);
// Enumerates the CIM objects of type T. Subclasses will
// be enumerated but they will be returned as strings in xml
// format. (Subclass properties will not be recorded).
template<class T>
static void Enumerate(vector<string> &vec,
const NameValuePairs *selectors = NULL,
const WsmanClient *cl = defaultProvider);
// Enumerates the CIM objects of type T. Subclasses will
// be enumerated and subclass properties will be recorded.
// Pointer to the created instances of T are entered to the
// given container.
template<class T>
static void Enumerate(CimClassContainer<T> &container,
const NameValuePairs *selectors = NULL,
const WsmanClient *cl = defaultProvider);
static void RegisterDefaultWsmanProvider(const WsmanClient *cl);
};
// maps CimClass names to ids, used by the factory class
class CimClassMap : public map<string, unsigned int>
{
public:
CimClassMap();
~CimClassMap(){}
};
// map entry
typedef pair<string,unsigned int> map_entry;
// factory class
class CimClassFactory
{
private:
static const CimClassMap cimMap;
// factory function. The implementation for this is autogenerated
// in CimClassFactory.cpp
public:
static CimClass* CreateCimClass(const string& xml);
};
template<class T>
class CimClassContainer
{
private:
vector<T*> vec;
public:
CimClassContainer(){}
~CimClassContainer()
{
Clear();
}
void AddCimClass(const string& xml)
{
T* t = static_cast<T*>(CimClassFactory::CreateCimClass(xml));
if(!t)
{
throw CIMClassException("An unidentified CIM object was received.", WSMAN_RESPONSE_UNKNOWN);
}
vec.push_back(t);
}
T* operator [](unsigned int i) const
{
return vec[i];
}
unsigned int Size() const
{
return vec.size();
}
bool Empty() const
{
return vec.empty();
}
void Clear()
{
for(unsigned int i = 0; i < vec.size(); i++)
{
delete vec[i];
vec[i] = NULL;
}
vec.empty();
}
};
}
using namespace CimClassNamespace;
template<class T>
void CimClass::Enumerate(vector<T> &vec,
const NameValuePairs *selectors,
const WsmanClient *cl)
{
if(!cl)
{
throw CIMClassException("A NULL WsmanClient pointer was provided.", WSMAN_MISSING_INPUT);
}
vector<string > tmp;
cl->Enumerate(T::CLASS_URI, tmp, selectors);
for(unsigned int i = 0; i < tmp.size(); i++)
{
T t;
t.Deserialize(tmp[i]);
vec.push_back(t);
}
}
template<class T>
void CimClass::Enumerate(vector<string> &vec,
const NameValuePairs *selectors,
const WsmanClient *cl)
{
if(!cl)
{
throw CIMClassException("A NULL WsmanClient pointer was provided.", WSMAN_MISSING_INPUT);
}
vector<string> tmp;
cl->Enumerate(T::CLASS_URI, tmp, selectors);
for(unsigned int i = 0; i < tmp.size(); i++)
{
vec.push_back(tmp[i]);
}
}
template<class T>
void CimClass::Enumerate(CimClassContainer<T> &container,
const NameValuePairs *selectors,
const WsmanClient *cl)
{
if(!cl)
{
throw CIMClassException("A NULL WsmanClient pointer was provided.", WSMAN_MISSING_INPUT);
}
vector<string > tmp;
cl->Enumerate(T::CLASS_URI, tmp, selectors);
for(unsigned int i = 0; i < tmp.size(); i++)
{
container.AddCimClass(tmp[i]);
}
}
#endif