标签:style http color io os ar for 2014 art
1. [代码]SharpEvent.hpp
/*
* SharpEvent.hpp
*
* Created on: 2014-5-5
* Author: leoking
* Copyright: This file is published under BSD license.
*
Copyright (c) <2014>, <leoking>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SHARPEVENT_HPP_
#define SHARPEVENT_HPP_
#include <list>
#include <stdexcept>
namespace SharpEvent
{
template<typename SenderT, typename EventArgsT>
class CEventHandler
{
public:
typedef SenderT SenderType;
typedef EventArgsT EventArgsType;
private:
struct holder
{
virtual ~holder() {}
virtual holder* clone() const = 0;
virtual bool equals(const holder*) const = 0;
virtual void invoke(SenderType, EventArgsType) const = 0;
};
struct fholder : public holder
{
typedef void (*function)(SenderType, EventArgsType);
function func;
fholder(function f) : func(f) {}
virtual holder* clone() const { return new fholder(func); }
virtual bool equals(const holder* rhs) const {
const fholder* p = dynamic_cast<const fholder*>(rhs);
if (p)
return func == p->func;
else
return false;
}
virtual void invoke(SenderType a0, EventArgsType a1) const { (*func)(a0, a1); }
};
template<typename C>
struct mholder : public holder
{
typedef C concept_t;
typedef void (concept_t::*function)(SenderType, EventArgsType);
concept_t& obj;
function func;
mholder(concept_t* o, function f) : obj(*o), func(f) {}
virtual holder* clone() const { return new mholder(&obj, func); }
virtual bool equals(const holder* rhs) const {
const mholder* p = dynamic_cast<const mholder*>(rhs);
if (p)
return &obj == &(p->obj) && func == p->func;
else
return false;
}
virtual void invoke(SenderType a0, EventArgsType a1) const { (obj.*func)(a0, a1); }
};
holder* h;
public:
CEventHandler() : h(0) {}
CEventHandler(void(*function)(SenderType, EventArgsType)) : h(0) {
if (function)
h = new fholder(function);
}
template<typename Concept>
CEventHandler(Concept* object, void(Concept::*function)(SenderType, EventArgsType)) : h(0) {
if (object && function)
h = new mholder<Concept>(object, function);
}
CEventHandler(const CEventHandler& rhs) : h(0) {
if (rhs.h)
h = rhs.h->clone();
}
~CEventHandler() { delete h; }
void Assign(void(*function)(SenderType, EventArgsType)) {
Clear();
if (function)
h = new fholder(function);
}
template<typename Concept>
void Assign(Concept* object, void(Concept::*function)(SenderType, EventArgsType)) {
Clear();
if (object && function)
h = new mholder<Concept>(object, function);
}
void Assign(const CEventHandler& rhs) {
if (Equals(rhs))
return;
Clear();
if (rhs.h)
h = rhs.h->clone();
}
void Clear() {
delete h;
h = 0;
}
bool Empty() const { return h == 0; }
bool Equals(const CEventHandler& rhs) const {
if (h && rhs.h)
return h->equals(rhs.h);
return false;
}
void Invoke(SenderType sender, EventArgsType e) const {
if (h)
h->invoke(sender, e);
else
throw std::runtime_error("Empty EventHandler invoked.");
}
CEventHandler& operator=(const CEventHandler& rhs) { Assign(rhs); return *this; }
bool operator==(const CEventHandler& rhs) { return Equals(rhs); }
void operator()(SenderType sender, EventArgsType e) const { Invoke(sender, e); }
};
template<typename DelegateType>
class CEvent
{
public:
typedef DelegateType EventHandlerType;
typedef typename EventHandlerType::SenderType SenderType;
typedef typename EventHandlerType::EventArgsType EventArgsType;
CEvent() {}
void operator+=(EventHandlerType handler) { handlers.push_front(handler); handlers.unique(); }
void operator-=(EventHandlerType handler) { handlers.remove(handler); }
void operator()(SenderType sender, EventArgsType e) {
for (typename std::list<EventHandlerType>::const_iterator it = handlers.begin();
it != handlers.end(); it++)
{
EventHandlerType handler = *it;
handler.Invoke(sender, e);
}
}
private:
std::list<EventHandlerType> handlers;
private:
CEvent(const CEvent&);
void operator=(const CEvent&);
};
}
#endif /* SHARPEVENT_HPP_ */
2. [代码]SharpProperty.hpp
/*
* SharpProperty.hpp
*
* Created on: 2014-5-8
* Author: leoking
* Copyright: This file is published under BSD license.
*
Copyright (c) <2014>, <leoking>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SHARPPROPERTY_HPP_
#define SHARPPROPERTY_HPP_
#include <stdexcept>
namespace SharpProperty
{
template<typename T>
class CGetter
{
public:
typedef T ValueType;
private:
struct holder
{
virtual ~holder() {}
virtual holder* clone() const = 0;