glibmm r711 - in trunk: . glib/glibmm tests tests/glibmm_ustring_compose



Author: murrayc
Date: Wed Aug  6 08:34:07 2008
New Revision: 711
URL: http://svn.gnome.org/viewvc/glibmm?rev=711&view=rev

Log:
2008-08-06  Murray Cumming  <murrayc murrayc com>

* configure.in:
* tests/Makefile.am:
* tests/glibmm_ustring_compose/main.cc: Added a test case.
* glib/glibmm/ustring.h: Added a ustring::Stringify<> 
template specialization so that ustring::compose() works with 
const char* arguments, though it still needs to be fixed to 
work for string literals.
Bug #506410 (SzilÃrd Pfeiffer).

Added:
   trunk/tests/glibmm_ustring_compose/
      - copied from r710, /trunk/tests/glibmm_nodetree/
Modified:
   trunk/ChangeLog
   trunk/configure.in
   trunk/glib/glibmm/ustring.h
   trunk/tests/Makefile.am
   trunk/tests/glibmm_ustring_compose/main.cc

Modified: trunk/configure.in
==============================================================================
--- trunk/configure.in	(original)
+++ trunk/configure.in	Wed Aug  6 08:34:07 2008
@@ -303,9 +303,10 @@
   tests/Makefile
     tests/glibmm_value/Makefile
     tests/glibmm_nodetree/Makefile
+    tests/glibmm_date/Makefile
+    tests/glibmm_ustring_compose/Makefile
     tests/giomm_simple/Makefile
     tests/giomm_ioerror/Makefile
-    tests/glibmm_date/Makefile
 
   examples/Makefile
     examples/compose/Makefile

Modified: trunk/glib/glibmm/ustring.h
==============================================================================
--- trunk/glib/glibmm/ustring.h	(original)
+++ trunk/glib/glibmm/ustring.h	Wed Aug  6 08:34:07 2008
@@ -1228,6 +1228,8 @@
   return buf.to_string();
 }
 
+/** An inner class used by ustring.
+ */
 template <class T>
 class ustring::Stringify
 {
@@ -1240,10 +1242,14 @@
 
 public:
   explicit inline Stringify(const T& arg) : string_ (ustring::format(arg)) {}
+
+  //TODO: Why is this here? See the template specialization:
   explicit inline Stringify(const char* arg) : string_(arg) {}
+
   inline const ustring* ptr() const { return &string_; }
 };
 
+/// A template specialization for Stringify<ustring>:
 template <>
 class ustring::Stringify<ustring>
 {
@@ -1259,6 +1265,45 @@
   inline const ustring* ptr() const { return &string_; }
 };
 
+/** A template specialization for Stringify<const char*>,
+ * because the regular template has ambiguous constructor overloads for char*.
+ */ 
+template <>
+class ustring::Stringify<const char*>
+{
+private:
+  const ustring& string_;
+
+  // noncopyable
+  Stringify(const ustring::Stringify<const char*>&);
+  Stringify<ustring>& operator=(const ustring::Stringify<const char*>&);
+
+public:
+  explicit inline Stringify(const char* arg) : string_(arg) {}
+  inline const ustring* ptr() const { return &string_; }
+};
+
+/* TODO: I can't seem to specify a template specialization for Stringify with a string literal. murrayc.
+
+ * A template specialization for Stringify<char[N]> (for string literals),
+ * because the regular template has ambiguous constructor overloads for char*.
+
+template <std::size_t N>
+class ustring::Stringify<const char[N]>
+{
+private:
+  const ustring& string_;
+
+  // noncopyable
+  Stringify(const ustring::Stringify<const char[N]>&);
+  Stringify<ustring>& operator=(const ustring::Stringify<const char[N]>&);
+
+public:
+  explicit inline Stringify(const char arg[N]) : string_(arg) {}
+  inline const ustring* ptr() const { return &string_; }
+};
+*/
+
 template <class T1>
 inline // static
 ustring ustring::compose(const ustring& fmt, const T1& a1)

Modified: trunk/tests/Makefile.am
==============================================================================
--- trunk/tests/Makefile.am	(original)
+++ trunk/tests/Makefile.am	Wed Aug  6 08:34:07 2008
@@ -1,4 +1,4 @@
-test_dirs = glibmm_value glibmm_nodetree glibmm_date giomm_simple giomm_ioerror
+test_dirs = glibmm_value glibmm_nodetree glibmm_date glibmm_ustring_compose giomm_simple giomm_ioerror
 
 SUBDIRS = $(test_dirs)
 EXTRA_DIST = Makefile.am_fragment

Modified: trunk/tests/glibmm_ustring_compose/main.cc
==============================================================================
--- /trunk/tests/glibmm_nodetree/main.cc	(original)
+++ trunk/tests/glibmm_ustring_compose/main.cc	Wed Aug  6 08:34:07 2008
@@ -1,112 +1,28 @@
-#include <iostream>
 #include <glibmm.h>
 
-typedef Glib::NodeTree<std::string> type_nodetree_string;
+#include <iostream>
 
-bool echo(type_nodetree_string& i)
+int main(int, char**)
 {
-  std::cout << i.data() << ' ';
-  return false;
-}
+  Glib::init();
 
-void echol(type_nodetree_string& i, bool is_leaf)
-{
-  if(i.is_leaf() == is_leaf)
-    std::cout << i.data() << ' ';
-}
+  const char *constant_string = "constant string";
+  std::cout << Glib::ustring::compose("Compose strings: %1", constant_string) << std::endl;
+  //TODO: Make this work. See ustring.h: std::cout << Glib::ustring::compose("Compose strings: %1 and %2", constant_string, "string_literal") << std::endl;
 
+  std::cout << Glib::ustring::compose("Compose strings: %1 and %2", 123, 123.4567) << std::endl;
 
-int main()
-{
-  std::string a("a"),
-              b("b"),
-              c("c"),
-              d("d"),
-              e("e"),
-              f("f");
-
-  type_nodetree_string ta(a), tb(b), tc(c), te(e);
-
-  sigc::slot<bool, type_nodetree_string&> echoslot = sigc::ptr_fun(echo);
-
-
-  ta.insert(0, tc);
-  ta.prepend(tb);
-  ta.append_data(d);
-  tc.append(te);
-  te.prepend_data(f);
-
-
-  std::cout << "Breadth-first:" << std::endl;
-  ta.traverse(echoslot, Glib::TRAVERSE_LEVEL_ORDER);
-  std::cout << std::endl;
-
-  std::cout << "Depth-first (pre):" << std::endl;
-  ta.traverse(echoslot, Glib::TRAVERSE_PRE_ORDER);
-  std::cout << std::endl;
-
-  std::cout << "Depth-first (in):" << std::endl;
-  ta.traverse(echoslot, Glib::TRAVERSE_IN_ORDER);
-  std::cout << std::endl;
-
-  std::cout << "Depth-first (post):" << std::endl;
-  ta.traverse(echoslot, Glib::TRAVERSE_POST_ORDER);
-  std::cout << std::endl;
-
-  std::cout << "Leaf children of 'a':" << std::endl;
-  ta.foreach(sigc::bind<bool>(sigc::ptr_fun(echol), true));
-  std::cout << std::endl;
-
-  std::cout << "Non-leaf children of 'a':" << std::endl;
-  ta.foreach(sigc::bind<bool>(sigc::ptr_fun(echol), false));
-  std::cout << std::endl;
-
-  type_nodetree_string* tmp = ta.find(e);
-  if(!tmp)
-    std::cout << e << " not found" << std::endl;
-  else
-    std::cout << "Found " << (tmp->data()) << std::endl;
-
-  tmp = ta.find(a);
-  if(!tmp)
-    std::cout << a << " not found" << std::endl;
-  else
-    std::cout << "Found " << (tmp->data()) << std::endl;
-
-  tmp = ta.find("f");
-  if(!tmp)
-    std::cout << a << " not found" << std::endl;
-  else
-    std::cout << "Found " << (tmp->data()) << std::endl;
-
-  tmp = ta.find_child(e);
-  if(!tmp)
-    std::cout << e << " is not a child of " << (ta.data()) << std::endl;
-  else
-    std::cout << "Mistakenly found " << e << " in " << (ta.data()) << "'s children" << std::endl;
-
-  tmp = ta.find_child(c);
-  if(!tmp)
-    std::cout << c << " is the number " << ta.child_index(c) << " child of " << (ta.data()) << std::endl;
-  else
-   std::cout << "Mistakenly didn't find " << c << " in " << (ta.data()) << "'s children" << std::endl;
-
-  tmp = tc.next_sibling();
-  if(!tmp)
-    std::cout << tc.data() << "'s next sibling is NULL" << std::endl;
-  else
-    std::cout << tc.data() << "'s next sibling is " << tmp->data() << std::endl;
-
-  tmp = ta.get_root();
-  std::cout << "Root is " << (tmp->data()) << std::endl;
-  std::cout << "Depth is " << tmp->get_max_height() << std::endl;
-
-  ta.unlink(tc);
-  std::cout << "New depth is " << tmp->get_max_height() << std::endl;
-
-  tmp = tc.get_root();
-  std::cout << "Pruned root is " << (tmp->data()) << std::endl;
-  std::cout << "Pruned depth is " << tmp->get_max_height() << std::endl;
+  std::cout << Glib::ustring::compose("Compose strings: %1 and %2", (int)123, (float)123.4567) << std::endl;
+
+  std::cout << Glib::ustring::compose("Compose strings: %1 and %2", Glib::ustring("foo"), std::string("goo")) << std::endl;
+
+  int i = 1;
+  std::cout << Glib::ustring::compose("Compose strings: %1 and %2", 'f', &i) << std::endl;
+
+  std::cout << Glib::ustring::compose("%1 is lower than 0x%2.", 12, Glib::ustring::format(std::hex, 16)) << std::endl;
+
+  //TODO: More tests.
 
   return 0;
 }
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]