mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-17 03:03:23 +02:00
- introduced Poco::Dynamic namespace
- moved/renamed: Poco::DynamicAny => Poco::Dynamic::Var Poco::DynamicAny typedef for backward compatibility (deprecated) DynamicAny.h forwarding header for backward compatibility (deprecated) Poco::DynamicAnyHolder => Poco::Dynamic::VarHolder DynamicAnyHolder.h forwarding header for backward compatibility (deprecated) Poco::DynamicStruct => Poco::Dynamic::Struct Poco::DynamicStruct typedef for backward compatibility (deprecated) DynamicStruct.h forwarding header for backward compatibility (deprecated) - changed FastMutex to Mutex in Data::SessionPool (deadlocking)
This commit is contained in:
407
Foundation/include/Poco/Dynamic/Struct.h
Normal file
407
Foundation/include/Poco/Dynamic/Struct.h
Normal file
@@ -0,0 +1,407 @@
|
||||
//
|
||||
// Struct.h
|
||||
//
|
||||
// $Id: //poco/Main/Foundation/include/Poco/Struct.h#9 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Dynamic
|
||||
// Module: Struct
|
||||
//
|
||||
// Definition of the Struct class.
|
||||
//
|
||||
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#ifndef Foundation_Struct_INCLUDED
|
||||
#define Foundation_Struct_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/Dynamic/Var.h"
|
||||
#include "Poco/Dynamic/VarHolder.h"
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Dynamic {
|
||||
|
||||
|
||||
class Foundation_API Struct
|
||||
/// Struct allows to define a named collection of Var objects.
|
||||
{
|
||||
public:
|
||||
typedef std::map<std::string, Var> Data;
|
||||
typedef Data::iterator Iterator;
|
||||
typedef Data::const_iterator ConstIterator;
|
||||
|
||||
Struct();
|
||||
/// Creates an empty Struct
|
||||
|
||||
Struct(const Data &val);
|
||||
/// Creates the Struct from the given value.
|
||||
|
||||
virtual ~Struct();
|
||||
/// Destroys the Struct.
|
||||
|
||||
Var& operator [] (const std::string& name);
|
||||
/// Returns the Var with the given name, creates an entry if not found.
|
||||
|
||||
const Var& operator [] (const std::string& name) const;
|
||||
/// Returns the Var with the given name, throws a
|
||||
/// NotFoundException if the data member is not found.
|
||||
|
||||
bool contains(const std::string& name) const;
|
||||
/// Returns true if the Struct contains a member with the given
|
||||
/// name
|
||||
|
||||
Iterator find(const std::string& name);
|
||||
/// Returns an iterator, pointing to the <name,Var> pair containing
|
||||
/// the element, or it returns end() if the member was not found
|
||||
|
||||
ConstIterator find(const std::string& name) const;
|
||||
/// Returns a const iterator, pointing to the <name,Var> pair containing
|
||||
/// the element, or it returns end() if the member was not found
|
||||
|
||||
Iterator end();
|
||||
/// Returns the end iterator for the Struct
|
||||
|
||||
ConstIterator end() const;
|
||||
/// Returns the end const iterator for the Struct
|
||||
|
||||
Iterator begin();
|
||||
/// Returns the begin iterator for the Struct
|
||||
|
||||
ConstIterator begin() const;
|
||||
/// Returns the begin const iterator for the Struct
|
||||
|
||||
std::pair<Struct::Iterator, bool> insert(const std::string& key, const Var& value);
|
||||
/// Inserts a <name, Var> pair into the Struct,
|
||||
/// returns a pair containing the iterator and a boolean which
|
||||
/// indicates success or not (is true, when insert succeeded, false,
|
||||
/// when already another element was present, in this case Iterator
|
||||
/// points to that other element)
|
||||
|
||||
std::pair<Struct::Iterator, bool> insert(const Struct::Data::value_type& aPair);
|
||||
/// Inserts a <name, Var> pair into the Struct,
|
||||
/// returns a pair containing the iterator and a boolean which
|
||||
/// indicates success or not (is true, when insert succeeded, false,
|
||||
/// when already another element was present, in this case Iterator
|
||||
/// points to that other element)
|
||||
|
||||
Struct::Data::size_type erase(const std::string& key);
|
||||
/// Erases the element if found, returns number of elements deleted
|
||||
|
||||
void erase(Struct::Iterator it);
|
||||
/// Erases the element at the given position
|
||||
|
||||
bool empty() const;
|
||||
/// Returns true if the Struct doesn't contain any members
|
||||
|
||||
Struct::Data::size_type size() const;
|
||||
/// Returns the number of members the Struct contains
|
||||
|
||||
std::set<std::string> members() const;
|
||||
/// Returns a sorted collection containing all member names
|
||||
|
||||
private:
|
||||
Data _data;
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// inlines
|
||||
//
|
||||
inline Var& Struct::operator [] (const std::string& name)
|
||||
{
|
||||
return _data[name];
|
||||
}
|
||||
|
||||
|
||||
inline bool Struct::contains(const std::string& name) const
|
||||
{
|
||||
return find(name) != end();
|
||||
}
|
||||
|
||||
|
||||
inline Struct::Iterator Struct::find(const std::string& name)
|
||||
{
|
||||
return _data.find(name);
|
||||
}
|
||||
|
||||
|
||||
inline Struct::ConstIterator Struct::find(const std::string& name) const
|
||||
{
|
||||
return _data.find(name);
|
||||
}
|
||||
|
||||
|
||||
inline Struct::Iterator Struct::end()
|
||||
{
|
||||
return _data.end();
|
||||
}
|
||||
|
||||
|
||||
inline Struct::ConstIterator Struct::end() const
|
||||
{
|
||||
return _data.end();
|
||||
}
|
||||
|
||||
|
||||
inline Struct::Iterator Struct::begin()
|
||||
{
|
||||
return _data.begin();
|
||||
}
|
||||
|
||||
|
||||
inline Struct::ConstIterator Struct::begin() const
|
||||
{
|
||||
return _data.begin();
|
||||
}
|
||||
|
||||
|
||||
inline std::pair<Struct::Iterator, bool> Struct::insert(const std::string& key, const Var& value)
|
||||
{
|
||||
return insert(std::make_pair(key, value));
|
||||
}
|
||||
|
||||
|
||||
inline std::pair<Struct::Iterator, bool> Struct::insert(const Struct::Data::value_type& aPair)
|
||||
{
|
||||
return _data.insert(aPair);
|
||||
}
|
||||
|
||||
|
||||
inline Struct::Data::size_type Struct::erase(const std::string& key)
|
||||
{
|
||||
return _data.erase(key);
|
||||
}
|
||||
|
||||
|
||||
inline void Struct::erase(Struct::Iterator it)
|
||||
{
|
||||
_data.erase(it);
|
||||
}
|
||||
|
||||
|
||||
inline bool Struct::empty() const
|
||||
{
|
||||
return _data.empty();
|
||||
}
|
||||
|
||||
|
||||
inline Struct::Data::size_type Struct::size() const
|
||||
{
|
||||
return _data.size();
|
||||
}
|
||||
|
||||
|
||||
template <>
|
||||
class VarHolderImpl<Struct>: public VarHolder
|
||||
{
|
||||
public:
|
||||
VarHolderImpl(const Struct& val): _val(val)
|
||||
{
|
||||
}
|
||||
|
||||
~VarHolderImpl()
|
||||
{
|
||||
}
|
||||
|
||||
const std::type_info& type() const
|
||||
{
|
||||
return typeid(Struct);
|
||||
}
|
||||
|
||||
void convert(Int8& val) const
|
||||
{
|
||||
throw BadCastException("Cannot cast Struct type to Int8");
|
||||
}
|
||||
|
||||
void convert(Int16& val) const
|
||||
{
|
||||
throw BadCastException("Cannot cast Struct type to Int16");
|
||||
}
|
||||
|
||||
void convert(Int32& val) const
|
||||
{
|
||||
throw BadCastException("Cannot cast Struct type to Int32");
|
||||
}
|
||||
|
||||
void convert(Int64& val) const
|
||||
{
|
||||
throw BadCastException("Cannot cast Struct type to Int64");
|
||||
}
|
||||
|
||||
void convert(UInt8& val) const
|
||||
{
|
||||
throw BadCastException("Cannot cast Struct type to UInt8");
|
||||
}
|
||||
|
||||
void convert(UInt16& val) const
|
||||
{
|
||||
throw BadCastException("Cannot cast Struct type to UInt16");
|
||||
}
|
||||
|
||||
void convert(UInt32& val) const
|
||||
{
|
||||
throw BadCastException("Cannot cast Struct type to UInt32");
|
||||
}
|
||||
|
||||
void convert(UInt64& val) const
|
||||
{
|
||||
throw BadCastException("Cannot cast Struct type to UInt64");
|
||||
}
|
||||
|
||||
void convert(bool& val) const
|
||||
{
|
||||
throw BadCastException("Cannot cast Struct type to bool");
|
||||
}
|
||||
|
||||
void convert(float& val) const
|
||||
{
|
||||
throw BadCastException("Cannot cast Struct type to float");
|
||||
}
|
||||
|
||||
void convert(double& val) const
|
||||
{
|
||||
throw BadCastException("Cannot cast Struct type to double");
|
||||
}
|
||||
|
||||
void convert(char& val) const
|
||||
{
|
||||
throw BadCastException("Cannot cast Struct type to char");
|
||||
}
|
||||
|
||||
void convert(std::string& val) const
|
||||
{
|
||||
// Serialize in JSON format: equals an object
|
||||
|
||||
// JSON format definition: { string ':' value } string:value pair n-times, sep. by ','
|
||||
val.append("{ ");
|
||||
Struct::ConstIterator it = _val.begin();
|
||||
Struct::ConstIterator itEnd = _val.end();
|
||||
if (!_val.empty())
|
||||
{
|
||||
Var key(it->first);
|
||||
appendJSONString(val, key);
|
||||
val.append(" : ");
|
||||
appendJSONString(val, it->second);
|
||||
++it;
|
||||
}
|
||||
for (; it != itEnd; ++it)
|
||||
{
|
||||
val.append(", ");
|
||||
Var key(it->first);
|
||||
appendJSONString(val, key);
|
||||
val.append(" : ");
|
||||
appendJSONString(val, it->second);
|
||||
}
|
||||
val.append(" }");
|
||||
}
|
||||
|
||||
void convert(Poco::DateTime&) const
|
||||
{
|
||||
throw BadCastException("Struct -> Poco::DateTime");
|
||||
}
|
||||
|
||||
void convert(Poco::LocalDateTime&) const
|
||||
{
|
||||
throw BadCastException("Struct -> Poco::LocalDateTime");
|
||||
}
|
||||
|
||||
void convert(Poco::Timestamp&) const
|
||||
{
|
||||
throw BadCastException("Struct -> Poco::Timestamp");
|
||||
}
|
||||
|
||||
VarHolder* clone() const
|
||||
{
|
||||
return new VarHolderImpl(_val);
|
||||
}
|
||||
|
||||
const Struct& value() const
|
||||
{
|
||||
return _val;
|
||||
}
|
||||
|
||||
bool isArray() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isStruct() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isInteger() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isSigned() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isNumeric() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isString() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Var& operator [] (const std::string& name)
|
||||
{
|
||||
return _val[name];
|
||||
}
|
||||
|
||||
const Var& operator [] (const std::string& name) const
|
||||
{
|
||||
return _val[name];
|
||||
}
|
||||
|
||||
private:
|
||||
Struct _val;
|
||||
};
|
||||
|
||||
|
||||
} // namespace Dynamic
|
||||
|
||||
|
||||
//@ deprecated
|
||||
typedef Dynamic::Struct DynamicStruct;
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
|
||||
#endif // Foundation_Struct_INCLUDED
|
Reference in New Issue
Block a user