extensions to template

This commit is contained in:
Peter Schojer 2008-09-16 08:15:36 +00:00
parent 64ab34cc6e
commit 8f9807daf3
6 changed files with 424 additions and 16 deletions

View File

@ -63,6 +63,7 @@
#include "Poco/WebWidgets/ExtJS/ProgressIndicatorRenderer.h"
#include "Poco/WebWidgets/ExtJS/HTMLRenderer.h"
#include "Poco/WebWidgets/ExtJS/DynamicCodeLoaderRenderer.h"
#include "Poco/WebWidgets/ExtJS/TemplateRenderer.h"
#include "Poco/WebWidgets/Label.h"
#include "Poco/WebWidgets/Page.h"
@ -96,6 +97,7 @@
#include "Poco/WebWidgets/WebApplication.h"
#include "Poco/WebWidgets/RequestHandler.h"
#include "Poco/WebWidgets/ProgressIndicator.h"
#include "Poco/WebWidgets/Template.h"
#include "Poco/String.h"
#include "Poco/StringTokenizer.h"
@ -140,6 +142,7 @@ void Utility::initialize(LookAndFeel::Ptr ptr)
ptr->registerRenderer(typeid(ProgressIndicator), new ProgressIndicatorRenderer());
ptr->registerRenderer(typeid(HTML),new HTMLRenderer());
ptr->registerRenderer(typeid(DynamicCodeLoader),new DynamicCodeLoaderRenderer());
ptr->registerRenderer(typeid(Template),new TemplateRenderer());
}

View File

@ -241,6 +241,10 @@
RelativePath=".\include\Poco\WebWidgets\Event.h"
>
</File>
<File
RelativePath=".\include\Poco\WebWidgets\Getter.h"
>
</File>
<File
RelativePath=".\include\Poco\WebWidgets\JavaScriptEvent.h"
>

View File

@ -240,6 +240,10 @@
RelativePath=".\include\Poco\WebWidgets\Event.h"
>
</File>
File
RelativePath=".\include\Poco\WebWidgets\Getter.h"
>
</File>
<File
RelativePath=".\include\Poco\WebWidgets\JavaScriptEvent.h"
>

View File

@ -0,0 +1,144 @@
//
// Getter.h
//
// $Id: //poco/Main/WebWidgets/include/Poco/WebWidgets/Getter.h#2 $
//
// Library: WebWidgets
// Package: Core
// Module: Getter
//
// Definition of the Getter class.
//
// Copyright (c) 2008, 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 WebWidgets_Getter_INCLUDED
#define WebWidgets_Getter_INCLUDED
#include "Poco/WebWidgets/WebWidgets.h"
#include "Poco/RefCountedObject.h"
#include "Poco/AutoPtr.h"
#include "Poco/Any.h"
#include "Poco/MetaProgramming.h"
namespace Poco {
namespace WebWidgets {
class WebWidgets_API Getter: public Poco::RefCountedObject
{
public:
typedef Poco::AutoPtr<Getter> Ptr;
public:
Getter()
{
}
virtual Poco::Any get() const = 0;
};
template <class TObj, class TArg>
class GetterImpl: public Getter
{
public:
typedef TArg (TObj::*NotifyMethod)() const;
GetterImpl(TObj* obj, NotifyMethod method):
_object(obj),
_method(method)
{
}
TArg operator()() const
{
return (_object->*_method)();
}
Poco::Any get() const
{
return this->operator();
}
protected:
TObj* _object;
NotifyMethod _method;
};
template <class TObj, class TArg, class TArgIn>
class GetterImpl2: public Getter
{
public:
typedef TArg (TObj::*NotifyMethod)(TArgIn) const;
typedef typename TypeWrapper<TArgIn>::TYPE TArgInValue;
GetterImpl2(TObj* obj, NotifyMethod method, TArgIn in):
_object(obj),
_method(method),
_value(in)
{
}
TArg operator()() const
{
return (_object->*_method)(_value);
}
Poco::Any get() const
{
return this->operator();
}
protected:
TObj* _object;
NotifyMethod _method;
TArgInValue _value;
};
template <class TObj, class TArg, class TArgIn>
Getter* getter(const TObj* object, TArg (TObj::*method)(TArgIn in) const)
/// "Constructor" function for a Getter.
{
return new GetterImpl2(object, method, in);
}
template <class TObj, class TArg>
Getter* getter(const TObj* object, TArg (TObj::*method)() const)
/// "Constructor" function for a Getter.
{
return new GetterImpl(object, method);
}
} } // namespace Poco::WebWidgets
#endif // WebWidgets_Getter_INCLUDED

View File

@ -40,7 +40,8 @@
#define WebWidgets_Template_INCLUDED
#include "Poco/WebWidgets/Renderable.h"
#include "Poco/WebWidgets/View.h"
#include "Poco/WebWidgets/Getter.h"
#include "Poco/FIFOEvent.h"
#include "Poco/Any.h"
#include <ostream>
@ -53,10 +54,11 @@ namespace WebWidgets {
class RenderContext;
class WebWidgets_API Template: public Renderable
class WebWidgets_API Template: public View
/// A JavaScript template class: Defines a template string which contains wildcards of the form %0, %1 %2 ...
/// You can bind values to the wildcards. Values must be of type:
/// - string, int, double, float, char, bool, Poco::WebWidgets::Renderable, DateTime
/// properties inherited by View (like width, height) are ignored
{
public:
typedef Poco::AutoPtr<Template> Ptr;
@ -73,6 +75,25 @@ public:
{
bind(num, val);
}
template<typename T>
void setValue(std::size_t num, Poco::AutoPtr<T> val)
{
bindPtr(num, val);
}
template<typename T>
void setValue(std::size_t num, Poco::SharedPtr<T> val)
{
bindPtr(num, val);
}
template<typename T>
const T& getValue(std::size_t num) const
{
return Poco::RefAnyCast<T>(_values[num]);
}
const std::string& templateStr() const;
/// Returns the templatestr
@ -92,6 +113,8 @@ protected:
std::size_t detectMaxWildCard() const;
void bind(std::size_t num, const std::vector<Poco::Any>& val);
void bind(std::size_t num, int val);
void bind(std::size_t num, float val);
void bind(std::size_t num, double val);
@ -101,12 +124,40 @@ protected:
void bind(std::size_t num, const char* pVal);
void bind(std::size_t num, Renderable* pVal);
void bind(std::size_t num, const Any& val);
void bindPtr(std::size_t num, Poco::AutoPtr<Renderable> pRend);
void bindPtr(std::size_t num, Poco::AutoPtr<Getter> pRend);
void bindPtr(std::size_t num, Poco::SharedPtr<std::vector<Poco::Any> > pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<std::vector<float> > pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<std::vector<double> > pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<std::vector<char> > pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<std::vector<bool> > pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<std::vector<Renderable::Ptr> > pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<std::vector<std::string> > pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<std::vector<Getter::Ptr> > pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<int> pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<float> pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<double> pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<char> pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<bool> pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<std::string> pVal);
void bindPtr(std::size_t num, Poco::SharedPtr<Poco::Any> pVal);
void write(const RenderContext& ctx, std::ostream& out, const Any& val) const;
template<typename T>
void writeVector(const RenderContext& ctx, std::ostream& out, const std::vector<T>& val) const
{
for (std::size_t i = 0; i < val.size(); ++i)
{
if (i > 0)
out << ",";
write(ctx, out, val[i]);
}
}
private:
std::string _templateStr;
std::size_t _maxWildCard;
std::vector<Poco::Any> _values;
std::vector<Poco::Any> _values;
};

View File

@ -43,7 +43,7 @@ namespace WebWidgets {
Template::Template(const std::string& templateStr):
Renderable(typeid(Template)),
View(typeid(Template)),
_templateStr(templateStr),
_maxWildCard(0),
_values()
@ -54,7 +54,7 @@ Template::Template(const std::string& templateStr):
Template::Template(const std::string& templateStr, const std::vector<Poco::Any>& values):
Renderable(typeid(Template)),
View(typeid(Template)),
_templateStr(templateStr),
_maxWildCard(values.size()),
_values()
@ -68,7 +68,7 @@ Template::Template(const std::string& templateStr, const std::vector<Poco::Any>&
Template::Template(const std::string& templateStr, const std::type_info& type):
Renderable(type),
View(type),
_templateStr(templateStr),
_values()
{
@ -103,8 +103,8 @@ std::size_t Template::detectMaxWildCard() const
else
{
// we read %123%
if (curVal > maxVal)
maxVal = curVal;
if (curVal+1 > maxVal)
maxVal = curVal+1;
// we remain within inPercent
curVal = 0;
cnt = 0;
@ -119,8 +119,8 @@ std::size_t Template::detectMaxWildCard() const
else
{
inPercent = false;
if (curVal > maxVal)
maxVal = curVal;
if (curVal+1 > maxVal)
maxVal = curVal+1;
// we remain within inPercent
curVal = 0;
cnt = 0;
@ -177,11 +177,13 @@ void Template::parse(const RenderContext& ctx, std::ostream& out) const
}
else
{
// we read neither percent nor number
inPercent = false;
if (cnt > 0)
write(ctx, out, _values[curVal]);
else
out << "%";
out.put(c);
curVal = 0;
cnt = 0;
}
@ -246,6 +248,7 @@ void Template::bind(std::size_t num, const char* pVal)
void Template::bind(std::size_t num, Renderable* pVal)
{
poco_check_ptr (pVal);
_values[num] = pVal;
}
@ -253,9 +256,20 @@ void Template::bind(std::size_t num, Renderable* pVal)
void Template::bind(std::size_t num, const Any& val)
{
const std::type_info& type = val.type();
Renderable* pRend = AnyCast<Renderable*>(val);
if (pRend)
bind(num, pRend);
if (type == typeid(Renderable*))
{
Any cpy = val;
Renderable* pRend = AnyCast<Renderable*>(cpy);
if (pRend)
bind(num, pRend);
}
else if (type == typeid(Renderable::Ptr))
{
Any cpy = val;
Renderable::Ptr pRend = AnyCast<Renderable::Ptr>(cpy);
if (pRend)
bindPtr(num, pRend);
}
else if (type == typeid(std::string))
bind(num, RefAnyCast<std::string>(val));
else if (type == typeid(const char*))
@ -268,6 +282,42 @@ void Template::bind(std::size_t num, const Any& val)
bind(num, AnyCast<double>(val));
else if (type == typeid(bool))
bind(num, AnyCast<bool>(val));
else if (type == typeid(std::vector<Poco::Any>))
bind(num, RefAnyCast<std::vector<Poco::Any> >(val));
else if (type == typeid(Poco::AutoPtr<Renderable>))
bindPtr(num, RefAnyCast<Poco::AutoPtr<Renderable> >(val));
else if (type == typeid(Poco::AutoPtr<Getter>))
bindPtr(num, RefAnyCast<Poco::AutoPtr<Getter> >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<Poco::Any> >))
bindPtr(num, RefAnyCast<Poco::SharedPtr<std::vector<Poco::Any> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<float> >))
bindPtr(num, RefAnyCast<Poco::SharedPtr<std::vector<float> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<double> >))
bindPtr(num, RefAnyCast<Poco::SharedPtr<std::vector<double> >>(val));
else if (type == typeid(Poco::SharedPtr<std::vector<char> >))
bindPtr(num, RefAnyCast<Poco::SharedPtr<std::vector<char> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<bool> >))
bindPtr(num, RefAnyCast<Poco::SharedPtr<std::vector<bool> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<Renderable::Ptr> >))
bindPtr(num, RefAnyCast<Poco::SharedPtr<std::vector<Renderable::Ptr> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<std::string> >))
bindPtr(num, RefAnyCast<Poco::SharedPtr<std::vector<std::string> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<Getter::Ptr> >))
bindPtr(num, RefAnyCast<Poco::SharedPtr<std::vector<Getter::Ptr> > >(val));
else if (type == typeid(Poco::SharedPtr<int>))
bindPtr(num, RefAnyCast<Poco::SharedPtr<int> >(val));
else if (type == typeid(Poco::SharedPtr<float>))
bindPtr(num, RefAnyCast<Poco::SharedPtr<float> >(val));
else if (type == typeid(Poco::SharedPtr<double>))
bindPtr(num, RefAnyCast<Poco::SharedPtr<double> >(val));
else if (type == typeid(Poco::SharedPtr<char>))
bindPtr(num, RefAnyCast<Poco::SharedPtr<char> >(val));
else if (type == typeid(Poco::SharedPtr<bool>))
bindPtr(num, RefAnyCast<Poco::SharedPtr<bool> >(val));
else if (type == typeid(Poco::SharedPtr<std::string>))
bindPtr(num, RefAnyCast<Poco::SharedPtr<std::string> >(val));
else if (type == typeid(Poco::SharedPtr<Poco::Any>))
bindPtr(num, RefAnyCast<Poco::SharedPtr<Poco::Any> >(val));
else /* if (type == typeid(char)) */
bind(num, AnyCast<char>(val));
}
@ -278,9 +328,20 @@ void Template::write(const RenderContext& ctx, std::ostream& out, const Any& val
if (val.empty())
return;
const std::type_info& type = val.type();
Renderable* pRend = AnyCast<Renderable*>(val);
if (pRend)
pRend->renderHead(ctx, out);
if (type == typeid(Renderable*))
{
Any cpy = val;
Renderable* pRend = AnyCast<Renderable*>(cpy);
if (pRend)
pRend->renderHead(ctx, out);
}
else if (type == typeid(Renderable::Ptr))
{
Any cpy = val;
Renderable::Ptr pRend = AnyCast<Renderable::Ptr>(cpy);
if (pRend)
pRend->renderHead(ctx, out);
}
else if (type == typeid(std::string))
out << RefAnyCast<std::string>(val);
else if (type == typeid(int))
@ -291,9 +352,150 @@ void Template::write(const RenderContext& ctx, std::ostream& out, const Any& val
out << AnyCast<double>(val);
else if (type == typeid(bool))
out << (AnyCast<bool>(val)?"true":"false");
else if (type == typeid(SharedPtr<std::string>))
out << *AnyCast<SharedPtr<std::string> >(val);
else if (type == typeid(SharedPtr<int>))
out << *AnyCast<SharedPtr<int> >(val);
else if (type == typeid(SharedPtr<float>))
out << *AnyCast<SharedPtr<float> >(val);
else if (type == typeid(SharedPtr<double>))
out << *AnyCast<SharedPtr<double> >(val);
else if (type == typeid(SharedPtr<bool>))
out << (*AnyCast<SharedPtr<bool> >(val)?"true":"false");
else if (type == typeid(Getter::Ptr))
{
Getter::Ptr pGet =AnyCast<Getter::Ptr>(val);
write(ctx, out, pGet->get());
}
else if (type == typeid(std::vector<Poco::Any>))
writeVector(ctx, out, RefAnyCast<std::vector<Poco::Any> >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<Poco::Any> >))
writeVector(ctx, out, *RefAnyCast<Poco::SharedPtr<std::vector<Poco::Any> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<float> >))
writeVector(ctx, out, *RefAnyCast<Poco::SharedPtr<std::vector<float> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<double> >))
writeVector(ctx, out, *RefAnyCast<Poco::SharedPtr<std::vector<double> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<char> >))
writeVector(ctx, out, *RefAnyCast<Poco::SharedPtr<std::vector<char> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<bool> >))
writeVector(ctx, out, *RefAnyCast<Poco::SharedPtr<std::vector<bool> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<Renderable::Ptr> >))
writeVector(ctx, out, *RefAnyCast<Poco::SharedPtr<std::vector<Renderable::Ptr> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<std::string> >))
writeVector(ctx, out, *RefAnyCast<Poco::SharedPtr<std::vector<std::string> > >(val));
else if (type == typeid(Poco::SharedPtr<std::vector<Getter::Ptr> >))
writeVector(ctx, out, *RefAnyCast<Poco::SharedPtr<std::vector<Getter::Ptr> > >(val));
else /* if (type == typeid(char)) */
out << AnyCast<char>(val);
}
void Template::bind(std::size_t num, const std::vector<Poco::Any>& val)
{
_values[num] = val;
}
void Template::bindPtr(std::size_t num, Poco::AutoPtr<Renderable> pRend)
{
_values[num] = pRend;
}
void Template::bindPtr(std::size_t num, Poco::AutoPtr<Getter> pRend)
{
_values[num] = pRend;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<std::vector<Poco::Any> > pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<std::vector<float> > pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<std::vector<double> > pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<std::vector<char> > pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<std::vector<bool> > pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<std::vector<Renderable::Ptr> > pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<std::vector<std::string> > pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<std::vector<Getter::Ptr> > pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<int> pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<float> pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<double> pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<char> pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<bool> pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<std::string> pVal)
{
_values[num] = pVal;
}
void Template::bindPtr(std::size_t num, Poco::SharedPtr<Poco::Any> pVal)
{
_values[num] = pVal;
}
} } // namespace Poco::WebWidgets