summaryrefslogtreecommitdiff
path: root/src/cstore
diff options
context:
space:
mode:
authorAn-Cheng Huang <ancheng@vyatta.com>2011-04-06 18:58:22 -0700
committerAn-Cheng Huang <ancheng@vyatta.com>2011-04-06 18:58:22 -0700
commitfcdc4ad086945f8a947c2ede2f87bc8d549bcae0 (patch)
treec5741ab4bfd5b8e3ffd9e0524d241cdcc63f5419 /src/cstore
parentff6dbf4361fe64ed00478e98f895d3bd15248869 (diff)
downloadvyatta-cfg-fcdc4ad086945f8a947c2ede2f87bc8d549bcae0.tar.gz
vyatta-cfg-fcdc4ad086945f8a947c2ede2f87bc8d549bcae0.zip
minor refactoring
Diffstat (limited to 'src/cstore')
-rw-r--r--src/cstore/cpath.hpp1
-rw-r--r--src/cstore/cstore-varref.cpp2
-rw-r--r--src/cstore/cstore.cpp14
-rw-r--r--src/cstore/cstore.hpp12
-rw-r--r--src/cstore/ctemplate.hpp1
-rw-r--r--src/cstore/svector.hpp27
-rw-r--r--src/cstore/unionfs/cstore-unionfs.cpp19
-rw-r--r--src/cstore/util.hpp36
8 files changed, 78 insertions, 34 deletions
diff --git a/src/cstore/cpath.hpp b/src/cstore/cpath.hpp
index ed26a92..05fe82d 100644
--- a/src/cstore/cpath.hpp
+++ b/src/cstore/cpath.hpp
@@ -65,6 +65,7 @@ public:
const char *back() const {
return (size() > 0 ? _data[size() - 1] : NULL);
};
+ std::string to_string() const { return _data.to_string(); };
private:
struct CpathParams {
diff --git a/src/cstore/cstore-varref.cpp b/src/cstore/cstore-varref.cpp
index 2ec3c51..1e3be90 100644
--- a/src/cstore/cstore-varref.cpp
+++ b/src/cstore/cstore-varref.cpp
@@ -232,7 +232,7 @@ bool
Cstore::VarRef::getValue(string& value, vtw_type_e& def_type)
{
vector<string> result;
- Cstore::MapT<string, bool> added;
+ MapT<string, bool> added;
def_type = ERROR_TYPE;
for (size_t i = 0; i < _paths.size(); i++) {
if (_paths[i].first.size() == 0) {
diff --git a/src/cstore/cstore.cpp b/src/cstore/cstore.cpp
index 70f1c55..2fa7a9b 100644
--- a/src/cstore/cstore.cpp
+++ b/src/cstore/cstore.cpp
@@ -74,7 +74,7 @@ const unsigned int Cstore::SORT_NONE = 1;
////// static
bool Cstore::_init = false;
-Cstore::MapT<unsigned int, Cstore::SortFuncT> Cstore::_sort_func_map;
+MapT<unsigned int, Cstore::SortFuncT> Cstore::_sort_func_map;
////// constructors/destructors
@@ -142,8 +142,8 @@ Cstore::parseTmpl(const Cpath& path_comps, bool validate_vals)
* return true if successful. otherwise return false.
*/
bool
-Cstore::getParsedTmpl(const Cpath& path_comps,
- Cstore::MapT<string, string>& tmap, bool allow_val)
+Cstore::getParsedTmpl(const Cpath& path_comps, MapT<string, string>& tmap,
+ bool allow_val)
{
/* currently this function is used outside actual CLI operations, mainly
* from the perl API. since value validation is from the original CLI
@@ -1950,7 +1950,7 @@ Cstore::sort_func_deb_version(string a, string b)
void
Cstore::sort_nodes(vector<string>& nvec, unsigned int sort_alg)
{
- Cstore::MapT<unsigned int, Cstore::SortFuncT>::iterator p
+ MapT<unsigned int, Cstore::SortFuncT>::iterator p
= _sort_func_map.find(sort_alg);
if (p == _sort_func_map.end()) {
return;
@@ -1991,7 +1991,7 @@ Cstore::append_tmpl_path(const Cpath& path_comps, bool& is_tag)
return true;
}
-typedef Cstore::MapT<Cpath, tr1::shared_ptr<Ctemplate>, CpathHash> TmplCacheT;
+typedef MapT<Cpath, tr1::shared_ptr<Ctemplate>, CpathHash> TmplCacheT;
static TmplCacheT _tmpl_cache;
/* check whether specified "logical path" is valid template path.
@@ -2462,7 +2462,7 @@ Cstore::set_cfg_path(const Cpath& path_comps, bool output)
*/
void
Cstore::get_child_nodes_status(const Cpath& path_comps,
- Cstore::MapT<string, string>& cmap,
+ MapT<string, string>& cmap,
vector<string> *sorted_keys)
{
// get a union of active and working
@@ -2512,7 +2512,7 @@ Cstore::get_child_nodes_status(const Cpath& path_comps,
*/
void
Cstore::get_child_nodes_status_da(const Cpath& path_comps,
- Cstore::MapT<string, string>& cmap,
+ MapT<string, string>& cmap,
vector<string> *sorted_keys)
{
// process deleted nodes first
diff --git a/src/cstore/cstore.hpp b/src/cstore/cstore.hpp
index 62677c1..f2163a8 100644
--- a/src/cstore/cstore.hpp
+++ b/src/cstore/cstore.hpp
@@ -19,9 +19,9 @@
#include <cstdarg>
#include <vector>
#include <string>
-#include <tr1/unordered_map>
#include <cli_cstore.h>
+#include <cstore/util.hpp>
#include <cstore/cpath.hpp>
#include <cstore/ctemplate.hpp>
@@ -55,10 +55,6 @@ public:
static Cstore *createCstore(bool use_edit_level = false);
static Cstore *createCstore(const string& session_id, string& env);
- // types
- template<class K, class V, class H = tr1::hash<K> >
- class MapT : public tr1::unordered_map<K, V, H> {};
-
// constants
static const string C_NODE_STATUS_DELETED;
static const string C_NODE_STATUS_ADDED;
@@ -86,7 +82,7 @@ public:
// for sorting
/* apparently unordered_map template does not work with "enum" type, so
* change this to simply unsigned ints to allow unifying all map types,
- * i.e., "Cstore::MapT".
+ * i.e., "cstore::MapT".
*/
static const unsigned int SORT_DEFAULT;
static const unsigned int SORT_DEB_VERSION;
@@ -448,10 +444,10 @@ private:
bool include_deactivated);
bool set_cfg_path(const Cpath& path_comps, bool output);
void get_child_nodes_status(const Cpath& path_comps,
- Cstore::MapT<string, string>& cmap,
+ MapT<string, string>& cmap,
vector<string> *sorted_keys);
void get_child_nodes_status_da(const Cpath& path_comps,
- Cstore::MapT<string, string>& cmap,
+ MapT<string, string>& cmap,
vector<string> *sorted_keys);
// these operate on current work path (or active with "active_cfg")
diff --git a/src/cstore/ctemplate.hpp b/src/cstore/ctemplate.hpp
index e970a5d..ebe4a5b 100644
--- a/src/cstore/ctemplate.hpp
+++ b/src/cstore/ctemplate.hpp
@@ -89,6 +89,7 @@ public:
const char *getValHelp() const { return _def->def_val_help; };
unsigned int getTagLimit() const { return _def->def_tag; };
unsigned int getMultiLimit() const { return _def->def_multi; };
+ unsigned int getPriority() const { return _def->def_priority; };
void setIsValue(bool is_val) { _is_value = is_val; };
diff --git a/src/cstore/svector.hpp b/src/cstore/svector.hpp
index fd63297..bc04fdc 100644
--- a/src/cstore/svector.hpp
+++ b/src/cstore/svector.hpp
@@ -21,14 +21,9 @@
#include <string>
#include <tr1/functional>
-namespace cstore {
+#include <cstore/util.hpp>
-template<int v>
-struct Int2Type {
- enum {
- value = v
- };
-};
+namespace cstore {
template<class P>
class svector {
@@ -88,6 +83,9 @@ public:
size_t hash() const {
return std::tr1::_Fnv_hash<sizeof(size_t)>::hash(_data, _len);
};
+ std::string to_string() const {
+ return to_string(Int2Type<RAW_CSTR_DATA>());
+ };
private:
size_t _num_elems;
@@ -117,7 +115,20 @@ private:
};
const char *elem_at(size_t idx, Int2Type<true>) const {
return (idx < _num_elems ? (_elems[idx] + 1) : NULL);
- }
+ };
+ std::string to_string(Int2Type<true>) const {
+ return std::string(_data);
+ };
+ std::string to_string(Int2Type<false>) const {
+ std::string ret;
+ for (size_t i = 0; i < size(); i++) {
+ if (i > 0) {
+ ret += " ";
+ }
+ ret += elem_at(i, Int2Type<RANDOM_ACCESS>());
+ }
+ return ret;
+ };
};
template<class P>
diff --git a/src/cstore/unionfs/cstore-unionfs.cpp b/src/cstore/unionfs/cstore-unionfs.cpp
index ae9e405..e1a7dfd 100644
--- a/src/cstore/unionfs/cstore-unionfs.cpp
+++ b/src/cstore/unionfs/cstore-unionfs.cpp
@@ -64,8 +64,8 @@ const string UnionfsCstore::C_DEF_NAME = "node.def";
////// static
-static Cstore::MapT<char, string> _fs_escape_chars;
-static Cstore::MapT<string, char> _fs_unescape_chars;
+static MapT<char, string> _fs_escape_chars;
+static MapT<string, char> _fs_unescape_chars;
static void
_init_fs_escape_chars()
{
@@ -81,7 +81,7 @@ _init_fs_escape_chars()
static string
_escape_char(char c)
{
- Cstore::MapT<char, string>::iterator p = _fs_escape_chars.find(c);
+ MapT<char, string>::iterator p = _fs_escape_chars.find(c);
if (p != _fs_escape_chars.end()) {
return p->second;
} else {
@@ -89,12 +89,12 @@ _escape_char(char c)
}
}
-static Cstore::MapT<string, string> _escape_path_name_cache;
+static MapT<string, string> _escape_path_name_cache;
static string
_escape_path_name(const string& path)
{
- Cstore::MapT<string, string>::iterator p
+ MapT<string, string>::iterator p
= _escape_path_name_cache.find(path);
if (p != _escape_path_name_cache.end()) {
// found escaped string in cache. just return it.
@@ -112,12 +112,12 @@ _escape_path_name(const string& path)
return npath;
}
-static Cstore::MapT<string, string> _unescape_path_name_cache;
+static MapT<string, string> _unescape_path_name_cache;
static string
_unescape_path_name(const string& path)
{
- Cstore::MapT<string, string>::iterator p
+ MapT<string, string>::iterator p
= _unescape_path_name_cache.find(path);
if (p != _unescape_path_name_cache.end()) {
// found unescaped string in cache. just return it.
@@ -132,7 +132,7 @@ _unescape_path_name(const string& path)
break;
}
string s = path.substr(i, 3);
- Cstore::MapT<string, char>::iterator p = _fs_unescape_chars.find(s);
+ MapT<string, char>::iterator p = _fs_unescape_chars.find(s);
if (p != _fs_unescape_chars.end()) {
char c = p->second;
if (path.size() == 3 && c == -1) {
@@ -426,8 +426,7 @@ UnionfsCstore::tmpl_node_exists()
return (path_exists(tmpl_path) && path_is_directory(tmpl_path));
}
-typedef Cstore::MapT<FsPath, tr1::shared_ptr<vtw_def>, FsPathHash>
- ParsedTmplCacheT;
+typedef MapT<FsPath, tr1::shared_ptr<vtw_def>, FsPathHash> ParsedTmplCacheT;
static ParsedTmplCacheT _parsed_tmpl_cache;
/* parse template at current tmpl_path and return an allocated Ctemplate
diff --git a/src/cstore/util.hpp b/src/cstore/util.hpp
new file mode 100644
index 0000000..7f18c13
--- /dev/null
+++ b/src/cstore/util.hpp
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2011 Vyatta, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _UTIL_H_
+#define _UTIL_H_
+#include <tr1/unordered_map>
+
+namespace cstore { // begin namespace cstore
+
+template<class K, class V, class H = std::tr1::hash<K> >
+ class MapT : public std::tr1::unordered_map<K, V, H> {};
+
+template<int v>
+struct Int2Type {
+ enum {
+ value = v
+ };
+};
+
+} // end namespace cstore
+
+#endif /* _UTIL_H_ */
+