Permalink
Browse files

major style improvements for all files

  • Loading branch information...
1 parent 89d17a5 commit 2544a48fcb4f552d105124c78a3255ec719cf22f @mrtazz committed Mar 25, 2011
View
@@ -5,24 +5,22 @@
*/
#ifndef PLUSTACHE_CONTEXT_H
#define PLUSTACHE_CONTEXT_H
-#include <iostream>
-#include "plustache_types.hpp"
-
-using namespace std;
+#include <iostream>
+#include "include/plustache_types.hpp"
-class context {
+class Context {
public:
- context ();
- ~context ();
- int add(string key, string value);
- int add(string key, CollectionType c);
- int add(string key, ObjectType o);
- int add(ObjectType o);
- CollectionType get(string key);
+ Context ();
+ ~Context ();
+ int add(const std::string& key, const std::string& value);
+ int add(const std::string& key, PlustacheTypes::CollectionType& c);
+ int add(const std::string& key, const PlustacheTypes::ObjectType& o);
+ int add(const PlustacheTypes::ObjectType& o);
+ PlustacheTypes::CollectionType get(const std::string& key) const;
private:
/* data */
- map<string, CollectionType> ctx;
+ std::map<std::string, PlustacheTypes::CollectionType> ctx;
};
#endif
@@ -3,15 +3,17 @@
* @brief header file for plustache types
* @author Daniel Schauenberg <d@unwiredcouch.com>
*/
-#ifndef PLUSTACHE_TYPES_H
-#define PLUSTACHE_TYPES_H
+#ifndef PLUSTACHE_TYPES_H_
+#define PLUSTACHE_TYPES_H_
+
#include <string>
#include <map>
#include <vector>
-using namespace std;
+namespace PlustacheTypes {
+/* typedefs */
+typedef std::map<std::string, std::string> ObjectType;
+typedef std::vector<ObjectType> CollectionType;
+}
-/* defines */
-typedef vector< map<string, string> > CollectionType;
-typedef map<string, string> ObjectType;
#endif
View
@@ -11,38 +11,40 @@
#include <boost/algorithm/string/trim.hpp>
#include <boost/regex.hpp>
-#include <plustache_types.hpp>
-#include <context.hpp>
-
-using namespace std;
-using namespace boost;
+#include "include/plustache_types.hpp"
+#include "include/context.hpp"
class template_t {
+ typedef PlustacheTypes::ObjectType ObjectType;
+ typedef PlustacheTypes::CollectionType CollectionType;
public:
template_t ();
- template_t (string tmpl_path);
+ template_t (std::string& tmpl_path);
~template_t ();
- string render(string tmplate, context ctx);
- string render(string tmplate, ObjectType ctx);
+ std::string render(const std::string& tmplate, const Context& ctx);
+ std::string render(const std::string& tmplate, const ObjectType& ctx);
private:
- string template_path;
+ std::string template_path;
/* opening and closing tags */
- string otag;
- string ctag;
+ std::string otag;
+ std::string ctag;
/* regex */
- regex tag;
- regex section;
- regex escape_chars;
+ boost::regex tag;
+ boost::regex section;
+ boost::regex escape_chars;
/* lut for HTML escape chars */
- map<string, string> escape_lut;
+ std::map<std::string, std::string> escape_lut;
/* render and helper methods */
- string render_tags(string tmplate, context ctx);
- string render_sections(string tmplate, context ctx);
- string html_escape(string s);
- string get_partial(string partial);
- void change_delimiter(string opentag, string closetag);
+ std::string render_tags(const std::string& tmplate,
+ const Context& ctx);
+ std::string render_sections(const std::string& tmplate,
+ const Context& ctx);
+ std::string html_escape(const std::string& s);
+ std::string get_partial(const std::string& partial) const;
+ void change_delimiter(const std::string& opentag,
+ const std::string& closetag);
void compile_data();
- string get_template(string tmpl);
+ std::string get_template(const std::string& tmpl);
};
#endif
View
@@ -32,8 +32,8 @@ BIN_OBJECTS = BIN_FILES.collect { |fn|
}
# include dirs
-INCLUDE_DIRS = ["."]
-LDFLAGS = []
+INCLUDE_DIRS = [".", "/usr/local/include"]
+LDFLAGS = ["-L /usr/local/lib"]
res = %x[uname].chomp
if res == "Darwin"
@@ -47,7 +47,7 @@ end
# compiler/linker stuff
CC = "g++"
LD = "g++"
-CCFLAGS = ["-O3","-Wall", "-fPIC", "-I #{INCLUDE_DIRS.join(" -I")}"]
+CCFLAGS = ["-O3","-Wall", "-fPIC", "-I #{INCLUDE_DIRS.join(" -I ")}"]
# task definitions
@@ -62,7 +62,7 @@ namespace :test do
desc "task for style and error checking with cppcheck"
task :style do
- sh "cppcheck --enable=all src/"
+ sh "cppcheck -I #{INCLUDE_DIRS.join(" -I ")} --enable=all src/"
end
end
@@ -108,15 +108,15 @@ directory BINDIR
file APPLICATION => BINDIR
file APPLICATION => SRC_OBJECTS + BIN_OBJECTS do |t|
sh "#{LD} #{SRC_OBJECTS} #{BIN_OBJECTS} -o #{t.name} #{LDFLAGS.join(" ")} \
--lboost_regex-mt"
+-lboost_regex"
end
# rules for unit testing
directory BINDIR
file TEST => BINDIR
file TEST => SRC_OBJECTS + TEST_OBJECTS do |t|
sh "#{LD} #{TEST_OBJECTS} #{SRC_OBJECTS} -o #{t.name} #{LDFLAGS.join(" ")} \
--lboost_regex-mt -lgtest"
+-lboost_regex -lgtest"
end
# rules for static library
@@ -130,7 +130,7 @@ end
directory LIBDIR
file DYNAMIC_LIB => LIBDIR
file DYNAMIC_LIB => SRC_OBJECTS do |t|
- sh "#{LD} #{DYNFLAGS.join(" ")} -o #{DYNAMIC_LIB} #{SRC_OBJECTS} -lboost_regex-mt"
+ sh "#{LD} #{DYNFLAGS.join(" ")} -o #{DYNAMIC_LIB} #{SRC_OBJECTS} -lboost_regex"
end
# compile rules for object files
View
@@ -4,51 +4,52 @@
* @author Daniel Schauenberg <d@unwiredcouch.com>
*/
-#include <context.hpp>
+#include "include/context.hpp"
+#include "include/plustache_types.hpp"
-context::context()
+Context::Context()
{
}
-context::~context()
+Context::~Context()
{
}
/**
- * @brief method to add a simple key/value to the context
+ * @brief method to add a simple key/value to the Context
*
* @param key
* @param value
*
* @return 0 on success
*/
-int context::add(string key, string value)
+int Context::add(const std::string& key, const std::string& value)
{
- ObjectType obj;
+ PlustacheTypes::ObjectType obj;
obj[key] = value;
ctx[key].push_back(obj);
return 0;
}
/**
- * @brief method to add a collection to a specific key in the context
+ * @brief method to add a collection to a specific key in the Context
*
* @param key to store the data
* @param c Collection to add
*
* @return 0 on success
*/
-int context::add(string key, CollectionType c)
+int Context::add(const std::string& key, PlustacheTypes::CollectionType& c)
{
if (ctx.find(key) == ctx.end())
{
ctx[key] = c;
}
else
{
- for(CollectionType::iterator it = c.begin();
+ for(PlustacheTypes::CollectionType::iterator it = c.begin();
it != c.end(); ++it)
{
(*this).add(key, (*it));
@@ -58,20 +59,20 @@ int context::add(string key, CollectionType c)
}
/**
- * @brief method to add an additional object to a collection in the context
+ * @brief method to add an additional object to a collection in the Context
*
* @param key for the collection
* @param o object to add
*
* @return 0
*/
-int context::add(string key, ObjectType o)
+int Context::add(const std::string& key, const PlustacheTypes::ObjectType& o)
{
if (ctx.find(key) == ctx.end())
{
- CollectionType c;
- c.push_back(o);
- ctx[key] = c;
+ PlustacheTypes::CollectionType c;
+ c.push_back(o);
+ ctx[key] = c;
}
else
{
@@ -82,15 +83,15 @@ int context::add(string key, ObjectType o)
}
/**
- * @brief method to add fields of an ObjectType directly to the context
+ * @brief method to add fields of an ObjectType directly to the Context
*
* @param o ObjectType with fields
*
* @return 0
*/
-int context::add(ObjectType o)
+int Context::add(const PlustacheTypes::ObjectType& o)
{
- for(ObjectType::const_iterator it = o.begin();
+ for(PlustacheTypes::ObjectType::const_iterator it = o.begin();
it != o.end(); it++)
{
(*this).add(it->first, it->second);
@@ -99,7 +100,7 @@ int context::add(ObjectType o)
}
/**
- * @brief method to get a value from the context
+ * @brief method to get a value from the Context
*
* This is a generic getter which always returns a collection
* (vector of maps) for a keyword. If the return value is a collection, the
@@ -111,15 +112,20 @@ int context::add(ObjectType o)
*
* @return collection for the keyword
*/
-CollectionType context::get(string key)
+PlustacheTypes::CollectionType Context::get(const std::string& key) const
{
- CollectionType ret;
- ret = ctx[key];
- if (ret.size() < 1)
- {
- ObjectType o;
- o[key] = "";
- ret.push_back(o);
- }
- return ret;
+ PlustacheTypes::CollectionType ret;
+ std::map<std::string, PlustacheTypes::CollectionType> :: const_iterator it;
+ it = ctx.find(key);
+ if (it == ctx.end())
+ {
+ PlustacheTypes::ObjectType o;
+ o[key] = "";
+ ret.push_back(o);
+ }
+ else
+ {
+ ret = it->second;
+ }
+ return ret;
}
View
@@ -1,7 +1,16 @@
#include <template.hpp>
+#include <context.hpp>
+#include <iostream>
int main(int argc, const char *argv[])
{
template_t t;
+ string str_template;
+ str_template = "text {{foo}} text";
+ context ctx;
+ ctx.add("foo", "bla");
+ string s;
+ s = t.render(str_template, ctx);
+ cout << s << endl;
return 0;
}
Oops, something went wrong.

0 comments on commit 2544a48

Please sign in to comment.