introduce place::error and badname

This commit is contained in:
qPCR4vir 2019-04-14 00:43:45 +02:00
parent 77dd467f73
commit 6b3c02b558
2 changed files with 60 additions and 47 deletions

View File

@ -79,6 +79,7 @@ namespace nana
{ {
struct implement; struct implement;
class field_interface class field_interface
{ {
field_interface(const field_interface&) = delete; field_interface(const field_interface&) = delete;
@ -103,6 +104,25 @@ namespace nana
virtual void _m_add_agent(const detail::place_agent&) = 0; virtual void _m_add_agent(const detail::place_agent&) = 0;
}; };
public: public:
class error :public ::std::invalid_argument
{
public:
error(::std::string what,
const place& plc,
std::string field = "unknown",
std::string::size_type pos = std::string::npos)
: std::invalid_argument(what),
owner_caption{ API::window_caption(plc.window_handle()).substr(0,80) },
field{ field },
div_text{ plc.div() },
pos{ pos }
{}
std::string owner_caption; ///< truncate caption (title) of the "placed" widget
std::string div_text;
std::string field; ///< posible field where the error ocurred.
std::string::size_type pos; ///< posible position in the div_text where the error ocurred. npos if unknown
};
/// reference to a field manipulator which refers to a field object created by place /// reference to a field manipulator which refers to a field object created by place
using field_reference = field_interface &; using field_reference = field_interface &;
@ -121,6 +141,10 @@ namespace nana
void div(std::string div_text); ///< Divides the attached widget into fields. void div(std::string div_text); ///< Divides the attached widget into fields.
const std::string& div() const noexcept; ///< Returns div-text that depends on fields status. const std::string& div() const noexcept; ///< Returns div-text that depends on fields status.
static bool check_field_name(const char* name) ///< must begin with _a-zA-Z
{
return name && (*name == '_' || (('a' <= *name && *name <= 'z') || ('A' <= *name && *name <= 'Z')));
}
void modify(const char* field_name, const char* div_text); ///< Modifies a specified field. void modify(const char* field_name, const char* div_text); ///< Modifies a specified field.
field_reference field(const char* name);///< Returns a field with the specified name. field_reference field(const char* name);///< Returns a field with the specified name.
@ -152,6 +176,8 @@ namespace nana
private: private:
implement * impl_; implement * impl_;
}; };
}//end namespace nana }//end namespace nana
#include <nana/pop_ignore_diagnostic> #include <nana/pop_ignore_diagnostic>

View File

@ -36,16 +36,16 @@
namespace nana namespace nana
{ {
namespace place_parts struct badname: place::error
{ {
//check the name explicit badname( ::std::string what,
void check_field_name(const char* name) const place& plc,
{ const char* name = "unknown",
if (*name && (*name != '_' && !(('a' <= *name && *name <= 'z') || ('A' <= *name && *name <= 'Z')))) std::string::size_type pos = std::string::npos)
throw std::invalid_argument(std::string("nana.place: bad field name '")+name+"'."); :place::error(what + ": bad field name '" + (name ? name : "nullptr") + "'.",
} plc, (name ? name : "nullptr"), pos)
}//end namespace place_parts {}
};
typedef place_parts::number_t number_t; typedef place_parts::number_t number_t;
typedef place_parts::repeated_array repeated_array; typedef place_parts::repeated_array repeated_array;
@ -73,7 +73,7 @@ namespace nana
return idstr_; return idstr_;
} }
const number_t& number() const const number_t& number() const noexcept
{ {
return number_; return number_;
} }
@ -3272,22 +3272,18 @@ namespace nana
void place::modify(const char* name, const char* div_text) void place::modify(const char* name, const char* div_text)
{ {
if (nullptr == div_text) if (nullptr == div_text)
throw std::invalid_argument("nana.place: invalid div-text"); throw error("nana::place.modify(): invalid div-text (nullptr)", *this);
if (nullptr == name) name = ""; if (! place::check_field_name(name) )
throw badname("nana::place.modify()", *this, name);
//check the name, it throws std::invalid_argument
//if name violate the naming convention.
place_parts::check_field_name(name);
auto div_ptr = impl_->search_div_name(impl_->root_division.get(), name); auto div_ptr = impl_->search_div_name(impl_->root_division.get(), name);
if (!div_ptr) if (!div_ptr)
{ {
std::string what = "nana::place: field '"; std::string what = "nana::place.modify(): field '";
what += name; what += name;
what += "' is not found."; what += "' was not found.";
throw error(what, *this, name);
throw std::invalid_argument(what);
} }
std::unique_ptr<implement::division>* replaced = nullptr; std::unique_ptr<implement::division>* replaced = nullptr;
@ -3342,7 +3338,7 @@ namespace nana
} }
} }
} }
catch (...) catch (...) /// todo throw error !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{ {
replaced->swap(impl_->tmp_replaced); replaced->swap(impl_->tmp_replaced);
throw; throw;
@ -3351,12 +3347,8 @@ namespace nana
place::field_reference place::field(const char* name) place::field_reference place::field(const char* name)
{ {
if (nullptr == name) if (!place::check_field_name(name))
name = ""; throw badname("nana::place.field()", *this, name);
//check the name, it throws std::invalid_argument
//if name violate the naming convention.
place_parts::check_field_name(name);
//get the field with the specified name. If no such field with specified name //get the field with the specified name. If no such field with specified name
//then create one. //then create one.
@ -3372,7 +3364,8 @@ namespace nana
if (div) if (div)
{ {
if (div->field && (div->field != p)) if (div->field && (div->field != p))
throw std::runtime_error(std::string("nana.place: unexpected error, the division attaches an unexpected field: ") + name); throw error(std::string("nana::place.field(): unexpected error, the division attaches an unexpected field: ") + name,
*this, name);
div->field = p; div->field = p;
p->attached = div; p->attached = div;
@ -3449,10 +3442,8 @@ namespace nana
void place::field_visible(const char* name, bool vsb) void place::field_visible(const char* name, bool vsb)
{ {
if (!name) name = ""; if (!place::check_field_name(name))
throw badname("set nana::place.field_visible()", *this, name);
//May throw std::invalid_argument
place_parts::check_field_name(name);
auto div = impl_->search_div_name(impl_->root_division.get(), name); auto div = impl_->search_div_name(impl_->root_division.get(), name);
if (div) if (div)
@ -3464,10 +3455,8 @@ namespace nana
bool place::field_visible(const char* name) const bool place::field_visible(const char* name) const
{ {
if (!name) name = ""; if (!place::check_field_name(name))
throw badname("get nana::place.field_visible()", *this, name);
//May throw std::invalid_argument
place_parts::check_field_name(name);
auto div = impl_->search_div_name(impl_->root_division.get(), name); auto div = impl_->search_div_name(impl_->root_division.get(), name);
return (div && div->visible); return (div && div->visible);
@ -3475,10 +3464,8 @@ namespace nana
void place::field_display(const char* name, bool dsp) void place::field_display(const char* name, bool dsp)
{ {
if (!name) name = ""; if (!place::check_field_name(name))
throw badname("set nana::place.field_display()", *this, name);
//May throw std::invalid_argument
place_parts::check_field_name(name);
auto div = impl_->search_div_name(impl_->root_division.get(), name); auto div = impl_->search_div_name(impl_->root_division.get(), name);
if (div) if (div)
@ -3491,10 +3478,8 @@ namespace nana
bool place::field_display(const char* name) const bool place::field_display(const char* name) const
{ {
if (!name) name = ""; if (!place::check_field_name(name))
throw badname("get nana::place.field_display()", *this, name);
//May throw std::invalid_argument
place_parts::check_field_name(name);
auto div = impl_->search_div_name(impl_->root_division.get(), name); auto div = impl_->search_div_name(impl_->root_division.get(), name);
return (div && div->display); return (div && div->display);
@ -3531,9 +3516,8 @@ namespace nana
place& place::dock(const std::string& name, std::string factory_name, std::function<std::unique_ptr<widget>(window)> factory) place& place::dock(const std::string& name, std::string factory_name, std::function<std::unique_ptr<widget>(window)> factory)
{ {
//check the name, it throws std::invalid_argument if (!place::check_field_name(name.data()))
//if name violate the naming convention. throw badname("get nana::place.field_display()", *this, name.data());
place_parts::check_field_name(name.data());
auto & dock_ptr = impl_->docks[name]; auto & dock_ptr = impl_->docks[name];
if (!dock_ptr) if (!dock_ptr)
@ -3601,6 +3585,9 @@ namespace nana
return nullptr; return nullptr;
} }
//end class place //end class place
//class place::error
}//end namespace nana }//end namespace nana
#include <nana/pop_ignore_diagnostic> #include <nana/pop_ignore_diagnostic>