1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-08 20:58:17 +03:00

feature #407: Moved implementation of ObjectCollection to cc file

This commit is contained in:
Ruben S. Montero 2011-06-03 12:11:33 +02:00
parent 72435cd453
commit 3cf5d5e7cd
3 changed files with 114 additions and 74 deletions

View File

@ -19,6 +19,8 @@
#include <set>
#include "PoolObjectSQL.h"
using namespace std;
/**
@ -29,8 +31,7 @@ class ObjectCollection
public:
ObjectCollection(const string& _collection_name)
:collection_name(_collection_name)
{};
:collection_name(_collection_name){};
~ObjectCollection(){};
@ -73,37 +74,7 @@ protected:
*
* @return 0 on success, -1 otherwise
*/
int from_xml_node(const xmlNodePtr node)
{
ObjectXML xml(node);
int rc = 0;
int id;
vector<string> values;
vector<string>::iterator it;
istringstream iss;
string xpath_expr = "/" + collection_name + "/ID";
values = xml[xpath_expr.c_str()];
for ( it = values.begin() ; it < values.end(); it++ )
{
iss.str(*it);
iss >> dec >> id;
if ( iss.fail() )
{
rc = -1;
}
else
{
collection_set.insert(id);
}
}
return rc;
};
int from_xml_node(const xmlNodePtr node);
/**
* Function to print the Collection object into a string in
@ -111,24 +82,7 @@ protected:
* @param xml the resulting XML string
* @return a reference to the generated string
*/
string& to_xml(string& xml) const
{
ostringstream oss;
set<int>::iterator it;
oss << "<" << collection_name << ">";
for ( it = collection_set.begin(); it != collection_set.end(); it++ )
{
oss << "<ID>" << *it << "</ID>";
}
oss << "</" << collection_name << ">";
xml = oss.str();
return xml;
};
string& to_xml(string& xml) const;
/**
* Adds an ID to the set.
@ -136,19 +90,7 @@ protected:
*
* @return 0 on success, -1 if the ID was already in the set
*/
int add_collection_id(int id)
{
pair<set<int>::iterator,bool> ret;
ret = collection_set.insert(id);
if( !ret.second )
{
return -1;
}
return 0;
};
int add_collection_id(int id);
/**
* Deletes an ID from the set.
@ -156,15 +98,7 @@ protected:
*
* @return 0 on success, -1 if the ID was not in the set
*/
int del_collection_id(int id)
{
if( collection_set.erase(id) != 1 )
{
return -1;
}
return 0;
};
int del_collection_id(int id);
/**
* Returns a copy of the IDs set

View File

@ -0,0 +1,105 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* Licensed 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. */
/* -------------------------------------------------------------------------- */
#include "ObjectCollection.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectCollection::from_xml_node(const xmlNodePtr node)
{
ObjectXML xml(node);
int rc = 0;
int id;
vector<string> values;
vector<string>::iterator it;
istringstream iss;
string xpath_expr = "/" + collection_name + "/ID";
values = xml[xpath_expr.c_str()];
for ( it = values.begin() ; it < values.end(); it++ )
{
iss.str(*it);
iss >> dec >> id;
if ( iss.fail() )
{
rc = -1;
}
else
{
collection_set.insert(id);
}
}
return rc;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& ObjectCollection::to_xml(string& xml) const
{
ostringstream oss;
set<int>::iterator it;
oss << "<" << collection_name << ">";
for ( it = collection_set.begin(); it != collection_set.end(); it++ )
{
oss << "<ID>" << *it << "</ID>";
}
oss << "</" << collection_name << ">";
xml = oss.str();
return xml;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectCollection::add_collection_id(int id)
{
pair<set<int>::iterator,bool> ret;
ret = collection_set.insert(id);
if( !ret.second )
{
return -1;
}
return 0;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectCollection::del_collection_id(int id)
{
if( collection_set.erase(id) != 1 )
{
return -1;
}
return 0;
};

View File

@ -23,7 +23,8 @@ lib_name='nebula_pool'
# Sources to generate the library
source_files=[
'PoolSQL.cc',
'PoolObjectSQL.cc'
'PoolObjectSQL.cc',
'ObjectCollection.cc'
]
# Build library