Commit b8ab7e45 authored by Kiste's avatar Kiste
Browse files

Code cleanup

parent 83580d22
project('libbulletml', 'c', 'cpp', default_options : ['c_std=c89', 'cpp_std=c++03'])
project('libBulletML', 'c', 'cpp', default_options : ['c_std=c89', 'cpp_std=c++03'])
subdir('src')
/**
*
*/
#ifndef auto_ptr_fix_h_
#define auto_ptr_fix_h_
......@@ -9,19 +5,18 @@
template <class T_>
inline void auto_ptr_copy (std::auto_ptr<T_>& lhs, std::auto_ptr<T_> rhs) {
lhs = rhs;
lhs = rhs;
}
template <class T_>
inline void auto_ptr_copy (std::auto_ptr<T_>& lhs, T_* rhs) {
std::auto_ptr<T_> p(rhs);
lhs = p;
std::auto_ptr<T_> p(rhs);
lhs = p;
}
template <class T_>
inline T_* auto_ptr_release(std::auto_ptr<T_>& p) {
T_* ret = p.release();
auto_ptr_copy(p, std::auto_ptr<T_>());
return ret;
T_* ret = p.release();
auto_ptr_copy(p, std::auto_ptr<T_>());
return ret;
}
#endif // ! auto_ptr_fix_h_
......@@ -8,4 +8,3 @@
#endif
#endif // ! bulletmlcommon_h_
......@@ -8,23 +8,22 @@
/// さすがにもうちょっと充実させんとねえ
class BulletMLError : public std::runtime_error {
public:
DECLSPEC BulletMLError(const std::string& msg)
: std::runtime_error(msg) {}
DECLSPEC BulletMLError(const std::string& msg)
: std::runtime_error(msg) {}
DECLSPEC static void doAssert(const char* str) {
throw BulletMLError(str);
}
DECLSPEC static void doAssert(const std::string& str) {
throw BulletMLError(str);
}
DECLSPEC static void doAssert(bool t, const char* str) {
if (!t) throw BulletMLError(str);
}
DECLSPEC static void doAssert(bool t, const std::string& str) {
if (!t) throw BulletMLError(str);
}
DECLSPEC static void doAssert(const char* str) {
throw BulletMLError(str);
}
DECLSPEC static void doAssert(const std::string& str) {
throw BulletMLError(str);
}
DECLSPEC static void doAssert(bool t, const char* str) {
if (!t) throw BulletMLError(str);
}
DECLSPEC static void doAssert(bool t, const std::string& str) {
if (!t) throw BulletMLError(str);
}
};
#endif // ! BULLETMLERROR_H_
......@@ -8,24 +8,24 @@
BulletMLParserTinyXML::BulletMLParserTinyXML(const char* filename)
: curNode_(0)
{
setName(filename);
setName(filename);
}
BulletMLParserTinyXML::~BulletMLParserTinyXML() {}
void BulletMLParserTinyXML::getTree(TiXmlNode* node) {
if (node->ToComment() != 0) return;
if (node->ToComment() != 0) return;
translateNode(node);
TiXmlNode* child;
for (child = node->FirstChild(); child; child = child->NextSibling()) {
TiXmlText* text;
if ((text = child->ToText()) != 0) {
curNode_->setValue(text->Value());
break;
}
TiXmlText* text;
if ((text = child->ToText()) != 0) {
curNode_->setValue(text->Value());
break;
}
getTree(child);
getTree(child);
}
curNode_ = curNode_->getParent();
......@@ -37,43 +37,41 @@ void BulletMLParserTinyXML::translateNode(TiXmlNode* node) {
BulletMLNode* xmlNode = addContent(elem->Value());
if (xmlNode->getName() == BulletMLNode::bulletml) {
TiXmlAttribute* attr;
for (attr = elem->FirstAttribute(); attr; attr = attr->Next()) {
if (attr->Value() == "horizontal") setHorizontal();
}
}
else {
MyAttributes mattr;
TiXmlAttribute* attr;
for (attr = elem->FirstAttribute(); attr; attr = attr->Next()) {
mattr.push_back(attr->Name());
mattr.push_back(attr->Value());
}
addAttribute(mattr, xmlNode);
if (curNode_ != 0) curNode_->addChild(xmlNode);
}
curNode_ = xmlNode;
if (xmlNode->getName() == BulletMLNode::bulletml) {
TiXmlAttribute* attr;
for (attr = elem->FirstAttribute(); attr; attr = attr->Next()) {
if (attr->Value() == "horizontal") setHorizontal();
}
}
else {
MyAttributes mattr;
TiXmlAttribute* attr;
for (attr = elem->FirstAttribute(); attr; attr = attr->Next()) {
mattr.push_back(attr->Name());
mattr.push_back(attr->Value());
}
addAttribute(mattr, xmlNode);
if (curNode_ != 0) curNode_->addChild(xmlNode);
}
curNode_ = xmlNode;
}
void BulletMLParserTinyXML::parseImpl(TiXmlDocument& doc) {
if (doc.Error()) {
throw BulletMLError(doc.Value() + ": " + doc.ErrorDesc());
}
if (doc.Error()) {
throw BulletMLError(doc.Value() + ": " + doc.ErrorDesc());
}
TiXmlNode* node;
for (node = doc.FirstChild(); node; node = node->NextSibling()) {
if (node->ToElement() != 0) {
getTree(node);
break;
}
if (node->ToElement() != 0) {
getTree(node);
break;
}
}
}
void BulletMLParserTinyXML::parse() {
TiXmlDocument doc(name_);
doc.LoadFile();
parseImpl(doc);
parseImpl(doc);
}
......@@ -22,7 +22,7 @@ public:
DECLSPEC virtual void parse();
private:
void parseImpl(TiXmlDocument& doc);
void parseImpl(TiXmlDocument& doc);
protected:
void getTree(TiXmlNode* node);
......
......@@ -6,153 +6,151 @@
namespace {
/// 内部のみで使用するクラス。
class IDPool {
public:
static int getID(BulletMLNode::Name domain, const std::string& key) {
KeyToID& kti = map_[domain];
KeyToID::iterator ite = kti.find(key);
if (ite == kti.end()) {
int id = maxMap_[domain]++;
map_[domain][key] = id;
return id;
}
else {
return ite->second;
}
}
static void init() {
map_.insert(std::make_pair(BulletMLNode::bullet, KeyToID()));
map_.insert(std::make_pair(BulletMLNode::action, KeyToID()));
map_.insert(std::make_pair(BulletMLNode::fire, KeyToID()));
maxMap_.insert(std::make_pair(BulletMLNode::bullet, 0));
maxMap_.insert(std::make_pair(BulletMLNode::action, 0));
maxMap_.insert(std::make_pair(BulletMLNode::fire, 0));
}
static void quit() {
map_.clear();
maxMap_.clear();
}
private:
typedef std::map<std::string, unsigned int> KeyToID;
typedef std::map<BulletMLNode::Name, KeyToID> DomainToIDMap;
typedef std::map<BulletMLNode::Name, int> DomainToMaxID;
static DomainToIDMap map_;
static DomainToMaxID maxMap_;
};
class IDPool {
public:
static int getID(BulletMLNode::Name domain, const std::string& key) {
KeyToID& kti = map_[domain];
KeyToID::iterator ite = kti.find(key);
if (ite == kti.end()) {
int id = maxMap_[domain]++;
map_[domain][key] = id;
return id;
}
else {
return ite->second;
}
}
static void init() {
map_.insert(std::make_pair(BulletMLNode::bullet, KeyToID()));
map_.insert(std::make_pair(BulletMLNode::action, KeyToID()));
map_.insert(std::make_pair(BulletMLNode::fire, KeyToID()));
maxMap_.insert(std::make_pair(BulletMLNode::bullet, 0));
maxMap_.insert(std::make_pair(BulletMLNode::action, 0));
maxMap_.insert(std::make_pair(BulletMLNode::fire, 0));
}
static void quit() {
map_.clear();
maxMap_.clear();
}
private:
typedef std::map<std::string, unsigned int> KeyToID;
typedef std::map<BulletMLNode::Name, KeyToID> DomainToIDMap;
typedef std::map<BulletMLNode::Name, int> DomainToMaxID;
static DomainToIDMap map_;
static DomainToMaxID maxMap_;
};
}
IDPool::DomainToIDMap IDPool::map_;
IDPool::DomainToMaxID IDPool::maxMap_;
BulletMLParser::BulletMLParser()
: bulletml_(0), isHorizontal_(false)
: bulletml_(0), isHorizontal_(false)
{}
BulletMLParser::~BulletMLParser() {
// BulletMLNode はルートノードだけ破壊すれば良い
delete bulletml_;
// BulletMLNode はルートノードだけ破壊すれば良い
delete bulletml_;
}
void BulletMLParser::build() {
IDPool::init();
parse();
IDPool::quit();
IDPool::init();
parse();
IDPool::quit();
}
BulletMLNode* BulletMLParser::getBulletRef(int id) {
BulletMLError::doAssert((int)bulletMap_.size() > id && bulletMap_[id] != 0,
"bulletRef key doesn't exist.");
return bulletMap_[id];
BulletMLError::doAssert((int)bulletMap_.size() > id && bulletMap_[id] != 0,
"bulletRef key doesn't exist.");
return bulletMap_[id];
}
BulletMLNode* BulletMLParser::getActionRef(int id) {
BulletMLError::doAssert((int)actionMap_.size() > id && actionMap_[id] != 0,
"actionRef key doesn't exist.");
return actionMap_[id];
BulletMLError::doAssert((int)actionMap_.size() > id && actionMap_[id] != 0,
"actionRef key doesn't exist.");
return actionMap_[id];
}
BulletMLNode* BulletMLParser::getFireRef(int id) {
BulletMLError::doAssert((int)fireMap_.size() > id && fireMap_[id] != 0,
"fireRef key doesn't exist.");
return fireMap_[id];
BulletMLError::doAssert((int)fireMap_.size() > id && fireMap_[id] != 0,
"fireRef key doesn't exist.");
return fireMap_[id];
}
BulletMLNode* BulletMLParser::addContent(const std::string& name) {
// ルートノードは別処理
if (name == "bulletml") {
bulletml_ = new BulletMLNode(name);
return bulletml_;
}
BulletMLError::doAssert(bulletml_ != 0, "<bulletml> doesn't come.");
return new BulletMLNode(name);
// ルートノードは別処理
if (name == "bulletml") {
bulletml_ = new BulletMLNode(name);
return bulletml_;
}
BulletMLError::doAssert(bulletml_ != 0, "<bulletml> doesn't come.");
return new BulletMLNode(name);
}
void BulletMLParser::addAttribute(const MyAttributes& attr,
BulletMLNode* elem)
BulletMLNode* elem)
{
if (!attr.empty()) {
MyAttributeIte ite = attr.begin();
while (ite != attr.end()) {
const std::string key(*ite);
ite++;
const std::string val(*ite);
ite++;
if (key == "type") elem->setType(val);
else if (key == "label") {
BulletMLNode::Name name = elem->getName();
BulletMLNode::Name domain;
if (name == BulletMLNode::bulletRef) {
domain = BulletMLNode::bullet;
}
else if (name == BulletMLNode::actionRef) {
domain = BulletMLNode::action;
}
else if (name == BulletMLNode::fireRef) {
domain = BulletMLNode::fire;
}
else {
domain = name;
}
int id = IDPool::getID(domain, val);
if (name == BulletMLNode::bullet) {
if ((int)bulletMap_.size() <= id){
bulletMap_.resize(id+1, 0);}
bulletMap_[id] = elem;
}
else if (name == BulletMLNode::action) {
if ((int)actionMap_.size() <= id){
actionMap_.resize(id+1, 0);
}
actionMap_[id] = elem;
}
else if (name == BulletMLNode::fire) {
if ((int)fireMap_.size() <= id)
fireMap_.resize(id+1, 0);
fireMap_[id] = elem;
}
else if (
name == BulletMLNode::bulletRef ||
name == BulletMLNode::actionRef ||
name == BulletMLNode::fireRef)
{
elem->setRefID(id);
}
else {
BulletMLError::doAssert(
"he can't have attribute \"label\".");
}
if (elem->getName() == BulletMLNode::action &&
val.length() >= 3 && val.substr(0, 3) == "top") {
topActions_.push_back(elem);
}
}
}
}
if (!attr.empty()) {
MyAttributeIte ite = attr.begin();
while (ite != attr.end()) {
const std::string key(*ite);
ite++;
const std::string val(*ite);
ite++;
if (key == "type") elem->setType(val);
else if (key == "label") {
BulletMLNode::Name name = elem->getName();
BulletMLNode::Name domain;
if (name == BulletMLNode::bulletRef) {
domain = BulletMLNode::bullet;
}
else if (name == BulletMLNode::actionRef) {
domain = BulletMLNode::action;
}
else if (name == BulletMLNode::fireRef) {
domain = BulletMLNode::fire;
}
else {
domain = name;
}
int id = IDPool::getID(domain, val);
if (name == BulletMLNode::bullet) {
if ((int)bulletMap_.size() <= id){
bulletMap_.resize(id+1, 0);}
bulletMap_[id] = elem;
}
else if (name == BulletMLNode::action) {
if ((int)actionMap_.size() <= id){
actionMap_.resize(id+1, 0);
}
actionMap_[id] = elem;
}
else if (name == BulletMLNode::fire) {
if ((int)fireMap_.size() <= id)
fireMap_.resize(id+1, 0);
fireMap_[id] = elem;
}
else if (
name == BulletMLNode::bulletRef ||
name == BulletMLNode::actionRef ||
name == BulletMLNode::fireRef)
{
elem->setRefID(id);
}
else {
BulletMLError::doAssert(
"he can't have attribute \"label\".");
}
if (elem->getName() == BulletMLNode::action &&
val.length() >= 3 && val.substr(0, 3) == "top") {
topActions_.push_back(elem);
}
}
}
}
}
......@@ -24,7 +24,7 @@ public:
DECLSPEC virtual ~BulletMLParser();
public:
DECLSPEC void build();
DECLSPEC void build();
DECLSPEC virtual void parse() =0;
public:
......@@ -38,11 +38,11 @@ public:
DECLSPEC BulletMLNode* getFireRef(int id);
//@}
DECLSPEC const std::vector<BulletMLNode*>& getTopActions() const {
return topActions_;
}
DECLSPEC const std::vector<BulletMLNode*>& getTopActions() const {
return topActions_;
}
DECLSPEC void setHorizontal() { isHorizontal_ = true; }
DECLSPEC void setHorizontal() { isHorizontal_ = true; }
DECLSPEC bool isHorizontal() const { return isHorizontal_; }
protected:
......@@ -57,10 +57,10 @@ protected:
protected:
BulletMLNode* bulletml_;
std::vector<BulletMLNode*> topActions_;
std::vector<BulletMLNode*> topActions_;
typedef std::vector<BulletMLNode*> MyMap;
typedef MyMap BulletMap;
typedef MyMap BulletMap;
typedef MyMap ActionMap;
typedef MyMap FireMap;
BulletMap bulletMap_;
......@@ -70,7 +70,7 @@ protected:
bool isHorizontal_;
protected:
/// 一時的な導入
/// 一時的な導入
void setName(const char* name) { name_ = name; }
const char* name_;
public:
......@@ -83,9 +83,9 @@ std::string BulletMLParser::uc2string(Char_* src, size_t len) {
std::string dst;
size_t i = 0;
while (i != len && *src != '\0') {
dst += *src;
src++;
i++;
dst += *src;
src++;
i++;
}
return dst;
}
......
......@@ -5,54 +5,54 @@
#include "bulletmlrunnerimpl.h"
BulletMLRunner::BulletMLRunner(BulletMLParser* bulletml) {
const std::vector<BulletMLNode*>& acts = bulletml->getTopActions();
for (size_t i = 0; i < acts.size(); i++) {
std::vector<BulletMLNode*> act;
act.push_back(acts[i]);
BulletMLState* state =
new BulletMLState(bulletml, act,
std::tr1::shared_ptr<BulletMLParameter>());
impl_.push_back(makeImpl(state));
}
const std::vector<BulletMLNode*>& acts = bulletml->getTopActions();
for (size_t i = 0; i < acts.size(); i++) {
std::vector<BulletMLNode*> act;
act.push_back(acts[i]);
BulletMLState* state =
new BulletMLState(bulletml, act,
std::tr1::shared_ptr<BulletMLParameter>());
impl_.push_back(makeImpl(state));
}
}
BulletMLRunner::BulletMLRunner(BulletMLState* state) {
impl_.push_back(makeImpl(state));
impl_.push_back(makeImpl(state));
}
BulletMLRunner::~BulletMLRunner() {
for (size_t i = 0; i < impl_.size(); i++) {
delete impl_[i];
}
for (size_t i = 0; i < impl_.size(); i++) {
delete impl_[i];
}
}
void BulletMLRunner::run() {
/*
std::for_each(impl_.begin(), impl_.end(),
std::mem_fun(&BulletMLRunnerImpl::run));
std::for_each(impl_.begin(), impl_.end(),
std::mem_fun(&BulletMLRunnerImpl::run));
*/
for (std::vector<BulletMLRunnerImpl*>::const_iterator ite = impl_.begin();
ite != impl_.end(); ++ite)
{
(*ite)->run();
}
for (std::vector<BulletMLRunnerImpl*>::const_iterator ite = impl_.begin();
ite != impl_.end(); ++ite)
{
(*ite)->run();
}
}
bool BulletMLRunner::isEnd() const {
/*
return
std::find_if(impl_.begin(), impl_.end(),
std::not1(std::mem_fun(&BulletMLRunnerImpl::isEnd)))
== impl_.end();
return
std::find_if(impl_.begin(), impl_.end(),
std::not1(std::mem_fun(&BulletMLRunnerImpl::isEnd)))
== impl_.end();
*/
for (std::vector<BulletMLRunnerImpl*>::const_iterator ite = impl_.begin();
ite != impl_.end(); ++ite)
{
if ((*ite)->isEnd()) return true;
}
return false;