2010-08-21 107 views
1

我在我的应用程序中有一个生产者/消费者设计,它在用户类型上实现生产者/消费者功能。但对于标准库,特别是对于算法而言,它并不是很自然。C++和枚举

在C#中有Enumerable和Observable概念,可以用来轻松实现像这样的东西,并获得很多不错的免费功能。

在C++中有我认为可能有用的ios,istream,ostream,input_iterator,output_iterator概念。但在我看来,所有这些都是针对原始字符类型的,例如char,int等...而不是用户类型。

当然,我可以使用真正的功能,如产品/消费者和std :: mem_fn算法。但我希望有更好的办法。

我正在寻找一些关于如何去设计I/O类似C++用户类型的解决方案的最佳实践建议。

E.g.来自C#

class FrameProducer : IEnumerable<Frame> // pull frames 
{...} 

// Some engine between 

class FrameConsumer : IObserver<Frame> // push frames 
{...} 

我希望在C++中有类似的东西,例如我不相信这是可能的。

class FrameProducer : istream<Frame> // pull frames 
{...} 

// Some engine between 

class FrameConsumer : ostream<Frame> // push frames 
{...} 

也许我在想这个问题,应该去KISS。

想法?

+0

你为什么在谈论C#?这是C++,忘记了解C#并开始用C++编程。说“我会用X语言这样做,我怎么能把它翻译成Y语言”总是会失败,你必须从头开始学习Y语言,这样你才能知道Y语言是如何完成的。[一本好书帮助(http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。哦,我不明白这个问题。当你把语言X带入关于Y语言的问题时,很多懂Y语言的人需要你解释X语言特性。 – GManNickG 2010-08-21 20:22:53

+0

您能否更好地解释“用户类型的产品/消费函数”的含义? – Dacav 2010-08-21 20:24:34

+0

无论我说什么语言,都不重要。我只是选择了C#,因为它具有与C++及其迭代器和算法类似的LINQ概念,因此很方便。 我的问题是为什么C + + ios概念不允许用户类型和有什么替代方案。 – ronag 2010-08-21 20:26:29

回答

3

术语是“插入操作符”和“提取操作符”,它们从流中插入和提取数据。

下面是一个例子:

#include <iostream> 
#include <sstream> 

struct foo 
{ 
    int x; 
}; 

// insertion operator 
std::ostream& operator<<(std::ostream& s, const foo& f) 
{ 
    s << f.x; // insert a foo by inserting x 
    return s; 
} 

// extraction operator 
std::istream& operator>>(std::istream& s, foo& f) 
{ 
    s >> f.x; // extract a foo by extracting x 
    return s; 
} 

int main(void) 
{ 
    std::stringstream ss; 

    foo f1 = {5}; 
    ss << f1; 

    foo f2; 
    ss >> f2; 
} 

根据您的愿望,要做到:

MyFrameProducer producer; 
MyFrameConsumer consumer; 
Frame frame; // frame should probably be in the while loop, since its 
while(!producer.eof()) // lifetime doesn't need to exist outside the loop 
{ 
    producer >> frame; 
    consumer << frame; 
} 

您可能会使:

struct MyFrameProducer {}; // add an eof function 
struct MyFrameConsumer {}; 
struct Frame {}; 

// producer produces a frame 
MyFrameProducer& operator>>(MyFrameProducer& p, Frame& f) 
{ 
    /* whatever it takes to make a frame */ 

    return p; 
} 

// consumer consumes a frame 
MyFrameConsumer& operator<<(MyFrameConsumer& c, const Frame& f) 
{ 
    /* whatever it takes to use a frame */ 

    return c; 
} 

或者一个类似于它。 (对不起,我对这个问题的理解很少。)想要这个接口有点奇怪,因为它与流无关,而且你可能更适合使用不同的接口(显式方法)。

+0

不完全一样,你展示的是如何将用户类型转换为字符串。 我一直在寻找这样的事情(这是自STL的istream对象不起作用/ ostream的似乎是基于字符的) 类MyFrameProducer:istream的 {} 类MyFrameConsumer:ostream的 {} int main() { MyFrameProducer producer; MyFrameConsumer consumer; 框架; (!producer.eof()) { producer >> frame;消费者<<框架; } } – ronag 2010-08-21 20:42:20

+1

@ronag:我没有*显示如何将用户类型转换为字符串,我展示了如何将其插入流中。 (只有巧合的'stringstream'提供了一种生成字符串的方法。)让我编辑您的评论片段的答案。 – GManNickG 2010-08-21 20:45:45

+0

我修正了一些问题,但现在它是社区wiki,对不起。 – Philipp 2010-08-21 21:14:59

2

看看this生产者/消费者解决方案。虽然它是纯粹的C它是如此优雅,所以我无法拒绝发布它。

+1

有趣的是,这似乎也很有趣:http://www.crystalclearsoftware.com/soc/coroutine/ – ronag 2010-08-21 20:43:30